I'm using locationManager and ItemizedOverlay to draw My Location marker, the problem is when onLocationChanged is triggered I'm getting new marker drawed and not the last one moved to the new location, here is my onLocationChanged code :
public void onLocationChanged(Location location) {
myOverlay object1 = new myOverlay(getResources().getDrawable(R.drawable.arrow),MyMap);
if(location!=null){
MyMap.invalidate();
GeoPoint MyPos = new GeoPoint(microdegrees(location.getLatitude()),microdegrees(location.getLongitude()));
MyController.animateTo(MyPos);
object1.addPoint(MyPos,"Ma position","Ma position");
MyMap.getOverlays().add(object1);
}
}
Can you please help me solve this issue ?
Thanks a lot.
MapActivity :
public class Main extends MapActivity implements LocationListener {
/** Called when the activity is first created. */
MapView MyMap;
MapController MyController;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyMap=(MapView)findViewById(R.id.MyGMap);
MyMap.setBuiltInZoomControls(true);
try {
getEventsFromAnXML(this);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int j=0;
myOverlay object = new myOverlay(getResources().getDrawable(R.drawable.marker),MyMap);
while( j<agencies.size()){
GeoPoint point = new GeoPoint(microdegrees(agencies.get(j).getLatitude()),microdegrees(agencies.get(j).getLongitude()));
String Tit;
String Des;
Tit=agencies.get(j).getTspTitle();
Des=agencies.get(j).getTspTitle() + "\nAgence: " + agencies.get(j).getTitle() +
"\nTél: " + agencies.get(j).getPhone();
object.addPoint(point,Tit,Des);
j=j+1;
}
MyMap.getOverlays().add(object);
MyController=MyMap.getController();
MyController.setZoom(12);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
}
private int microdegrees(double value){
return (int)(value*1000000);
}
boolean na=false;
List<Agency> agencies = new ArrayList<Agency>();
Agency agency=new Agency();
int i=0;
String TempTspTitle;
String TempTspPhone;
Boolean TempTspEnabled;
private String getEventsFromAnXML(Activity activity)
throws XmlPullParserException, IOException
{
StringBuffer stringBuffer = new StringBuffer();
Resources res = activity.getResources();
XmlResourceParser xpp = res.getXml(R.xml.hotels);
xpp.next();
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_DOCUMENT)
{
stringBuffer.append("--- Start XML ---");
}
else if(eventType == XmlPullParser.START_TAG)
{
if (xpp.getName().equals("DataBase")){
String name=xpp.getAttributeValue(null, "name");
agency.setTspTitle(name);
TempTspTitle=name;
na=true;
}
if (xpp.getName().equals("DataBaseEnabled")){
xpp.next();
agency.setTspEnabled(Boolean.parseBoolean(xpp.getText()));
TempTspEnabled=Boolean.parseBoolean(xpp.getText());
xpp.nextTag();
}
if (xpp.getName().equals("Title")){
xpp.next();
agency.setTitle(xpp.getText());
na=false;
xpp.nextTag();
}
if (xpp.getName().equals("Address")){
xpp.next();
agency.setAddress(xpp.getText());
xpp.nextTag();
}
if (xpp.getName().equals("Phone") && na==true){
xpp.next();
agency.setTspPhone(xpp.getText());
TempTspPhone=xpp.getText();
xpp.nextTag();
}else{
if (xpp.getName().equals("Phone") && na==false){
xpp.next();
agency.setPhone(xpp.getText());
xpp.nextTag();
}
}
if (xpp.getName().equals("Fax")){
xpp.next();
agency.setFax(xpp.getText());
xpp.nextTag();
}
if (xpp.getName().equals("e-Mail")){
xpp.next();
agency.setMail(xpp.getText());
xpp.nextTag();
}
if (xpp.getName().equals("Latitude")){
xpp.next();
agency.setLatitude(Double.parseDouble(xpp.getText()));
xpp.nextTag();
}
if (xpp.getName().equals("Longitude")){
xpp.next();
agency.setLongitude(Double.parseDouble(xpp.getText()));
}
}
else if(eventType == XmlPullParser.END_TAG)
{
if (xpp.getName().equals("DataBase") ){
agency = new Agency();
}else if (xpp.getName().equals("Agency")){
agencies.add(i,agency);
i=i+1;
agency = new Agency();
agency.setTspTitle(TempTspTitle);
agency.setTspPhone(TempTspPhone);
agency.setTspEnabled(TempTspEnabled);
}
}
eventType = xpp.next();
}
stringBuffer.append("\n" + "Size: " + agencies.size());
return stringBuffer.toString();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public void onLocationChanged(Location location) {
myOverlay object1 = new myOverlay(getResources().getDrawable(R.drawable.arrow),MyMap);
if(location!=null){
MyMap.getOverlays().remove(object1);
MyMap.invalidate();
GeoPoint MyPos = new GeoPoint(microdegrees(location.getLatitude()),microdegrees(location.getLongitude()));
MyController.animateTo(MyPos);
object1.addPoint(MyPos,"Ma position","Ma position");
MyMap.getOverlays().add(object1);
}
}
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
}
and myOverlay Class :
public class myOverlay extends ItemizedOverlay<OverlayItem>{
List<OverlayItem> points= new ArrayList<OverlayItem>();
private Drawable marker=null;
private Context c;
public myOverlay(Drawable marker, MapView mapView) {
super(marker);
this.marker=marker;
c=mapView.getContext();
}
@Override
protected OverlayItem createItem(int i) {
return (points.get(i));
}
public void draw(Canvas canvas,MapView mapView, boolean shadow){
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}
@Override
public int size() {
return points.size();
}
public void addPoint(GeoPoint point, String Titre, String Desc){
this.points.add(new OverlayItem(point,Titre,Desc));
populate();
}
public void removePoint(){
this.points.clear();
populate();
}
@Override
protected boolean onTap(int i){
Toast.makeText(c, points.get(i).getSnippet(), Toast.LENGTH_SHORT).show();
return (true);
}
}
}
getOverlays()
returns the List of Overlay objects in your MapView. Each time the location is updated, your code is adding another overlay to this list. You need to do is remove the previous overlay, or empty the list entirely each time.So, in your method call
getOverlays().clear()
before adding your new overlay.Edit, alternatively maybe this will point you in the right direction? I don't know exactly how your myOverlay class works, so hopefully you can fill in the blanks!
Use an ItemizedOverlay, and as Dave suggested, keep track of the one you want moved and then either delete the tracked OverlayItem and add a new one or take the tracked one, get its marker or MapPoint and reset its lat/lon coordinates to the new location coordinates. You may also have to call mapView.invalidate() to force a redraw.
according to Dave's suggestion this fixed the problem :
thanks..