0

我正在使用此代码查看用户的当前位置。但我还需要在上面做一个标记。我已经看过一些关于此的教程,但我仍然不明白。

这是我的活动

public class MapViewActivity extends MapActivity {
    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        setContentView(R.layout.mapview);


//------------Start Displaying Map View--------------//     
     LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

            LocationListener locationListener = new LocationListener() {
                public void onLocationChanged(Location location) {
                    int lat = (int) (location.getLatitude() * 1E6);
                    int lng = (int) (location.getLongitude() * 1E6);
                    GeoPoint test= new GeoPoint(
                            lat,lng);

                   MapView mapview = (MapView) findViewById(R.id.mvmap);
                   MapController mapcontrol = mapview.getController();
                   mapcontrol.animateTo(test);
                   mapcontrol.setZoom(10);
                   mapview.setStreetView(true);
                   mapview.setSatellite(true);
                   mapview.setBuiltInZoomControls(true);
                }


                public void onProviderDisabled(String provider) {
                    // TODO Auto-generated method stub

                }

                public void onProviderEnabled(String provider) {
                    // TODO Auto-generated method stub

                }

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

                }
            };
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
4

1 回答 1

1

将此类用于 ItemizedOverlay。

public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {

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

private Context context;

public CustomItemizedOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
}

public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
    this(defaultMarker);
    this.context = context;
}

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

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

@Override
protected boolean onTap(int index) {
    OverlayItem item = mapOverlays.get(index);
    Log.e(" on tap item value ", "" + mapOverlays.get(index) + " Index "
            + index);
    AlertDialog.Builder dialog = new AlertDialog.Builder(context);
    dialog.setTitle(item.getTitle());
    dialog.setMessage(item.getSnippet());
    dialog.show();

    return true;
}

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

public void removeOverlay() {
    mapOverlays.clear();
    // this.populate();
}

}

在您的 MapViewActivity 中,声明,

 CustomItemizedOverlay itemizedOverlay;
List<Overlay> mapOverlays;

mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.marker);
itemizedOverlay = new CustomItemizedOverlay(drawable, this);


OverlayItem overlayitem = new OverlayItem(test," "," ");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
于 2012-08-06T08:31:10.790 回答