pekechis
pekechis

Reputation: 388

Android Adding new point to a MapView (don't refresh)

I'm working in a Treasure Quest that guides the user through a ruote. I'm having problems showing my current location in a map where quest points are also showed

I extended ItemizedOverlay

public class QuestItemizedOverlay extends ItemizedOverlay<OverlayItem> {

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

    public QuestItemizedOverlay(Drawable defaultmarker) {
        super(boundCenterBottom(defaultmarker));
        populate();     
    }

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

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

    public void removeItem(int index) {
        mOverlays.remove(index);
        populate();
    }

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

}

I got the listener when my location change:

private final LocationListener locationlistener= new LocationListener() {
        public void onLocationChanged(Location location) {
            Log.i("LOCATION_UPDATED","");
            updateWithNewLocation(location);
            mapview.invalidate();
    }
        public void onProviderDisabled(String provider) {

        }

        public void onProviderEnabled(String provider) { }
        public void onStatusChanged(String provider, int sttus, Bundle extras) { }
    };

And updatewithNewlocation method has the following code:

private void updateWithNewLocation(Location location) {
      Double curlat=location.getLatitude();
          Double curlong=location.getLongitude();
          GeoPoint point=new GeoPoint((int)(curlong*1E6),(int) (curlat*1E6));
          OverlayItem overlayitem=new OverlayItem(point,"I'm here","This is where you are");
          Drawable marker=this.getResources().getDrawable(R.drawable.eplogo_marker);          
          overlayitem.setMarker(marker);
          itemizedoverlay.addOverlay(overlayitem);
          mapOverlays.clear();
          mapOverlays.add(itemizedoverlay);
}

Debugging with DDMS (just testing) the listener works perfectly and my mapview mOverlays grows as i change the location with DDMS but when i call mapview.invalidate() nothings happens

Wich was the way to refresh the mapview?

And one more question, what happens if a mapOverlay has more than one itemizedoverlay each with some markers?

Thanks in advance

Upvotes: 2

Views: 5158

Answers (1)

Yilmaz Guleryuz
Yilmaz Guleryuz

Reputation: 9745

@pekechis: after you update mapOverLays (adding new etc) just call

mapView.postInvalidate();

this will refresh map view. your problem may also be caused by not calling this inside UI thread, e.g. like that

mActivity.runOnUiThread(new Runnable() {
 public void run() {            
   mapView.postInvalidate();
  }
});

Upvotes: 6

Related Questions