0

What I want to achieve is to have 2 kinds of markers displayed on a map.

1)Red markers that display events drawn from a database (done that - itemizedOverlay)

2)Orange marker which will dynamically display the user's location on the map(itemizedOverlay2).

I'm using ItemizedOverlay to have the 2 different layers (one for events and one for user location) but I have an issue with the dynamic marker that shows user position.

ISSUE:

What happens is the LocationListener successfully gets the latitude/longitude and places a point on the map BUT the previous point still exists. So I have a trail of markers showing the user's location.

I have implemented a few ways I found searching the internet but none of which happen during the calling of onLocationChanged.

class myLocationListener implements LocationListener{
        OverlayItem overlayItem;


        @Override
        public void onLocationChanged(Location location) {

            if (location != null){

                mapOverlays.remove(itemizedOverlay2);
                mapView.invalidate();

                lat = location.getLatitude();
                longi = location.getLongitude();
                GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (longi * 1E6));
                mControl.animateTo(point);

                overlayItem = new OverlayItem(point, "My Location", "This is probably where you are");
                itemizedOverlay2.addOverlay(overlayItem);
                mapOverlays.add(itemizedOverlay2);

            }

        }

I know that you might need more of my code but let's just stay at this block see if someone sees where I'm getting it wrong.

Thank you.

bubbly
  • 495
  • 1
  • 10
  • 22
  • This link might be useful to you : [Android MapView can't remove marker](http://stackoverflow.com/a/4634287/3577656). – Rajal Aug 12 '14 at 11:56

2 Answers2

1

You haven't posted the code for the class that itemizedOverlay2 belongs to, but I'm guessing it extends ItemizedOverlay. Since it has an addOverlay() method, I'm also guessing that it has a .clear() method.

If so, the you should call the clear() before you add the new location

itemizedOverlay2.clear();
itemizedOverlay2.addOverlay(overlayItem);
NickT
  • 23,844
  • 11
  • 78
  • 121
  • you are correct about itemizedOverlay2 extends a class called MyItemizedOverlay thing is there is not clear() method. clear() method is available for the mapOverlays mapOverlays.clear() Which I have tried and it clears BOTH itemizedOverlay and itemizedOverlay2 – bubbly Apr 25 '12 at 16:00
  • You should write a clear() method then. public void clear() {mOverlays.clear();} – NickT Apr 25 '12 at 16:10
  • Wouldn't that remove BOTH items? (itemizedOverlay and itemizedOverlay2) ? – bubbly Apr 25 '12 at 17:27
  • No it wouldn't, call clear to remove the item that's already on the overlay, then addOverlay to add the new one, as in the posted answer. – NickT Apr 25 '12 at 17:39
  • You are right, it worked. I'm new to android and even though I tried the clear() method out, I cleared both the items. Didn't think about using it the way you just mentioned :) Silly of me. Thank you. – bubbly Apr 25 '12 at 17:46
0

Use built in class MyLocationOverlay :

position = new MyLocationOverlay(this, mapView);
position.enableMyLocation();
mapOverlays.add(position);
vtuhtan
  • 1,056
  • 7
  • 18
  • I have considered that, but it is an alternative way, not a solution to my problem. But with MyLocationOverlay I don't know how to change frequency of receiving the position and generally I do not know if it can do all the things what the LocationListener can. – bubbly Apr 25 '12 at 16:03