English 中文(简体)
Geocoding town names to their coordinates in a loop
原标题:

I ve read similar posts, but still didn t find a solution for myself. Basically I have an array with countries+towns in PHP and I need to show them on the map with markers. Here is my code:

function showAddress(markers) {

    var address = "<?php echo $Fcity[$j], " , ", $Fcountry[$j]?>";
     if (geocoder) {
        geocoder.getLatLng(address, function(point) {
            if (!point) {
              alert(address + " not found");
            } else {

            var marker = new GMarker(point);
            map.addOverlay(marker);
            markers[i] = marker;        
              marker.openInfoWindowHtml(address);

            }
          }
        );
      }
    }

Everything seems to work if I geocode one location, but I can t put it into a loop to process all of them.

for (var i = 0; i < markers.length; i++) {
            showAddress(markers[i]);
        }
问题回答

In your showAddress function, you reference markers[i].

However, you don t pass in i... that variable is not in the scope of the function. So, you aren t iterating and adding, you are adding variables over and over to a non-existent place in the array.

You either need to pass in i or not encapsulate showAddress in a function.

How about making the function showAddresses and putting the loop in the function.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签