Javascript Google Maps API3 多边形多个圆圈

发布于 2024-12-09 06:12:59 字数 1153 浏览 0 评论 0原文

我正在使用 Mysql 和 PHP 生成供 Google 地图使用的 XML。我正在设置标记和多边形圆圈。标记设置没有问题,但是只显示一个多边形圆,而不显示其他九个(从数据库总共返回十个) 我很确定这是一个数组问题,但是我似乎无法解释为什么只创建一个多边形。希望有人能让我指出正确的方向,这是我的代码:

   //function to consume xml
   var xml = parseXml(data); 
   var polygonNodes = xml.documentElement.getElementsByTagName("polygon"); 
   for (var i = 0; i < polygonNodes.length; i++) {
   var polylatlng = new google.maps.LatLng( 
          parseFloat(polygonNodes[i].getAttribute("polygon_lat")), 
          parseFloat(polygonNodes[i].getAttribute("polygon_lng"))); 
   }
   createCircle(polylatlng);

  //here i am creating the polygon circle
  function createCircle(polylatlng) { 
  var html = "<div id='infodiv'>HelloWorld</div>"; 
    var circle = new google.maps.Circle({
        map: map,
        center: polylatlng,
        fillColor: '#7491D3',
        fillOpacity: 0.03,
        strokeColor: '#7491D3',
        strokeOpacity: 0.1,
        strokeWeight: 1,
        radius: 18362.55489862987
        }); 
  google.maps.event.addListener(circle, 'click', function() { 
    infoWindow.setContent(html); 
    infoWindow.open(map, circle); 
  }); 
}

I am using Mysql and PHP to generate XML which is consumed by a Google Map. I am setting markers and polygon circles. The markers set with no problems, however only one polygon circle is displayed, not the other nine (a total of ten is returned from the database)
I am quite certain this is an array issue, however I just cant seem to put my finger on why only one polygon is created. Hopeful someone can get me pointed in the right direction, here is my code:

   //function to consume xml
   var xml = parseXml(data); 
   var polygonNodes = xml.documentElement.getElementsByTagName("polygon"); 
   for (var i = 0; i < polygonNodes.length; i++) {
   var polylatlng = new google.maps.LatLng( 
          parseFloat(polygonNodes[i].getAttribute("polygon_lat")), 
          parseFloat(polygonNodes[i].getAttribute("polygon_lng"))); 
   }
   createCircle(polylatlng);

  //here i am creating the polygon circle
  function createCircle(polylatlng) { 
  var html = "<div id='infodiv'>HelloWorld</div>"; 
    var circle = new google.maps.Circle({
        map: map,
        center: polylatlng,
        fillColor: '#7491D3',
        fillOpacity: 0.03,
        strokeColor: '#7491D3',
        strokeOpacity: 0.1,
        strokeWeight: 1,
        radius: 18362.55489862987
        }); 
  google.maps.event.addListener(circle, 'click', function() { 
    infoWindow.setContent(html); 
    infoWindow.open(map, circle); 
  }); 
}

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

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

发布评论

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

评论(1

转身以后 2024-12-16 06:12:59

除非我读错了,否则您不会在循环中包含 createCircle 函数,因此它只会被调用一次。尝试:

for (var i = 0; i < polygonNodes.length; i++) {
    var polylatlng = new google.maps.LatLng( 
          parseFloat(polygonNodes[i].getAttribute("polygon_lat")), 
          parseFloat(polygonNodes[i].getAttribute("polygon_lng"))
    ); 
    createCircle(polylatlng);
}

良好的代码缩进对于解决此类问题很有帮助:)。

Unless I'm reading this wrong, you're not including the createCircle function in your loop, so it only gets called once. Try:

for (var i = 0; i < polygonNodes.length; i++) {
    var polylatlng = new google.maps.LatLng( 
          parseFloat(polygonNodes[i].getAttribute("polygon_lat")), 
          parseFloat(polygonNodes[i].getAttribute("polygon_lng"))
    ); 
    createCircle(polylatlng);
}

Good code indentation is helpful for issues like this :).

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