1 (edited by mikli1 2023-12-07 15:28:34)

Topic: Center map on marker, but within a smaller rect

I have the map on fullscreen with markers (vertical orientation). When I select a marker I have a popup that takes the bottom half of the screen. Is there a way I can center the map on the selected marker but within the remaining space for map? I know there is a method GetCenterPointAndZoom but it does not allow to pass the rect bounds.

Thanks for help.

Re: Center map on marker, but within a smaller rect

Hello.

Something like that:

/// <summary>
/// Centers the map on the marker with the specified vertical offset in pixels.
/// </summary>
/// <param name="marker">Marker</param>
/// <param name="offsetY">Vertical offset in pixels</param>
private void CenterOnMarker(OnlineMapsMarkerBase marker, float offsetY = 0)
{
    OnlineMaps map = OnlineMaps.instance;
    double tx, ty;
    marker.GetTilePosition(map.zoom, out tx, out ty);
    ty -= offsetY / OnlineMapsUtils.tileSize;
    map.SetTilePosition(tx, ty);
}
Kind Regards,
Infinity Code Team.

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

3 (edited by mikli1 2023-12-07 15:58:44)

Re: Center map on marker, but within a smaller rect

Thanks for quick reply. The problem is that I'm using UI markers manager and my markers don't inherit from OnlineMapsMarkerBase. I guess that is an important detail I've forgot to mention.

How can I convert longitude and latitude to the tile position?

EDIT. I can use map.projection.CoordinatesToTile I guess. I'll try it.

I'm getting wierd results. It moves the map to far-away place. My code:


public void CenterOnMarker(double latitude, double longitude, float offsetY = 0)
{
    OnlineMaps map = OnlineMaps.instance;
    Debug.Log("Map position before: " + map.position);
    Debug.Log("Center on marker: " + latitude + " " + longitude + " offset: " + offsetY + "  map zoom: " + map.zoom);

    map.projection.CoordinatesToTile(latitude, longitude, map.zoom, out double tx, out double ty);
    Debug.Log("tx: " + tx + " ty: " + ty + "   tileSize: " + OnlineMapsUtils.tileSize);
    ty -= offsetY / OnlineMapsUtils.tileSize;
    map.SetTilePosition(tx, ty);
    map.zoom = 14;

    Debug.Log("Map position after: " + map.position);
}

The logs im getting:

Map position before: (16.95, 52.40)
Center on marker: 52,41 16,91 offset: 209,44  map zoom: 12
tx: 2644,31128645833 ty: 1852,74819069953   tileSize: 256
Map position after: (24.00, 49.00)

Re: Center map on marker, but within a smaller rect

This is because in CoordinatesToTile (and the rest of API) the longitude of the first is expected.

Kind Regards,
Infinity Code Team.

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

5 (edited by mikli1 2023-12-08 08:15:16)

Re: Center map on marker, but within a smaller rect

Holy smokes. Thank you for that. Now it is working fine... but not always. Clicking on the same marker should always get the same results, but it is not the case. When I start the app the first click is always wrong. Then it is correct, but also it happens that the result is incorrect... I don't know what's happening there. It seems to me that when I zoom out the map and click the result is always wrong.

That is connected to the next thing I would like to ask:

I would like to zoom it smoothly to the marker. Is there a way to do that?
map.zoom = 14 just jumps to that zoom immediately and it breaks further calculations of center on marker.

Re: Center map on marker, but within a smaller rect

This is most likely due to the fact that you are setting zoom to 14.
In the method you calculate the shift in pixels based on the current zoom, which will be different for each zoom level.
To make this constant, calculate the shift for zoom - 14.

Just make a tween from the current zoom to the desired zoom.
This can be done with any tween framework or manually in Update using Lerp.

Kind Regards,
Infinity Code Team.

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

7 (edited by mikli1 2024-02-19 08:39:40)

Re: Center map on marker, but within a smaller rect

Hello, I've just discovered a problem with this solution.
On mobile device with pinch to zoom it is possible that map zoom is not an integer. In these cases the CoordinatesToTile method returns incorrect result, because it is casting the map zoom into an integer. In the editor the map zoom is always an integer (excluding the smooth zoom), and the problem is not occuring in the editor.

Is there any way I can fix that? If I change the CoordinatesToTile zoom parameter to float, how can I calculate the mapSize in mercator projection?

long mapSize = (long)OnlineMapsUtils.tileSize << zoom;

Re: Center map on marker, but within a smaller rect

Hello.  I'm currently on a business trip in another country.  I'll be back in a couple of days and answer your question.

Kind Regards,
Infinity Code Team.

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

Re: Center map on marker, but within a smaller rect

What needs to change:

ty -= offsetY / OnlineMapsUtils.tileSize * map.zoomFactor; // For v3.8 and earlier: zoomCoof.
Kind Regards,
Infinity Code Team.

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

Re: Center map on marker, but within a smaller rect

Thank you. Working perfectly.