0
我有以下的onCreate()在我的Android應用:安卓:無法獲取當前位置,以顯示
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customer_map);
context=getApplicationContext();
mapView = (MapView) findViewById(R.id.map);
mapView.setBuiltInZoomControls(true);
mapView.setEnabled(true);
mapOverlays = mapView.getOverlays();
//set my location marker
myLocationOverlay = new MyLocationOverlay(context, mapView);
myLocationOverlay.disableCompass();
myLocationOverlay.enableMyLocation();
mapOverlays.add(myLocationOverlay);
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapView.getController().animateTo(myLocationOverlay.getMyLocation());
}
});
//and zoom to current location
MapController mc=mapView.getController();
GeoPoint centre=new GeoPoint((int)(getBestLocation().getLat()*1e6),(int)(getBestLocation().getLng()*1e6));
mc.setCenter(centre);
mc.animateTo(centre);
mc.setZoom(12);
}
正如你所看到的,我想顯示當前位置。但它根本行不通!我覺得我在做一些愚蠢的事情,但是在最後一個小時我無法弄清楚。
任何有識之士非常感謝。非常感謝提前,
p.s.這裏是getBestLocation:
private Coords getBestLocation() {
Coords returnCoords=new Coords();
Criteria criteria=new Criteria();
//criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAccuracy(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider=lm.getBestProvider(criteria, true);
if(provider!=null){
lm.requestLocationUpdates(provider, 2000, 1, this);
List<String> providers = lm.getProviders(false);
//Loop over the array backwards, and if you get an accurate location, then break out the loop
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null){
returnCoords.setLat(l.getLatitude());
returnCoords.setLng(l.getLongitude());
break;
//Log.i("EOH",String.valueOf(l.getLatitude()));
}
}
}else{
Toast.makeText(context, "Location not available", 3000).show();
}
return returnCoords;
}
你看到了什麼?你錯過了覆蓋層,還是完全錯過了mapview? – theAlse 2012-07-06 12:39:45
準確地說,你遇到的問題是什麼?據我所知,你的getBestLocation方法可能會導致問題。 – AAnkit 2012-07-06 12:45:28
我錯過了標記。地圖出來很好。 – Eamorr 2012-07-06 12:51:56