On Map Region Change in MapView Android Sdk

余生颓废 提交于 2019-12-12 04:24:35

问题


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?


回答1:


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...




回答2:


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

    }
});


来源:https://stackoverflow.com/questions/4829170/on-map-region-change-in-mapview-android-sdk

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