我现在正在处理一个与谷歌地图服务相关的项目。我想在我的地图上指出我当前的位置。为此,我有了使用 itemizedoverlay 的想法。我在 Geopoint 的帮助下检索当前位置。但是当我在我的代码中使用 itemizedoverlay 时,无法获取该位置,我在我的 OnCreate 方法中使用它。如果我将其移至 OnLocationChanged 方法,则 GeoPoint(point) 将不会被 itemizedoverlay 拾取并且会以错误结束,在目前的情况下,应用程序会被压垮。
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
}
你能建议我解决这个问题吗?提前致谢,如果我在这里犯了任何错误,我们深表歉意。