答案 0 :(得分:2)
有不同的方法,
你需要添加一个位置监听器。有两种类型,一种是使用GPS本身(这不会在室内工作),使用网络。
private LocationManager locationManager;
public LocationListener locationListener;
public LocationListener locationListener2;
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
locationListener2 = new MyLocationListener();
locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 0, 0, locationListener2);
//Location lister
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location loc) {
mlongti = loc.getLongitude();
mlatiti = loc.getLatitude();
GeoPoint userLoc = new GeoPoint((int) (mlatiti * 1E6), (int) (mlongti * 1E6));
LItemizedOverlay itemizedoverlay = new LItemizedOverlay(selfImage,getParent(), "",userLoc,display);
try {
OverlayItem overlayitem = new OverlayItem(userLoc, "", "");
itemizedoverlay.addOverlay(overlayitem);
} catch (Exception e) {
e.printStackTrace();
}
List<Overlay> listOfOverlays = map.getOverlays();
listOfOverlays.add(itemizedoverlay);
MapController mc = map.getController();
mc.animateTo(userLoc);
mc.setZoom(10);
map.invalidate();
if(locationManager!=null && locationListener2 != null){
locationManager.removeUpdates(locationListener);
locationManager.removeUpdates(locationListener2);
locationManager= null;
}
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}