Topic: Ratio scale to zoom

Hi all,

I'm trying to convert ratio scales (e.g. 1:20,000) to a float zoom level.

Is there a utility method for this within OnlineMaps? Or do we have a reference table?

For instance, how would I calculate the following scales into their respective zoom level?

1:200,000
1:150,000
1:100,000
1:50,000
1:25,000
1:10,000

Many thanks.

Re: Ratio scale to zoom

Hello.

Something like that:

using UnityEngine;

namespace InfinityCode.OnlineMapsSupport
{
    public class SetFloatZoomByScale : MonoBehaviour
    {
        private OnlineMaps map;

        private void OnGUI()
        {
            if (GUILayout.Button("1:200,000")) SetScale(200000);
            if (GUILayout.Button("1:150,000")) SetScale(150000);
            if (GUILayout.Button("1:100,000")) SetScale(100000);
            if (GUILayout.Button("1:50,000")) SetScale(50000);
            if (GUILayout.Button("1:25,000")) SetScale(25000);
            if (GUILayout.Button("1:10,000")) SetScale(10000);
        }

        private void Start()
        {
            map = GetComponent<OnlineMaps>();
        }

        private void SetScale(int scale)
        {
            float s = (float)scale / map.width * OnlineMapsUtils.tileSize;
            double prevDistance = 0;

            double lng, lat;
            map.GetPosition(out lng, out lat);

            for (int z = 2; z < OnlineMaps.MAXZOOM; z++)
            {
                double tx, ty;
                map.projection.CoordinatesToTile(lng, lat, z, out tx, out ty);

                double lng1, lat1, lng2, lat2;
                map.projection.TileToCoordinates(0, (int)ty, z, out lng1, out lat1);
                map.projection.TileToCoordinates(1, (int)ty, z, out lng2, out lat2);

                double distance = OnlineMapsUtils.DistanceBetweenPoints(lng1, lat1, 0, lng2, lat2, 0) * 1000;
                if (distance < s)
                {
                    Debug.Log($"Z: {z}, Distance: {distance}, Prev Distance: {prevDistance}, Scale: {scale}, S: {s}");

                    map.floatZoom = z - (float)((s - distance) / (prevDistance - distance));
                    break;
                }

                prevDistance = distance;
            }

            ValidateZoom();
        }

        private void ValidateZoom()
        {
            const float equator = 40075000;
            float s;
            if (map.zoom < 5) s = Mathf.RoundToInt(equator / (1 << map.zoom) * map.zoomCoof * map.width / OnlineMapsUtils.tileSize);
            else
            {
                double lng, lat, lng1, lat1, lng2, lat2;
                map.GetPosition(out lng, out lat);
                map.GetTopLeftPosition(out lng1, out lat1);
                map.GetBottomRightPosition(out lng2, out lat2);
                s = Mathf.RoundToInt((float)OnlineMapsUtils.DistanceBetweenPoints(lng1, lat, 0, lng2, lat, 0) * 1000);
            }
            Debug.Log(s);

        }
    }
}
Kind Regards,
Infinity Code Team.

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

Re: Ratio scale to zoom

Thank you this helped me a bunch!