Issue
Dear Folks, I have an osmdroid map version 4.2, and slf version 1.5.8
I also have this java code set for it (down there). My issue is, where ever I set the "setCenter" method, and launch the map, then i try to navigate,the system right away shifts me a back towards the defualt coordinates that i have set previously.
any idea how to allow me to freely navigate with predefined centers?
package com.example.com.example;
import org.osmdroid.api.IMapController;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewTreeObserver;
public class OSM_Map extends Activity {
private MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_osm__map);
mapView = (MapView) this.findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);
//IMapController mapController = mapView.getController();
mapView.getController().setZoom(11);
ViewTreeObserver vto = mapView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() { //VERY IMPORTANT, DRAWS LAYOUT FIRST, then positions
mapView.getController().setCenter(new GeoPoint(40.712784,-74.005941));
}
});
}
}
Thank you
Solution
The onGlobalLayout listener is being called repeatly and resets the center to your predefined point afterwards.
You need to remove the listener by adding the following after you set the center initially:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
else
mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
Answered By - headuck
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.