adding markers in cycle

75 views
Skip to first unread message

George

unread,
Dec 29, 2010, 3:09:58 PM12/29/10
to Google Maps JavaScript API v3
geocoder.geocode( {
'address': loc
}, function(results, status) {
if(status=='OK'){
if(results[0].types[0]=='locality') var icon = '/
css/blue_mini.png';
if(results[0].types[0]=='country') var icon = '/
css/green_big_select.png';
//console.log(markers.length);
markers[markers.length] = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
icon: icon
});

google.maps.event.addListener(markers[markers.length-1], 'click',
function() {/*infowindow gets only the last marker*/
var infowindow = new google.maps.InfoWindow({
content: title
});

infowindow.open(map,markers[markers.length-1]);
});
}
else alert('не удается найти объект');
});
this code is running in cycle setting markers and infowindows by name
of input objects.. but in fact only the last maker get its infowindow
and all other makers refernse their clicks to this marker

geoco...@gmail.com

unread,
Dec 29, 2010, 8:17:27 PM12/29/10
to Google Maps JavaScript API v3
On Dec 29, 3:09 pm, George <colorix.stu...@gmail.com> wrote:
> this code is running in cycle setting markers and infowindows by name
> of input objects.. but in fact only the last maker get its infowindow
> and all other makers refernse their clicks to this marker

Sounds like pitfall #3 in this page of Mike Williams' v2 tutorial :
http://econym.org.uk/gmap/basic1.htm

Please read and follow the posting guidelines and post a link to your
map that exhibits the problem, not code:
http://groups.google.com/group/google-maps-js-api-v3/t/2b3f101fd509919e

-- Larry

George

unread,
Dec 30, 2010, 12:53:11 AM12/30/10
to Google Maps JavaScript API v3
Ok, this problem was solved with the use of function to create markers
and their infowindows, but now some more interesting one.
http://tripcatcher.ru/guide/region/5 click in right block "Посмотреть
карту Европы" then you will get map processing code ajax and it'll
execute
this code won't set any markers it'll just console.log nessasury data
to make this markers informative later.. and it gives me empty arrays.
look like geocode works in asynchronous way. any ideas how to set
different contents for different windows?

geoco...@gmail.com

unread,
Dec 30, 2010, 4:46:08 AM12/30/10
to Google Maps JavaScript API v3
On Dec 30, 12:53 am, George <colorix.stu...@gmail.com> wrote:
> Ok, this problem was solved with the use of function to create markers
> and their infowindows, but now some more interesting one.http://tripcatcher.ru/guide/region/5click in right block "Посмотреть
> карту Европы" then you will get map processing code ajax and it'll
> execute
> this code won't set any markers it'll just console.log nessasury data
> to make this markers informative later.. and it gives me empty arrays.
> look like geocode works in asynchronous way.

It does.

> any ideas how to set
> different contents for different windows?

Use function closure to connect the infowindow contents to the marker
(createMarker function)
Use function closure to connect the address with the geocoded
coordinates

Beware, geocoding is subject to rate limits and a quota, so it is
better not to geocode lots of known addresses each time your page
loads, geocode them off-line, save the coordinates and use those to
create the markers on your map.

-- Larry
Reply all
Reply to author
Forward
0 new messages