如果我不能通过第一次调用myOverlay.getMyLocation来获取GeoPoint,那么如何从myOverlay.runOnFirstFix(new Runnable()…线程中找到的第二个调用中传递lat和lon值.你会注意到我一直在尝试将lat和lon添加到MyApp,它是辅助bean类,但是即使在刷新之后,此类中的lat和lon也为null.如果我在addUserMapPoint函数中手动设置lat和lon第一次活动访问这些值是保留的.我猜这是因为它是在主UI线程上设置的.
public class MapActivity extends com.google.android.maps.MapActivity { private MapView mapView = null; private MapController mapController = null; private MyLocationOverlay myOverlay = null; public static MyApp app; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map); app = (MyApp) getApplicationContext(); mapView = (MapView) findViewById(R.id.mapview); mapView.setBuiltInZoomControls(true); mapController = mapView.getController(); List<Overlay> mapOverlays = mapView.getOverlays(); mapOverlays.clear(); addUserMapPoint(mapView); if (!app.isLocServOff()) { //map other points – service call to get items from our service near lat and lon addOtherMapPoints(mapOverlays); } else { Toast.makeText(app.getApplicationContext(),"Current location could not be found.",Toast.LENGTH_LONG).show(); } } private void addUserMapPoint(MapView mapView){ myOverlay = new MyLocationOverlay(app.getApplicationContext(),mapView); myOverlay.disableCompass(); myOverlay.enableMyLocation(); if(app.getMyLat()==null||app.getMyLon()==null){ GeoPoint gp = myOverlay.getMyLocation(); if(gp!=null){ app.setMyLat(Helper.latLon1E6Calc(gp.getLatitudeE6())); app.setMyLon(Helper.latLon1E6Calc(gp.getLongitudeE6())); app.setLocServOff(false); }else{ app.setLocServOff(true); } } myOverlay.runOnFirstFix(new Runnable() { public void run() { GeoPoint gp = myOverlay.getMyLocation(); if(gp!=null){ app.setMyLat(Helper.latLon1E6Calc(gp.getLatitudeE6())); app.setMyLon(Helper.latLon1E6Calc(gp.getLongitudeE6())); app.setLocServOff(false); mapController.animateTo(gp); }else{ app.setLocServOff(true); } } }); mapView.getOverlays().add(myOverlay); } }
我们要求您提供以下问题的帮助.
如何在主UI线程中获得包含lat和lon的GeoPoint,或者如何从GeoPoint传递这些值,我可以从myOverlay.runOnFirstFix(new Runnable()…线程获取?
如果您打算建议我使用Handler或runOnUiThread,请提供代码示例,将lat和lon传递回主UI线程/地图视图可以使用的内容.我尝试过以下代码,但没有产生预期的结果.我能够得到吐司的消息,但是无法以我可以使用的方式获得lat和lon.
final Handler handler = new Handler(); myOverlay.runOnFirstFix(new Runnable() { @Override public void run() { handler.post(new Runnable() { @Override public void run() { GeoPoint gp = myOverlay.getMyLocation(); if(gp!=null){ app.setMyLat(Helper.latLon1E6Calc(gp.getLatitudeE6())); app.setMyLon(Helper.latLon1E6Calc(gp.getLongitudeE6())); app.setLocServOff(false); mapController.animateTo(gp); }else{ app.setLocServOff(true); } //Toast.makeText(getApplicationContext(),"wowoowowowoowoowowow",Toast.LENGTH_LONG).show(); } }); } });
我也使用了以下代码来获取lat和lon并且它可以工作,但是因为当前位置有时会比whas被返回不同的lat和lang因为我无法获得gps信号但是旧的价值被退回.我添加了检查以查看lat / lon数据是否超过2分钟,但我仍然无法将最新的lat和lon与myOverlay.getMyLocation返回的相匹配.
LocationManager locMgr = (LocationManager)appcontext.getSystemService(Context.LOCATION_SERVICE); MyLocationListener locLstnr = new MyLocationListener(); //fetch current location for current location locMgr.requestSingleUpdate(LocationManager.GPS_PROVIDER,locLstnr,appcontext.getMainLooper());
解决方法
handler.post(new Runnable() { @Override public void run() { GeoPoint gp = myOverlay.getMyLocation(); if(gp!=null){ app.setMyLat(Helper.latLon1E6Calc(gp.getLatitudeE6())); app.setMyLon(Helper.latLon1E6Calc(gp.getLongitudeE6())); app.setLocServOff(false); // HERE WE HAVE VALID gp VALUE AND WE NEED TO SHARE IT mapController.animateTo(gp); }else{ app.setLocServOff(true); } } });
我认为您的app.set / get | MyLat / Lon无法正常工作,因为您从不同的线程调用它们.要修复它同步set和获取MyLat / Long的方法. (创建用于同步和同步的对象)
或者,如果你喜欢处理程序的方式,这应该工作:
final Handler handler = new Handler(); // BE SURE TO RUN THIS LINE ON UI THREAD ... myOverlay.runOnFirstFix(new Runnable() { @Override public void run() { // THIS PART WORKS AS BEFORE final GeoPoint gp = myOverlay.getMyLocation(); mapController.animateTo(gp); ... // AND THIS RUNNABLE TO UPDATE MyLat/MyLong FROM UI THREAD handler.post(new Runnable() { @Override public void run() { app.setMyLat(Helper.latLon1E6Calc(gp.getLatitudeE6())); app.setMyLon(Helper.latLon1E6Calc(gp.getLongitudeE6())); }); } });