derrida
asked on
google maps multiple markers issue
Hi
i have an ajax call that gring the lat and lng from a database, and it is passed ok. my issue is that i only get the last marker and not all. this is the code i have:
the map is showing and also the last mark. i do build it inside of the loop so i don't see what i am missing.
best regards
i have an ajax call that gring the lat and lng from a database, and it is passed ok. my issue is that i only get the last marker and not all. this is the code i have:
function initialize() {
var mapCanvas = document.getElementById('map');
/*ajax*/
$.ajax({
url: 'get-locations.php',
success:function(data){
//Loop through each location.
$.each(data, function(){
//Plot the location as a marker
var theposition = new google.maps.LatLng(this.lat, this.lng);
var mapOptions = {
center: theposition,
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions);
var marker = new google.maps.Marker({
position: theposition,
map: map
});
});
}
});
/*ajax*/
}
google.maps.event.addDomListener(window, 'load', initialize);
the map is showing and also the last mark. i do build it inside of the loop so i don't see what i am missing.
best regards
ASKER
Hi Rainer
first thanks for answering. i thought and try that, but then, in the mapOptions variable how do i use the "theposition" ?
first thanks for answering. i thought and try that, but then, in the mapOptions variable how do i use the "theposition" ?
Hi,
ah sorry, I did not see this.
The main question would be: where do you want to center if you have multiple locations?
If possible I would remove this configuration from the options and call the explicit "setCenter" function afterwards - if you know where to center.
HTH
Rainer
ah sorry, I did not see this.
The main question would be: where do you want to center if you have multiple locations?
If possible I would remove this configuration from the options and call the explicit "setCenter" function afterwards - if you know where to center.
HTH
Rainer
ASKER
Hi
how would you use the setCenter? without passing it the position outside of the loop?
how would you use the setCenter? without passing it the position outside of the loop?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi
i get this error:
InvalidValueError: setCenter: not a LatLng or LatLngLiteral: not an Object
even though when i output the data[0].lat it has a value
i get this error:
InvalidValueError: setCenter: not a LatLng or LatLngLiteral: not an Object
even though when i output the data[0].lat it has a value
ASKER
this done it:
map.setCenter(new google.maps.LatLng(centerL at,centerL ng));
map.setCenter(new google.maps.LatLng(centerL
because you create the map in the $.each loop.
Please try this:
Open in new window
So moving the map options and map creation outside of the ajax call / each loop.
HTH
Rainer