简体   繁体   English

Google Maps API v3

[英]Google Maps API v3

I am facing an issue with my google maps code. 我的Google Maps代码遇到问题。 I am trying to place markers on my map from an array. 我试图通过数组将标记放置在地图上。 But I am stuck in between when I am trying to do the same.My firebug console gives me an error that results is not defined in function createMarkers. 但是当我尝试执行相同操作时,我陷入了困境.firebug控制台给我一个错误,提示结果未在函数createMarkers中定义。 Here is my code: 这是我的代码:

    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
var addresses = new Array();
abc = document.getElementsByTagName('td');
//loc = mydiv.getAttribute("data-addr");
var l = abc.length; 
 for (var i=0; i < l; i++){
    if (abc[i].hasAttribute('name'))
    {   
        addresses.push("'"+abc[i].innerHTML+"'");
    }   
}
var len = addresses.length;
var geocoder;
var map;
var add = document.getElementById("addr").value;
window.onload = function init() {

      geocoder = new google.maps.Geocoder();    
      var add = document.getElementById("address").value;
      var latlng = codeAddress(add);
      var myOptions = {
        zoom: 10,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById("map_canvas"),
          myOptions);
}

//for (var i = 0; i < addresses.length; i++)
//{
    function codeAddress(add) 
    {
      //var addr = addresses[i];
      geocoder.geocode( { 'address':add }, function(results, status) {

          if (status == google.maps.GeocoderStatus.OK) {
             map.setCenter(results[0].geometry.location);     
          } else {
              alert("Geocode was not successful for the following reason: " + status);
          }
        });
    }

    function createMarkers()
    {
        for(var i = 0; i < len; i++){
            (function(addresses){
                geocoder.geocode( { 'address': addresses }, function(results) {
                    var marker = new google.maps.Marker ({
                        map: map,
                        position: results[0].geometry.location,//error:results[0] is undefined
                        title: address
                    }); 

                    google.maps.event.addListener(marker, 'click', function() {
                        alert(addresses);
                    });
                });
            })(addresses[i]);
        }
    }
    window.onload = createMarkers;
</script>

Well after a long battle with the code,I found the solution. 经过长时间与代码的斗争,我找到了解决方案。 The error I was facing because I was pushing the addresses into array in a wrong format ie I pushed the addresses into the array with a '(single quote) surrounding it,which the geocoder did not accept.So then finally edited the loc where I was pushing the address.The modified code is as : 我遇到的错误是因为我将地址以错误的格式推入数组,即我将地址推入到数组中并带有'(single quote) ,而地址解析器不接受。因此,最后编辑了我所在的位置正在推送地址。修改后的代码为:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
var addresses = new Array();
abc = document.getElementsByTagName('td');
//loc = mydiv.getAttribute("data-addr");
var l = abc.length; 
 for (var i=0; i < l; i++){
    if (abc[i].hasAttribute('name'))
    {   
        addresses.push(""+abc[i].innerHTML+""); //removed single quotes here. see previous code
    }   
}
var len = addresses.length;
var geocoder;
var map;
var add = document.getElementById("addr").value;
window.onload = function init() {

      geocoder = new google.maps.Geocoder();    
      var add = document.getElementById("address").value;
      var latlng = codeAddress(add);
      var myOptions = {
        zoom: 10,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById("map_canvas"),
          myOptions);
}

//for (var i = 0; i < addresses.length; i++)
//{
    function codeAddress(add) 
    {
      //var addr = addresses[i];
      geocoder.geocode( { 'address':add }, function(results, status) {

          if (status == google.maps.GeocoderStatus.OK) {
             map.setCenter(results[0].geometry.location);     
          } else {
              alert("Geocode was not successful for the following reason: " + status);
          }
        });
    }

    function createMarkers()
    {
        for(var i = 0; i < len; i++){
            (function(addresses){
                geocoder.geocode( { 'address': addresses }, function(results) {
                    var marker = new google.maps.Marker ({
                        map: map,
                        position: results[0].geometry.location,//error:results[0] is undefined
                        title: address
                    }); 

                    google.maps.event.addListener(marker, 'click', function() {
                        alert(addresses);
                    });
                });
            })(addresses[i]);
        }
    }
    window.onload = createMarkers;
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM