【问题标题】:Generating Map with multiple markers using JSON + PHP + Google Maps API V2使用 JSON + PHP + Google Maps API V2 生成具有多个标记的地图
【发布时间】:2012-06-29 21:13:42
【问题描述】:

我正在使用 Google Maps API V2(我知道,但我正在编辑当前代码,因此必须使用 V2)。我所有的标记都指向了正确的位置,但只有一个问题让我差点扯掉我的头发。

问题是所有标记只有一个正确显示,但信息窗口只有一个内容。即信息窗口没有被更新。它们会正确弹出,但它们中的所有信息都相同。

我在其他一些论坛上看到过这个问题,并尝试了解决方案,但无济于事。

以下是从我的 php 脚本中获取 json 编码的 PHP 字符串然后循环它以在谷歌地图上显示标记的代码。

function showAddress(address) {

var response=$.ajax({
      type: "POST",
      url: "<?php echo WEBROOT; ?>/index/ajaxMaps",
      data: {address : escape(address)},
      cache: false,
      async:false,
      dataType: "json",
      success: function (html) {           

          for(i in html){

              if (geocoder) {
                  geocoder.getLatLng(
                    unescape(html[i].businessAddress),
                    function(point) {
                      if(!point) {

                        //alert(address + " not found");

                      } else {

                        map.setCenter(point, 11);

                          string='<div style="height:100px; background:green; color:white; padding:5px; border-radius:0.5em; width:auto"><strong>Company name : </strong>'+ html[i].businessName;
                          string+='<br/><strong>Adress : </strong>'+ html[i].businessAddress;
                          string+='<br/><strong>Website : </strong>'+'<a style="color:white !important" href="'+html[i].businessWebsite+'" >'+'Visit our website'+'</a></div>';

                        var marker = createMarker(point, string);

                        map.addOverlay(marker);

                        //map.addMapType(G_PHYSICAL_MAP);

                        map.addControl(new GLargeMapControl());

                        // As this is user-generated content, we display it as
                        // text rather than HTML to reduce XSS vulnerabilities.

                        marker.openInfoWindowHtml(string);

                        //$('.map_enlarged').trigger('click');
                      }
                    }
                  );
                }

            }           

      }

    });


 $('.map_enlarged').trigger('click');

}

现在添加标记的功能:

function createMarker(point,html) {

    // Create our "tiny" marker icon
   var blueIcon = new GIcon(G_DEFAULT_ICON);
   blueIcon.image = "<?php echo WEBROOT; ?>images/marker.png";

    // Set up our GMarkerOptions object
    markerOptions = { icon:blueIcon };

    var marker = new GMarker(point,markerOptions);

    GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);

  });

  return marker;
}

我知道变量名“string”没有更新。但我不明白为什么。我已将其追溯到循环,但看不出它不应该更新的原因。

请记住,我已经挖掘了足够多的上述代码,如果可以的话,请指出我的代码中的错误。提供其他链接可能没有任何用处,因为我可能已经看过它们了。

我将不胜感激。

【问题讨论】:

  • 您是否正在检查浏览器控制台是否有错误?
  • @LawrenceCherone 不,控制台中没有错误。另外,我已经告诉过问题在于 geocoder.getLatLng 函数的回调。

标签: php ajax json google-maps


【解决方案1】:

您需要在“i”上使用另一个函数闭包(关闭地理编码器的输入参数)。

example 解决了这个问题,但没有解决查询限制问题(你真的不应该对大量地址进行即时地理编码,离线地理编码并存储结果坐标)。

【讨论】:

  • 你能告诉我如何在“i”上使用闭包吗?我有点困惑。你提到的代码正是我所做的。
  • Here 是 Mike Williams 在他的 google maps API v2 教程中对函数闭包的解释。您在 showAddress 函数中有一个循环。我提供的示例在 showAddress 函数之外有循环(因此它可以关闭地址)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-02
  • 2013-03-29
  • 2023-03-25
  • 2013-03-30
  • 2013-03-14
相关资源
最近更新 更多