我正在開發基於谷歌地圖的應用程序,用戶必須在地圖中顯示當前位置。我有谷歌它,並找到解決方案,但是當我運行的代碼它不顯示當前的位置,而是它顯示谷歌地圖單獨。我的代碼是低於如何在Android中通過GPS獲取當前位置
public class GPSLocation extends MapActivity {
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private GPSLocationListener locationListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// mapView = (MapView) findViewById(R.id.mapView);
// enable Street view by default
// mapView.setStreetView(true);
// enable to show Satellite view
// mapView.setSatellite(true);
// enable to show Traffic on map
// mapView.setTraffic(true);
// mapView.setBuiltInZoomControls(true);
// mapController = mapView.getController();
// mapController.setZoom(16);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
}
private class GPSLocationListener implements LocationListener
{
public GPSLocationListener() {
// TODO Auto-generated constructor stub
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
System.out.println("onLocationChanged");
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
Toast.makeText(getBaseContext(),
"Latitude: " + location.getLatitude() +
" Longitude: " + location.getLongitude(),
Toast.LENGTH_SHORT).show();
mapController.animateTo(point);
mapController.setZoom(16);
mapView.invalidate();
}
else
{
System.out.println("OnLocationChanged value is null");
}
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
我不知道我犯了什麼錯誤。它甚至沒有進入GPSLocationListener類。請幫助我.. 在此先感謝。
謝謝我從你的代碼中得到了很多幫助。 :) –