通过提供地址在谷歌地图上显示多个标记

发布于 2024-09-29 22:07:22 字数 2145 浏览 3 评论 0原文

我有以下脚本来使用谷歌地图 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 地图中显示多个标记。

I have the following script to use the google maps API, I have to create a map that has more than one Marker (the balloon shaped icon pointing to something) and i need each of those markers to point on a different area of the map (i.e. different coordinates), how can i do it?
NOTE: I do not have latitude and longitude for the area on which marker has to be placed instead I have the address of the area on which marker has to be placed.

The code to display multiple markers on Google map is given below but it does not work as expected!

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();
              }
          }
    )
      }


  }  
}

I have stored address of group of items in an array and looped to display markers in the map. This displays only one marker for the last address present in the array. Please correct this code or provide new code to display multiple markers in Google map by providing address.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久随 2024-10-06 22:07:22

我遇到了完全相同的问题,并修复了该问题 。看看我用过的代码。具体来说,标记需要是一个数组,当您打开信息窗口时,内容需要存储在标记数组中。我还建议您使用 JSON 来代替,它更容易解析,而不是用“|”分隔。

I ran into this exact same problem and fixed it. Look at the code that I have used. The specific bits are that marker needs to be an array and when you open the infowindow the content needs to be stored in the marker array. I would also suggest that you use something JSON instead which is easier to parse instead of separating with '|'.

撧情箌佬 2024-10-06 22:07:22

前面

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>");
          });

试试这个:将 var 放在标记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>

try this: put var in front of marker

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文