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:
      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);

Open in new window


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
LVL 1
derridaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
because you create the map in the $.each loop.

Please try this:
      function initialize() {
        var mapCanvas = document.getElementById('map');
		var mapOptions = {
			center: theposition,
			zoom: 16,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(mapCanvas, mapOptions);

        /*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 marker = new google.maps.Marker({
						position: theposition,
						map: map
					});
				});
            }
        });

        /*ajax*/

    }
    google.maps.event.addDomListener(window, 'load', initialize);

                                

Open in new window


So moving the map options and map creation outside of the ajax call / each loop.

HTH
Rainer
derridaAuthor Commented:
Hi Rainer
first thanks for answering. i thought and try that, but then, in the mapOptions variable how do i use the "theposition" ?
Rainer JeschorCommented:
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

derridaAuthor Commented:
Hi
how would you use the setCenter? without passing it the position outside of the loop?
Rainer JeschorCommented:
Hi,
you would need to select one of the locations and use that one to be used in the setCenter like
      function initialize() {
        var mapCanvas = document.getElementById('map');
		var mapOptions = {
			zoom: 16,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		var map = new google.maps.Map(mapCanvas, mapOptions);
		var centerLat = '';
		var centerLng = '';
        /*ajax*/

        $.ajax({
            url: 'get-locations.php',
            success:function(data){
                //Loop through each location.
				// Sample use of first data
				centerLat = data[0].lat;
				centerLng = data[0].lng;
                $.each(data, function(){
					//Plot the location as a marker
                    var theposition = new google.maps.LatLng(this.lat, this.lng); 
					var marker = new google.maps.Marker({
						position: theposition,
						map: map
					});
				});
				map.setCenter(centerLat,centerLng);
            }
        });

        /*ajax*/

    }
    google.maps.event.addDomListener(window, 'load', initialize);

Open in new window


Could you perhaps provide a sample of your data Array, so that I can prepare a sample at jsFiddle?

Thanks.
Rainer

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
derridaAuthor Commented:
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
derridaAuthor Commented:
this done it:
map.setCenter(new google.maps.LatLng(centerLat,centerLng));
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.