Mapview on a tablet: how can I center a map with an offset?

Hint: here is a similar post with HTML.

In the current version of the application of my application, I have a full-screen MapView with some information displayed in the RelativeLayout in the left pane, for example:

(My layout is pretty trivial, and I think there is no need to publish it for readability)

enter image description here

The problem occurs when I want to focus the map on a specific point ... If I use this code:

mapController.setCenter(point); 

Of course, I will get a point in the center of the screen, and not in the center of an empty area.

I have no idea where I can start turning the left panel offset to the map coordinates ...

Thanks so much for any help or suggestion.

+4
source share
1 answer

You can achieve your goal by getting the map coordinates from the top-left and bottom-right angles and divide it by the screen size to get the value per pixel.

Then you just need to multiply by the offset and add it to the original center.

Code example:

 private void centerMap(GeoPoint center, int offX, int offY){ GeoPoint tl = mapView.getProjection().fromPixels(0, 0); GeoPoint br = mapView.getProjection().fromPixels(mapView.getWidth(), mapView.getHeight()); int newLon = offX * (br.getLongitudeE6() - tl.getLongitudeE6()) / mapView.getWidth() + center.getLongitudeE6(); int newLat = offY * (br.getLatitudeE6() - tl.getLatitudeE6()) / mapView.getHeight() + center.getLatitudeE6(); mapController.setCenter(new GeoPoint(newLat, newLon)); } 

For use, you call the method above with the original center and both offsets (x and Y) to apply.

Note: as indicated, the code above moves the map to the left for positive offset values ​​and to the right for negative offset values. On the screen of your question, you will need to use a negative offset, move the map to the left and a zero offset for Y.

Hello

+3
source

Source: https://habr.com/ru/post/1445192/


All Articles