我正在开发基于谷歌地图的应用程序,用户必须在地图中显示当前位置。我有谷歌它,并找到解决方案,但是当我运行的代码它不显示当前的位置,而是它显示谷歌地图单独。我的代码是低于如何在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类。请帮助我.. 在此先感谢。
谢谢我从你的代码中得到了很多帮助。 :) –