Topic: Zoom To Cluster

Hi there,

I'm using the Clustering2DMarkers script that was posted in another thread and it works great.

I'm now trying to implement the following functionality:

-> Click on a cluster marker
-> Center and zoom the map to the ClusterItem's child markers

You can see an example of the effect that I'm trying to achieve here:
https://developers.google.com/maps/docu … clustering

When you click on a cluster, it zooms to the "extents" of the cluster's children.

I'm having trouble getting the correct array of positions to calculate the zoom and center values... I can't figure out how to recursively get all the individual marker positions which make up a cluster.

Can anyone help??

Re: Zoom To Cluster

Hello.

Out of the box, this script cannot do this.
But you can easily add it, for example like this:

// ...
public class Clustering2DMarkers : MonoBehaviour
{
    public static Action<Cluster, OnlineMapsMarker> OnCreateInstance;
    // ...
    public class Cluster : ClusterItem
    {
        // ...
        private void TryGetMarkers(int z, double tlx, double tly, double brx, double bry, ref List<OnlineMapsMarker> markers)
        {
            if (zoom < z)
            {
                for (int i = 0; i < count; i++) childs[i].GetMarkers(z, tlx, tly, brx, bry, ref markers);
            }
            else
            {
                if (markerRef == null)
                {
                    UpdatePosition();

                    markerRef = new OnlineMapsMarker();
                    markerRef.SetPosition(longitude, latitude);
                    markerRef.texture = instance.groupTexture;
                    markerRef.label = "Group (childs: " + totalCount + ")";

                    if (OnCreateInstance != null) OnCreateInstance(this, markerRef);

                    markerRef.Init();
                }

                markers.Add(markerRef);
            }
        }
        // ...
    }
    // ...
}
using UnityEngine;
using System.Collections.Generic;

public class ZoomOnCluster : MonoBehaviour
{
    public int countMarkers = 50000;
    public Texture2D texture;

    private void GenerateMarkers()
    {
        Clustering2DMarkers.OnCreateInstance += OnCreateInstance;

        for (int i = 0; i < countMarkers; i++)
        {
            double longitude = Random.Range(-180f, 180f);
            double latitude = Random.Range(-90f, 90f);

            OnlineMapsMarker marker = new OnlineMapsMarker();
            marker.SetPosition(longitude, latitude);
            marker.label = "Marker " + i;
            marker.texture = texture;
            marker.Init();
            marker.OnPositionChanged += delegate (OnlineMapsMarkerBase m)
            {
                Debug.Log("Position Changed");
                Clustering2DMarkers.UpdateMarkerPosition(m as OnlineMapsMarker);
            };

            Clustering2DMarkers.Add(marker);
        }

        Clustering2DMarkers.UpdatePositions();
    }

    private void OnCreateInstance(Clustering2DMarkers.Cluster cluster, OnlineMapsMarker marker)
    {
        marker.OnClick += m =>
        {
            var cc = cluster;
            while (cc.count == 1)
            {
                cc = cc.childs[0] as Clustering2DMarkers.Cluster;
            }

            List<Vector2> points = new List<Vector2>();
            for (int i = 0; i < cc.count; i++)
            {
                var child = cc.childs[i];
                var subCluster = child as Clustering2DMarkers.Cluster;
                if (subCluster != null)
                {
                    double lng, lat;
                    OnlineMapsUtils.MercatToLatLong(subCluster.mx.Value, subCluster.my.Value, out lng, out lat);
                    points.Add(new Vector2((float)lng, (float)lat));
                }
                else points.Add(child.markerRef.position);
            }

            Vector2 center;
            int zoom;
            OnlineMapsUtils.GetCenterPointAndZoom(points.ToArray(), out center, out zoom);
            OnlineMaps.instance.SetPositionAndZoom(center.x, center.y, zoom - 1);
        };
    }

    private void Start()
    {
        GenerateMarkers();
    }
}
Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Zoom To Cluster

Thanks for the quick reply...

I'm getting an error that theres no MercatToLatLong function in the Utils class... I just updated to latest stable release (3.0.0.33) but doesn't help

Re: Zoom To Cluster

This script is for Online Maps v2, so I thought that you are using the old version.
In this case, use:

OnlineMaps.instance.projection.TileToCoordinates(subCluster.mx.Value, subCluster.my.Value, 0, out lng, out lat);
Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Zoom To Cluster

Awesome, thanks for that.

Another question: how would I go about animating the markers position as they transition from a marker into a cluster group?

For example if at zoom level 10 I have 2 markers on screen and at zoom level 11 I have one cluster group which contains the two markers, how can I animate the markers so they look like they are “flying” from their position into the cluster group’s position?

I would like to do the reverse too if that possible.

Any direction you can give me on that would be hugely appreciated!!

Thanks again for your help so far

Re: Zoom To Cluster

Unfortunately, with this script you cannot do this.
In this world, nothing is impossible, but it requires such changes that in fact it will be a new script.
Of course, you can try, but it will be very difficult.
So I recommend you to think about some other effect.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: Zoom To Cluster

Ok. Thought it might be too difficult. Any ideas for some kind of transition effect??

Re: Zoom To Cluster

There can be many effects that are easy to implement.
For example, scale down / scale up markers.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.