我的 Android 应用程序中有以下 onCreate() :
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);
}
如您所见,我正在尝试显示当前位置。但它根本行不通!我想我在做一些愚蠢的事情,但在过去的一个小时里我无法弄清楚。
任何见解都非常感谢。提前谢谢了,
ps这里是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;
}