Mapview on tablet: How can I center the map with an offset?

自闭症网瘾萝莉.ら 提交于 2019-12-07 07:05:47

问题


Hint: Here is a similar post with HTML.

In the current tablet implementation of my app, I have a fullscreen MapView with some informations displayed in a RelativeLayout on a left panel, like this:

(My layout is quite trivial, and I guess there is no need to post it for readability)

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

mapController.setCenter(point);

I will of course get the point in the center of the screen and not in the center of the empty area.

I have really no idea where I could start to turn the offset of the left panel into map coordinates...

Thanks a lot for any help or suggestion


回答1:


You can achive your objective by getting the map coordinates from top-left and bottom-right corners 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.

Example code:

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));

}

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

Note: as written, the code above move map left for positive offset values, and right for negative offset values. From the screen in your question you will need to use negative offset, to move map left, and a zero offset for Y.

Regards



来源:https://stackoverflow.com/questions/13320386/mapview-on-tablet-how-can-i-center-the-map-with-an-offset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!