我现在正在处理与Google地图服务相关的项目。我想指出我目前在我的地图中的位置。为此,我有了使用itemizedoverlay的想法。我在Geopoint的帮助下检索当前位置。但是当我在代码中使用itemizedoverlay时,无法拾取位置,我在我的OnCreate方法中使用它。如果我将它移动到OnLocationChanged方法,那么GeoPoint(point)将不会被itemedoverlay拾取,并且会以错误结束,并且在当前情况下,应用程序被压缩。当前Loation不采摘android
MapView mapView;
Location location;
LocationManager locationmanager;
LocationListener locationlistener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setTraffic(true);
mapView.setBuiltInZoomControls(true);
//locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// locationlistener = new GPSLocationListener();
// locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER 0,0,locationlistener);
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();
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
OverlayItem overlayitem = new OverlayItem(point,"","");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
/* private class GPSLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
if (location != null) {
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();
OverlayItem overlayitem = new OverlayItem(point,"","");
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
} */
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getBaseContext(),"GPS Disabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getBaseContext(),"GPS Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
您能否为我推荐一个解决方案?先谢谢了,如果我在这里犯了错误,我很抱歉。
虽然我没有得到当前位置的地图,它会工作兄弟吗? – Tony
GPS位置并不总是专门在室内使用。所以如果你有GPS设置位置监听器,你在室内,那么你可能不会收到GPS信号。 MyLocationOverlay查找使用哪个提供程序并设置位置侦听器并获取位置 –
如果看到enableMyLocation的文档,它会显示“尝试启用MyLocation,并从LocationManager.GPS_PROVIDER和LocationManager.NETWORK_PROVIDER注册更新”。因此,它会注册提供商和您无需担心设置监听器并重新更新覆盖以进行更新。 –