mapbox-unity-sdk icon indicating copy to clipboard operation
mapbox-unity-sdk copied to clipboard

How do I check if a coordinate is in the currently displayed map

Open CrazyBoyLanZao opened this issue 2 years ago • 2 comments

I have a object need to update when I change the scale of the map. But now I do not know it's coordinate in the map of displayed ? so How can I do for judge the coordinate is in scope of the displaying map

CrazyBoyLanZao avatar Mar 01 '22 07:03 CrazyBoyLanZao

Maybe there is an mapbox-way of doing this. Otherwise there are plenty approaches to check this using c# or unity api tools.

my first approach would be to calculate a bounding box based on your screen viewport based on bottom-left / top-right coordindates. Maybe you'll have to shoot a raycast to find the corresponding coordinate on the map. Based on this boundingrect you can go on with your calculation.

a bite more simple would be to calculate the normalized screen position of the gameobject you want to check:

Vector3 screenPoint = Camera.main.WorldToViewportPoint(_pooledGameObjects[i].transform.position);
bool onScreen = screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1;

Never tried but looks like what you are searching for:

Unity api provides the following callback

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnBecameInvisible.html

Markovicho avatar Mar 16 '22 08:03 Markovicho

there isn't a built in solution for this but there's a few possible solutions;

  • if you don't need too much precision; you can convert your latlng into tileId and check if that tile is currently loaded (Map.MapVisualizer.ActiveTiles). It probably wouldn't be precise because tile might be there but exact latlng might be outside the screen.
  • another solution is, as @Markovicho suggested, converting latlng to unity space and then converting unity space to screen space.

brnkhy avatar May 17 '22 09:05 brnkhy