p-lr / MapCompose

A fast, memory efficient Jetpack Compose library to display tiled maps, with support for markers, paths, and rotation.
Apache License 2.0
220 stars 19 forks source link

How to use Latitude / Longitude to zoom on a location? #73

Closed thegrxp closed 2 years ago

thegrxp commented 2 years ago

I've managed to display a world map from MapBox using Static Tiles and it's working well. I would like to display a specific location when I open the map, but I don't get how I'm supposed to do it using Latitude and Longitude. Also I want to be able to zoom out to see the entire world map. Thank you for your help

p-lr commented 2 years ago

Well, the answer isn't that simple. But it's relatively easy to do. If you used WMTS servers, the map size should be 67108864 (a square). This number is the size of the world map in Web-mercator projection. Here's an example of a configuration for such a case:

val mapSize = 67108864
val mapState = MapState(levelCount = 19, fullWidth = mapSize, fullHeight = mapSize, workerCount = 16).apply {
    addLayer(tileStreamProvider)
}

Let's say you have a latitude and longitude point. All you need to do is to convert those lat/lon coordinates to Web-Mercator cordinates. See the formulas.

Basically: lat/lon ----(projection) ---> X/Y X and Y are the actual coordinates on the map. To place a marker :

mapState.addMarker("id", X, Y) {
   _some_composable_
}
thegrxp commented 2 years ago

Nice, thank you!