在当前位置映射查看标记

时间:2012-08-06 08:18:19

标签: android android-mapview google-maps-markers

我正在使用此代码查看用户的当前位置。但我还需要在上面加上标记。我已经看过一些关于这方面的教程,但我仍然没有得到它。

这是我的活动

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);

1 个答案:

答案 0 :(得分: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);