2010-11-01 128 views
-1

在谷歌地图上显示多个标记我有以下脚本来使用谷歌地图API,我必须创建一个具有多个标记(气球形状的图标指向某个东西)的地图,我需要每个那些标记指向地图的不同区域(即不同的坐标),我该怎么做? 注意:我没有经纬度来放置标记的区域,而是具有放置标记的区域的地址。通过提供地址

下面给出了在Google地图上显示多个标记的代码,但它不能按预期工作!

var map = null; 
var geocoder = null; 

function initializeNew() { 
    var allAddress = document.getElementById("HiddenField1").value; 
    var testary = new Array(); 
    testary = allAddress.split("|"); 

     if (GBrowserIsCompatible()) { 
     //calls map to appear inside <div> with id, "map_canvas" 
     map = new GMap2(document.getElementById("map_canvas")); 

     //adds zoom and arrow controls to map 
     //map.addControl(new GSmallMapControl()); 

     var marker = null; 
     var i = 0; 
     for (i = 0; i < testary.length; i++) { 

      address = testary[i]; 

      geocoder = new GClientGeocoder(); 


      //converts a street address into geocode for the api 
      geocoder.getLatLng(
      address, 
      function(point) { 
       if (!point) { 
        alert(address + " not found"); 
       } else { 
        map.setCenter(point, 13); 
        marker = new GMarker(point); 
        map.addOverlay(marker); 

        map.setMapType(G_HYBRID_MAP); 

        //adds your own marker title and description in html 
        marker.openInfoWindowHtml("<p class='para1bold' style=\"font-weight: bold;\">Address</p><p class='para1'>" + address + "</p>"); 
        //Add click event on push pin to open info window on click of the icon 
        GEvent.addListener(marker, "click", function() { 
        marker.openInfoWindowHtml("<p class='para1bold' style=\"font-weight: bold;\">Address</p><p class='para1'>" + address + "</p>"); 
       }); 
        //Provides map,satellite,hybrid and terrain views in the map along with zoom control 
        map.setUIToDefault(); 
       } 
      } 
    ) 
     } 


    } 
} 

我已经将一组项目的地址存储在一个数组中,并循环显示地图中的标记。这仅显示数组中最后一个地址的一个标记。请更正此代码或提供新代码,以通过提供地址在Google地图中显示多个标记。

+0

您是否尝试打印出地理编码的结果?所有不同的经纬度? – jebberwocky 2010-11-01 10:14:51

+0

当我在geocoder.getLatLng方法的else部分中输出地址变量(数组中的项)时,它显示数组中的最后一项(最后一个地址)。我认为它在Google地图中的相同位置显示多个标记!任何人都可以指导我纠正代码! – banupriya 2010-11-01 10:45:31

+0

也许您提供给Google Geocoder webervice的地址太相似了,这可能会导致相同的经过长时间的结果。 – Vishwanath 2010-11-13 14:13:27

回答

0

尝试这个办法:把var在前面标记

else { 
       map.setCenter(point, 13); 
       var marker = new GMarker(point); 
       map.addOverlay(marker); 

       map.setMapType(G_HYBRID_MAP); 

       //adds your own marker title and description in html 
       marker.openInfoWindowHtml("<p class='para1bold' style=\"font-weight: bold;\">Address</p><p class='para1'>" + address + "</p>"); 
       //Add click event on push pin to open info window on click of the icon 
       GEvent.addListener(marker, "click", function() { 
       marker.openInfoWindowHtml("<p class='para1bold' style=\"font-weight: bold;\">Address</p><p class='para1'>" + address + "</p>"); 
      }); 

UPDATE

var geocoder = new GClientGeocoder(); 

var addresses = ["new york","chicago"]; 
var curIndex = 0; 

function showAddress() { 
    var _cur = addresses[curIndex]; 
    geocoder.getLatLng(
    _cur, 
    function(point) { 
     if (!point) { 
     alert(_cur + " not found"); 
     } else { 
     console.log(_cur+":"+point); 
     } 
     //do next after done 
     curIndex++; 
     if(curIndex<addresses.length) 
     showAddress(); 
    } 
); 
} 

showAddress(); 
</script> 
+0

抱歉还是没变!它没有解决! – banupriya 2010-11-01 10:03:28

+0

您是否删除了“var marker = null;” ? – jebberwocky 2010-11-01 10:09:04

+0

是的,我删除了var marker = null – banupriya 2010-11-01 10:14:20

0

我就遇到了这个确切的同样的问题,fixed it的。看看我用过的代码。具体位是那个标记需要是一个数组,当你打开infowindow时,内容需要存储在标记数组中。我还建议你使用JSON代替,而不是用'|'分隔,这样更容易解析。