On Map Region Change in MapView Android Sdk
I am implementing a Map Kit based application in Android. I was very new to this 开发者_开发百科sdk. My problem is that I need to fire a method when the Map Region changed. Can you guys please let me know is it possible to fire a method when the region is changed?
the map region will change when the map perform pan or zoom methods but you cannot obtain any info from this methods, so you can doit through the onDraw method. To achieve this you have to subclass the MapView and overrides the onDraw method.
if you do this you can obtain the four coordinates that limits the region displayed, with Projection class retrieveing coordinates for each point ( top-left[0,0], top-right[width-0], bottom-left[0,height] and bottom-right[width,height] ).
in example, in the first onDraw: you get this four coordinates, GeoPoint[4] init in the second run of onDraw you get the new four coordinates, GeoPoint[4] end, so here you can compare the regions.
this is a expensive operation so a delay mechanism will be helpful to slowdown the changes detection...
You can use MapView's onCameraChange event like below:
final Fragment mapFragment = getFragmentManager().findFragmentById(R.id.map);
mMap = ((MapFragment)mapFragment).getMap();
mMap.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition position) {
Point point = new Point(mapFragment.getView().getWidth() / 2 , mapFragment.getView().getHeight()/2);
LatLng l = mMap.getProjection().fromScreenLocation(point);
Log.v("asd","center lat: " + l.latitude + " center long: " + l.longitude + " ");
}
});
精彩评论