开发者

adding single marker on map, Android, Google API

开发者 https://www.devze.com 2023-02-23 07:56 出处:网络
On my application i receive coordinates from a remote server and i want to mark the location on the coordinates on a map, this happens on demand inside onClick method. The problem is that when i updat

On my application i receive coordinates from a remote server and i want to mark the location on the coordinates on a map, this happens on demand inside onClick method. The problem is that when i update the location i end up with multiple markers on the map instead of just one, the current location. is there any way to remove the previous marker before adding the next one?

I followed the steps in this tutorial : http://developer.android.com/resources/tutorials/views/hello-mapview.html

And my code goes like this :

public class AppTwoAndroid extends MapActivity {
private Button refreshButton;
double lat, lon;
ConnectionHandler conhandler;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    Log.i("AppTwo", "making connectionhandler object");
    conhandler = new ConnectionHandler();
    conhandler.execute();

    Log.i("AppTwo", "making button");
    this.refreshButton = (Button)this.findViewById(R.id.close);

    final List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
    final AppTwoAndroidItemizedOverlay itemizedoverlay = new AppTwoAndroidItemizedOverlay(drawable);

    refreshButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.i("AppTwo", "inside onclick");
            if (mapOverlays.contains(itemizedoverlay) == true) {
                mapOverlays.remove(itemizedoverlay);
            }
            conhandler.write();
            lat = conhandler.retLat();
            lon = conhandler.retLon();
            lat = lat * 1e6;
            lon = lon * 1e6;
            int ilat = (int) lat;
            int ilon = (int) lon;
            GeoPoint point = new GeoPoint(ilat ,ilon);
            OverlayItem overlayitem = new OverlayItem(point, null, "AppOne");
            itemizedoverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedoverlay);


            Toast.makeText(getBaseContext(), "lat is: " + lat + " lon is: " + lon
                    ,  Toast.LENGTH_SHORT).show();

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

and my AppTwoAndroi开发者_运维知识库dItemizedOverlay class is :

public class AppTwoAndroidItemizedOverlay extends ItemizedOverlay {

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

public AppTwoAndroidItemizedOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    // TODO Auto-generated constructor stub
}

@Override
protected boolean onTap(int index) {
  OverlayItem item = mOverlays.get(index);
  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
  dialog.setTitle(item.getTitle());
  dialog.setMessage(item.getSnippet());
  dialog.show();
  return true;
}

public AppTwoAndroidItemizedOverlay(Drawable defaultMarker, Context context) {
      super(defaultMarker);
      mContext = context;
}

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

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

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

}


In the onClick method just give

mapOverlays.clear();

this should clear any existing markers.

Hope this works :)


In this part of code:

   if (mapOverlays.contains(itemizedoverlay) == true) {
        mapOverlays.remove(itemizedoverlay);
    }

When your removing the Overlay from the mapOverlay structure you are not really clearing the overlay, so when you add other item and re-add it to the mapOverlay there will be 2 markers.

If you just want a single marker do an Overlay that has setOverlayItem instead of a list with an 'adding' logic. (meaning do a overlay with just an item that when you add another, just replaces the old one)

Hope it helped! :D


Add this statement before the code....

mMap.clear();

    mMap.clear();

    LatLng latLng = new LatLng(gps.getLatitude(),  gps.getLongitude());
    mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new 
    LatLng(gps.getLatitude(),  gps.getLongitude()), 15));
    mMap.addMarker(new MarkerOptions().position(latLng).title(""));
    mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
0

精彩评论

暂无评论...
验证码 换一张
取 消