1 (edited by adrian.blanco 2019-08-02 10:44:41)

Topic: Tile To Coordinates with float zoom

Hi, I am making an app that shows some pois on the map and then draws a circle around each poi with its range (The user has to be inside the range to interact with it).

The circles have a fixed radius that has to be refreshed accordingly with the zoom of the map, I use a code that I found in this post:
http://forum.infinity-code.com/viewtopi … 3651#p3651

My code:

private void UpdatePoiRangeCircle(MyMarker myMarker)
    {
        OnlineMaps mapInstance = OnlineMaps.instance;
        Vector2 distanceBetweenTileCorners = OnlineMapsUtils.DistanceBetweenPoints(mapInstance.topLeftPosition, mapInstance.bottomRightPosition);
        float radiusInPixels = (myMarker.poi.range / 1000f) * OnlineMapsTileSetControl.instance.sizeInScene.x / distanceBetweenTileCorners.x;
        float r = radiusInPixels / OnlineMapsUtils.tileSize;

        float step = 360f / poiCircleSegments;
        double x, y;
        myMarker.OMmarcador.GetPosition(out x, out y);
        OnlineMapsProjection projection = mapInstance.projection;
        projection.CoordinatesToTile(x, y, mapInstance.zoom, out x, out y);

        for (int i = 0; i < poiCircleSegments; i++)
        {
            double px = x + Mathf.Cos(step * i * Mathf.Deg2Rad) * r;
            double py = y + Mathf.Sin(step * i * Mathf.Deg2Rad) * r;

            // Old way (Online Maps v2.3):
            // OnlineMapsUtils.TileToLatLong(px, py, map.zoom, out px, out py);

            projection.TileToCoordinates(px, py, mapInstance.zoom, out px, out py);

            myMarker.poiCircle.points[i] = new Vector2((float)px, (float)py);
        }

    }

This works fine but the problem is that when zooming in or out the circle doesn't update correctly until it reaches an int value of the zoom.

I attached a video of what's happening.

Is there any way to get the coordinates from tile position using a float zoom instead of an int zoom?

Post's attachments

Attachment icon t_video5920002858597484201 (3).mp4 952.15 kb, 91 downloads since 2019-08-02 

Re: Tile To Coordinates with float zoom

Hello.

Your link does not contain scripts.
Your code is insufficient for testing.

I understand correctly that you want to draw a circle with a radius in meters?
Example of how to do this:

using System;
using UnityEngine;

public class DrawCircleAroundMarker : MonoBehaviour
{
    public float radiusKM = 0.1f;
    public int segments = 32;

    private void Start()
    {
        OnlineMapsControlBase.instance.OnMapClick += OnMapClick;
    }

    private void OnMapClick()
    {
        double lng, lat;
        OnlineMapsControlBase.instance.GetCoords(out lng, out lat);

        OnlineMapsMarkerManager.CreateItem(lng, lat, "Marker " + OnlineMapsMarkerManager.CountItems);

        OnlineMaps map = OnlineMaps.instance;
        
        double nlng, nlat;
        OnlineMapsUtils.GetCoordinateInDistance(lng, lat, radiusKM, 90, out nlng, out nlat);

        double tx1, ty1, tx2, ty2;
        map.projection.CoordinatesToTile(lng, lat, 20, out tx1, out ty1);
        map.projection.CoordinatesToTile(nlng, nlat, 20, out tx2, out ty2);

        double r = tx2 - tx1;

        OnlineMapsVector2d[] points = new OnlineMapsVector2d[segments];

        double step = 360d / segments;

        for (int i = 0; i < segments; i++)
        {
            double px = tx1 + Math.Cos(step * i * OnlineMapsUtils.Deg2Rad) * r;
            double py = ty1 + Math.Sin(step * i * OnlineMapsUtils.Deg2Rad) * r;
            map.projection.TileToCoordinates(px, py, 20, out lng, out lat);
            points[i] = new OnlineMapsVector2d(lng, lat);
        }

        OnlineMapsDrawingElementManager.AddItem(new OnlineMapsDrawingPoly(points, Color.red, 3));
    }
}

Please note that when using Drawing API, you do not need to update points when changing the position or zoom of the map.

Kind Regards,
Infinity Code Team.

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