1

Possible Duplicate:
Google Map V3 : Only some markers are displayed

I need help with google maps.. I have the following code working with 1 property shown on the map, but I have many addresses/properties to show them on the same map and have no success.

var address = "<?php echo $address?>";
       var map = new google.maps.Map(document.getElementById('map'), {
           mapTypeId: google.maps.MapTypeId.ROADMAP,
           zoom: 16
       });
       var geocoder = new google.maps.Geocoder();
       geocoder.geocode({
          'address': address
       }, 
       function(results, status) {
          if(status == google.maps.GeocoderStatus.OK) {
             new google.maps.Marker({
                position: results[0].geometry.location,
                map: map
             });
             map.setCenter(results[0].geometry.location);
          }
       });

From php I get the address but JS side scripting is hard for me.. Please help and show me what needs to be changed so the adress to be JS array and how to show all addresses.

Community
  • 1
  • 1
thecore7
  • 484
  • 2
  • 8
  • 29
  • It's likely that you will have to call the `geocoder` as many times as the number of addresses you have, and create a marker for each. – tchap Dec 31 '12 at 14:10
  • thats ok, but How to do it? As I said I am not familar with JS scripting side.. I believe some litle changes need to apply to the code, but what they are? – thecore7 Dec 31 '12 at 14:11

1 Answers1

0

You're looking for a plain Javascript for loop over you array I guess, so this should be something like this (assuming address is a valid Javascript array) :

// address = ... //define address array here

var map = new google.maps.Map(document.getElementById('map'), {
       mapTypeId: google.maps.MapTypeId.ROADMAP,
       zoom: 16
   });
var geocoder = new google.maps.Geocoder();

var nbAddresses = address.length;

for (var i=0;i<nbAddresses;i++)
{ 

  geocoder.geocode({
          'address': address[i]
   }, 
   function(results, status) {
      if(status == google.maps.GeocoderStatus.OK) {
         new google.maps.Marker({
            position: results[0].geometry.location,
            map: map
         });
         map.setCenter(results[0].geometry.location);
      }
   });

}

EDIT : Please note that this code snippet can be optimized, it's just a quick way to address (no pun intended) your need ;)

tchap
  • 3,412
  • 3
  • 29
  • 46
  • dear tchap, thanks for your reply, but the code doesn't work or at least doesnt work when I copy /paste it.. Iget error Undifined geocoder.geocode function.. – thecore7 Dec 31 '12 at 14:28
  • 1
    That will work for ~10 addresses, then start getting OVER_QUERY_LIMIT errors and failing (not displaying markers, because it doesn't check the returned status). – geocodezip Dec 31 '12 at 14:28
  • Lets replace var address = ""; with address1 = 'adress1'; and address2 = 'adress2'; instead of that php variable - it will be esier for me to understand what and where to replace – thecore7 Dec 31 '12 at 14:29
  • I will have between 5-9 addresses per page where it will be used, so limit issue is not a problem – thecore7 Dec 31 '12 at 14:32
  • I didn't copy/paste all your code, just the relevant bit. So you have to insert it in your code just after defining geocoder. And @geocodezip is right : you will likely hit the rate limit ... – tchap Dec 31 '12 at 14:33
  • Now it's edited with your bit of code included. But you have to define a valid javascript array ... – tchap Dec 31 '12 at 14:35
  • dear tchap, it is on its way to start working :) I copied your code and first did not see change, but after alerted address to check is it array or not I received 11 alerts , so I was not right about the number of addresses - in this case they are 11 but they do not appear together on the map, just alert 11 times and only 1 address is shown on the map.. What could cause this? Thank you for your help – thecore7 Dec 31 '12 at 14:46
  • Post your full code to jsfiddle.net so we can see where's the problem – tchap Dec 31 '12 at 15:07
  • 1
    tchap, seems the problem to be in the php array, so before I post it want to check it first.. will be back soon, thank you ! – thecore7 Dec 31 '12 at 15:30
  • tchap, the problem was with the php $address variable - it was not array, now it is an array but I am experience troubles to turn it into js array. Please help me with this last issue: till now I did this var address = []; but it is wrong! – thecore7 Dec 31 '12 at 16:20
  • try : `var address = ;` – tchap Dec 31 '12 at 17:29
  • Happy New Year tchap ! When I use var address = ; I get this errors this.a.j is null and address is null - any ideas? – thecore7 Jan 01 '13 at 11:21
  • Problem fixed thanks to tchap ! I was using wrong variable and after I changed it I've got all addresses marked on the map ! Thanks to all of you guys! – thecore7 Jan 01 '13 at 11:27
  • I am new here and can not upvote, just accepted your answer :) But a new issue became after that - shall I start a new question? Thanks – thecore7 Jan 02 '13 at 08:27
  • Yes that's better to start a brand new question – tchap Jan 02 '13 at 08:28