开发者

How to update latitude/longitude of the center of a Mapview whenever map is moved

What is the best way to constantly update the center of a mapView's latitude and longitude in an Android application?

I need to display it on a label that is shown on top of the map开发者_如何学运维. I know you can call MapViewObject.getMapCenter() to get a GeoPoint of the center of the map, but where would I place that code so that it is called every time the user moves the map or zooms in/out?


I eventually answered the question on my own by adding the call to getMapCenter in the draw method of an overlay.

 public MyOverlay(TextView lblCoords) {
        super();
        coordinateLabel = lblCoords;
    }

    @Override
        public void draw(Canvas canvas, MapView mapView, boolean shadow) {
            super.draw(canvas, mapView, shadow);
            currentCenter = mapView.getMapCenter();
            latitude = currentCenter.getLatitudeE6() / 1E6;
            longitude = currentCenter.getLatitudeE6() / 1E6;
            coordinateLabel.setText("lat: " + latitude + " long: " + longitude);
    }

You can also extend the MapView class and put the coordinate code in the onInterceptTouchEvent method so it is updated only when the user moves the screen. This would be more efficient than putting it in an overlays onDraw, because onDraw is called constantly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜