我正在开发基于谷歌地图的应用程序,因为用户的当前位置必须在地图中显示。我有谷歌它并找到解决方案,但当我运行代码时,它不显示当前位置,而是单独显示谷歌地图。我的代码在
之下 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类。请帮我.. 提前谢谢。
答案 0 :(得分:1)
检查你的gps是否有效。改变最小距离和最小时间。例如:
requestLocationUpdates(LocationManager.GPS_PROVIDER,10,30,locationListener);
答案 1 :(得分:0)
您是在设备或模拟器上进行测试吗?因为在模拟器上你必须手动输入坐标。
另外,请确保以下其中一个文件位于您的清单文件中:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION" />
答案 2 :(得分:0)
请同时添加此行代码。它对我有用。
LocationManager.requestLocationUpdates(LocationManager.NETWORk_PROVIDER,0,0,locationListener);
如果仍然无效,请提供日志详细信息吗?
答案 3 :(得分:0)
最近的Google Maps V2允许您实施 OnMyLocationChangeListener ,因此您无需实现(并设置)自定义的LocationSource。