开发者

How to change the overlay position dynamically based on the GPS data

I have an app in which I'm trying to simulate real time tracking so I'm reading some GPS data from a class where I have stored the data and try to put those points on the map and I also add a marker to teh current position.

The problem that I'm facing is that once I add an overlay to one position it stays there...and when I'm reading the next position and put it on the map and add a marker 开发者_运维技巧to this to....I have two overlays on my map...

What I want to do is to put the overlay at each current position ...like it would walk on the map...and have only one overlay on the map at the position that I'm representing at that time...al the previous overlays from the previous ppositions to dissappear but I can't really know how to accomplish that.

Hope u can help me!

This is my code:

public void onCreate(Bundle savedInstanceState)
{
   marker = getResources().getDrawable(R.drawable.comet);
   marker.setBounds(0,0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
}

----------------I'm using an Async thread to read GPS data-------------------------

protected Void doInBackground(Void... voids) 
{
   while (true) {
     longitude = ServerManager.getInstance().getLastLongitude();
     latitude = ServerManager.getInstance().getLastLatitude();
     p = new GeoPoint(latitude, longitude);
     publishProgress(p);
    }
  }

  protected void onProgressUpdate(GeoPoint... progress1) 
  {
    mapView.getOverlays().add(new SitesOverlay(marker,progress1[0]));
  }
}

Finally this is my classs which adds overlays:

private class SitesOverlay extends ItemizedOverlay<OverlayItem> 
{
    private List<OverlayItem> items = new ArrayList<OverlayItem>();
    private Drawable marker = null;
    GeoPoint gp1;

    public SitesOverlay(Drawable marker,GeoPoint gp1)
    {
       super(marker);
       this.marker = marker;
       this.gp1=gp1;
       items.add(new OverlayItem(gp1,"Your location", "You are here!"));
       populate();
    }

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

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) 
    {
        super.draw(canvas, mapView, shadow);
        boundCenterBottom(marker);
    }

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


If you use:

mapView.getOverlays().add(new SitesOverlay(marker,progress1[0]));

then you can also use:

mapView.getOverlays().remove(i);

mapView.getOverlays() returns an array of overlays that should be drawn. You can modify this array at any time. More here: http://code.google.com/intl/pl-PL/android/add-ons/google-apis/reference/com/google/android/maps/MapView.html#getOverlays()

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜