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