0

在这个我的代码中,我不明白我的代码哪里出错了。我没有在地图视图上用针点图像获得当前位置。如何获取纬度和纬度并通过地理点。然后传递值 OverlayItem ..

public class HelloGoogleMaps2 extends MapActivity implements LocationListener{

 private LocationManager locationManager;
 private String provider;
 int lat;
 int lng;
 MyLocationOverlay  myLocOverlay;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    MapView mapView = (MapView) findViewById(R.id.mapview);    
    mapView.setBuiltInZoomControls(true);

    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the locatioin provider -> use
    // default
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(provider);

    Log.d("provider  ","Provider  "+provider);
    Log.d("provider  ","Provider  "+provider);
    Log.d("provider  ","Provider  "+provider);
    Log.d("location","Location "+location);

    Log.d("location","Location "+location);        

    Log.d("location","Location "+location);
    // Initialize the location fields
    if (location != null) {
      System.out.println("Provider " + provider + " has been selected.");
      onLocationChanged(location);
    } else {
      //latituteField.setText("Location not available");
     // longitudeField.setText("Location not available");
    }


    List<Overlay> mapOverlays = mapView.getOverlays();

    myLocOverlay = new MyLocationOverlay(this, mapView);
    mapOverlays.add(myLocOverlay);
    myLocOverlay.enableMyLocation();
    GeoPoint point = new GeoPoint(lat, lng);
   //        mc.setCenter(point);
    //        mapView.invalidate();

    Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
    HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable);

      GeoPoint point = new GeoPoint(lat,lng);
OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");


itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}


@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

 protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
      }

      /* Remove the locationlistener updates when Activity is paused */
      //
      protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
      }

      //
      public void onLocationChanged(Location location) {
         lat = (int) (location.getLatitude()* 1E6);
         lng = (int) (location.getLongitude()* 1E6);
       // latituteField.setText(String.valueOf(lat));
       // longitudeField.setText(String.valueOf(lng));
      }

      //
      public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

      }

      //
      public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

      }

      //
      public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
      }

}

4

2 回答 2

2

正确的代码就是这个,还要加上uses-permissio,Internet,ACCESS_COARSE_LOCATION,ACCESS_FINE_LOCATION和WRITE_EXTERNAL_STORAGE.user-library。

  public class GoogleMapActivity extends  MapActivity implements LocationListener {
  private final static String TAG = GoogleMapActivity.class.getSimpleName();
private MyItemizedOverlay itemizedOverlay;

double lat;
double lng;
private String provider;
private LocationManager locationManager;

private MapView mapview;

Drawable drawable;



boolean sat = true;
boolean dra = true;

private MapController controller;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // fetch the map view from the layout
    mapview = (MapView) findViewById(R.id.mapview);

    // make available zoom controls
    mapview.setBuiltInZoomControls(false);

    // Use the location manager through GPS

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        lat = (double) (location.getLatitude());
        lng = (double) (location.getLongitude());

        Log.i(TAG, "Lattitude:" + lat);
        Log.i(TAG, "Longitude:" + lng);
        Toast.makeText(
                this,
                "Current location:\nLatitude: " + lat + "\n"
                        + "Longitude: " + lng, Toast.LENGTH_LONG).show();
        // create geo point
        GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

        // get the MapController object
        controller = mapview.getController();

        // animate to the desired point
        controller.animateTo(point);

        // set the map zoom to 13
        // zoom 1 is top world view
        controller.setZoom(13);

        // invalidate the map in order to show changes
        mapview.invalidate();

        drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
        OverlayItem overlayItem = new OverlayItem(point, "", "");
        itemizedOverlay = new MyItemizedOverlay(drawable, this);
        itemizedOverlay.addOverlay(overlayItem);
        mapview.getOverlays().add(itemizedOverlay);
        mapview.invalidate();
    } else {

        System.out.println("Location not avilable");
    }

    // when the current location is found – stop listening for updates
    // (preserves battery)
    locationManager.removeUpdates(this);

}

@Override
protected boolean isRouteDisplayed() {
    return false;

}

@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(provider, 400, 1, this);
}

/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

public void onProviderDisabled(String provider) {
    Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
}

public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();
}

public void onStatusChanged(String provider, int status, Bundle extras) {

}

public void onLocationChanged(Location location) {

}

public MapView getMapView() {
    return this.mapview;
}


    }

` //这里是另一个项目覆盖

    public class MyItemizedOverlay  extends ItemizedOverlay<OverlayItem>
     {



   private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();


    public MyItemizedOverlay(Drawable defaultMarker, Context ctx){ 
        super(boundCenterBottom(defaultMarker));

    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    public void clear() {
        mOverlays.clear();
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        return mOverlays.size();
    }

    @Override
    protected boolean onTap(int index) {
        return true;
    }

}

于 2012-10-19T07:08:18.233 回答
0

我会推荐MyLocationOverlay

用于在地图上绘制用户当前位置(和准确性)的叠加层,和/或罗盘玫瑰插图。子类可以覆盖 dispatchTap() 来处理当前位置的点击。
您可能需要调用 enableMyLocation() 和/或 enableCompass(),可能来自 Activity 的 Activity.onResume() 方法,以启用此叠加层的功能。请记住在 Activity 的 Activity.onPause() 方法中调用相应的 disableMyLocation() 和/或 disableCompass() 以在后台关闭更新。
可选地,构造函数还可以使用 MapController 并使用它来保持“我的位置”点可见,方法是在地图离开屏幕时平移地图,并在位置或方向发生更改时使用 View 到 View.postInvalidate()。
Runnables 可以在 runOnFirstFix(java.lang.Runnable) 中提供,以便在我们修复后立即运行。(例如,这可以使地图居中并放大以显示位置。)

您可以在此处找到有关 MyLocationOverlay 的好教程

于 2012-10-19T05:16:50.587 回答