Question : How can I prevent my object being overwritten in a for loop?

Ok here is my problem, (see code attached)
I have a method displayPosition(),
This updates a table of mine with data from a single result. It then overlays a marker on a map (a google map). This is done using the createMarker method

Now I have a second method displayPositionHistory, now what happens here is basically the same thing except there is more than one result coming in. Updating the table is fine, the problem occurs when it tries to create the markers. If I have three results. It creates only one marker essentially overwriting the first marker again and again instead of creating 3 individual markers.

My question is how can I prevent this from happening.

I am using the Google Maps API for GWT (Google Web Toolkit)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
public void displayPosition(GPSDataDTO data)
        {
        map.clearOverlays();
        map.addOverlay(createMarker(data));
        map.setCenter(LatLng.newInstance(data.getLat(),data.getLongitude()),14);
        }
    
    private Marker createMarker(GPSDataDTO data) 
        {
    	
        final Marker marker = new Marker(LatLng.newInstance(data.getLat(),data.getLongitude()));
        final GPSDataDTO p = data;
        
        marker.addMarkerClickHandler(new MarkerClickHandler() {
            public void onClick(MarkerClickEvent event) {
                InfoWindow info = map.getInfoWindow();
                info.open(marker,
                new InfoWindowContent(
					"ID: <b> " + p.getID() + "</b><br />" +
					"Date: <b>" + p.getDate() +"</b><br />" +
					"Time: <b>"+p.getTime() + "</b><br />" +
					"Latitude: <b>" + p.getLat() + "</b><br/>" +
					"Latitude Direction: <b>"+p.getLat_Dir() + "</b><br />" +
					"Longitude: <b>"+ p.getLongitude() + "</b><br />" +
					"Longitude Direction: <b>"+p.getLong_Dir() + "</b><br />" +
					"Speed: <b>" + p.getSpeed() + "</b>kph??<br />" +
					"Course: <b>" + p.getCourse() + "</b>Course<br />" +
					"IP Address: <b>" + p.getIp_add() + "</b>"));}
                    });
        
        return marker;
        
    }

	public void displayPositionHistory(Vector<GPSDataDTO> data) {
		map.clearOverlays();
		GPSDataDTO init = new GPSDataDTO();
		init = data.elementAt(0);
		
		map.setCenter(LatLng.newInstance(init.getLat(),init.getLongitude()),14);
		for (int i=0; i < data.size(); i++)
		{
			GPSDataDTO marker = (GPSDataDTO)data.elementAt(i);
		map.addOverlay(createMarker(marker));
		
	}
	}

Answer : How can I prevent my object being overwritten in a for loop?

GPSDataDTO marker ; //put this code out side of the loop

for (int i=0; i < data.size(); i++)  
               {  
                           marker = (GPSDataDTO)data.elementAt(i);  
               map.addOverlay(createMarker(marker));  
                 
       }  

Random Solutions  
 
programming4us programming4us