我的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);
}
如您所见,我正在尝试显示当前位置。但它根本行不通!我觉得我做的事情很傻,但我在最后一小时都搞不清楚。
任何见解都非常感谢。非常感谢,
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;
}
答案 0 :(得分:0)
好的,它现在正在运作。愚蠢的是,我这样做了:
@Override
protected void onPause() {
super.onPause();
if(myLocationOverlay!=null){
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}
}
@Override
protected void onResume() {
super.onResume();
if(myLocationOverlay!=null){
**myLocationOverlay.disableLocation();**
myLocationOverlay.disableCompass();
}
}
我有点克服!