views:

108

answers:

1

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?

A: 

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.

Austyn Mahoney