2015-09-06 21 views
0

亲爱的伙计们,我有一个osmdroid地图4.2版本,和SLF版本1.5.8OSMdroid继续导航回到setcenter的位置,为什么?

我也有这个java代码为其设置(那里)。 我的问题是,我在哪里设置“setCenter”方法,并启动地图,然后尝试导航,系统立即将我转回到之前设置的默认坐标。

任何想法如何让我自由浏览预定义的中心?

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)); 
      } 
     }); 

    } 


} 

谢谢

回答

0

的onGlobalLayout监听器被称为屡和中心后重置您的预定点。

您需要通过添加下面的删除监听您最初设置后的中心:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) 
      mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
     else 
      mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
+0

我做了globalLayout内setCenter之后然而插入此代码,它开枪报错了,说“呼叫需要api level 16 ...“在代码的第二行。不过,我只是添加了mapView.getViewTreeObserver()。removeGlobalOnLayoutListener(this);它完美的作品! – user3676224

+0

是的,函数从removeGlobalOnLayoutListener()更改为removeOnGlobalLayoutListener(),因为API 16是条件语句(Build.VERSION_CODES.JELLY_BEAN为16)的原因。但是如果你的目标sdk低于16,那么就调用removeGlobalOnLayoutListener()。 – headuck