3

我现在正在处理一个与谷歌地图服务相关的项目。我想在我的地图上指出我当前的位置。为此,我有了使用 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

    }

你能建议我解决这个问题吗?提前致谢,如果我在这里犯了任何错误,我们深表歉意。

4

4 回答 4

2

试试这个 -

/**
* Setting Google Map to provided location 
*/
 private void setMaptoLocation(Location location) {
  mapView.setBuiltInZoomControls(true);
  mapView.setSatellite(true);
  int LAT = (int)(location.getLatitude() * 1E6) ;
  int LNG = (int)(location.getLongitude() * 1E6) ;
  GeoPoint point = new GeoPoint(LAT, LNG);
  mapController = mapView.getController();
  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);
  mapController.setZoom(ZOOM_LEVEL - ZOOM_LEVEL / 2); // Set zoom level per requierment
  mapController.animateTo(point);

 }
于 2013-01-21T12:31:54.963 回答
2

您可以使用 com.google.android.maps.MyLocationOverlay,它会自动在地图视图上显示您的位置。它甚至会在旅行时更新您的位置。

注意:您分别在 onResume 和 onPause 上显示调用 enableMyLocation 和 disableMyLocation 以获得更好的性能。

@Override
    protected void onResume() {
        super.onResume();
        if (locationOverlay != null)
            locationOverlay.enableMyLocation();
    }

    @Override
    protected void onPause() {
        if (locationOverlay != null)
            locationOverlay.disableMyLocation();
        super.onPause();
    }

已编辑

MyLocationOverlay locationOverlay;

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      mapView = (MapView) findViewById(R.id.mapView);
      mapView.setTraffic(true);
      mapView.setBuiltInZoomControls(true);
locationOverlay = new MyLocationOverlay(this, mapView);
List<Overlay> mapOverlays = mapView.getOverlays();
 mapOverlays.add(locationOverlay);
}

还添加 onResume() 和 onPause() 方法。

于 2013-01-21T12:33:56.827 回答
0

我认为你必须与Google Maps Android API v2

有关更多信息,请查看Google Maps Android API v2

请从上面的链接中检查示例代码的链接。 示例代码

于 2013-01-21T12:23:01.857 回答
0

位置不是您在onCreate()中初始化的位置,所以我猜它为空。还有一件事,如果你有纬度和经度,逐项列出的肯定会在地图上显示位置。检查 Manifest 中所需的 Access_Course_locationAccess_fine_location权限。

于 2013-01-21T12:30:12.170 回答