【问题标题】:How to display content in an infowindow specific to a marker?如何在特定于标记的信息窗口中显示内容?
【发布时间】:2012-03-08 23:06:16
【问题描述】:

我创建了一个谷歌地图(api v3),其标记数量取决于搜索结果。当我单击标记时,它会打开一个信息窗口。让该信息窗口显示与其标记相关联的信息的最佳方式是什么?与所有标记相关的信息都在我从 ajax 请求收到的 json 对象中。

for (i=0; i < result.point.length; i++) {
                    var latLng = new google.maps.LatLng(result.proint[i].Latitude,result.point[i].Longitude);                                    
                    var marker = new google.maps.Marker({
                        position: latLng,
                        title: i.toString()
                        //map: map
                    });
                    markersArray.push(marker);

                    var infowindow = new google.maps.InfoWindow({
                        content: 'specific information associated to the marker clicked'
                    });

                    google.maps.event.addListener(markersArray[i], 'click', function(event) {
                        infowindow.open(map, this);
                    });

【问题讨论】:

  • 链接到实时代码会很有价值。
  • 您想要多个信息窗口还是一个?您的代码现在的方式是为每个标记创建一个新的信息窗口,但是只会显示一个,即最后创建的一个。如果您的意图是只有一个,那么您应该将它的创建移出 for 循环。
  • 我同意更多代码会有所帮助,至少是 JSON 对象。
  • 感谢您指出这一点。是的,我的意图是只有一个。这是 json 对象: $arr = array(); foreach ($points as $row) { array_push($arr, array('Address' => $row->getAddress(),'Latitude' => $row->getLatitude(), 'Longitude' => $row- >getLongitude(),'PublicTransportation' => $row->getPublicTransportation(),'Name' => $row->getName(),'Description' => $row->getDescription() )); } die(json_encode(array('point' => $arr)));
  • 你发现了吗?任何答案有帮助吗?如果有帮助,请将答案标记为有用。

标签: google-maps-api-3 google-maps-markers


【解决方案1】:

按照上面的建议只创建 1 个信息窗口。

标记内的内容存储:

var marker = new google.maps.Marker({
                        position: latLng,
                        title: i.toString(),
                        //set your content here
                        content:'your specific content'
                        //map: map
                    });

开窗:

google.maps.event.addListener(markersArray[i], 'click', function(event) {
                        infoWindow.setContent(this.content);
                        infowindow.open(map, this);
                    });

【讨论】:

    【解决方案2】:

    首先,您应该将 infoWindow 的创建移出 for 循环。

    接下来,更改将点击事件附加到此的位置:

    google.maps.event.addListener(markersArray[i], 'click', function(content) {
        return function(event){
            infowindow.setContent(content);
            infowindow.open(map, this);
        }
    }(WHATEVER_THE_CONTENT_SHOULD_BE_FOR_THIS_MARKER));
    

    您想使用 this 而不是 ma​​rkerthis 将引用发生事件的对象,而 ma​​rker 将引用最后创建的标记。

    【讨论】:

    • 以下链接是别人发布的。这是我正在努力实现的完美示例。 updates.orbitz.com
    • 以上内容应该可以满足您的需求。如果没有,您可以提供更新的代码吗?此外,以 JavaScript 格式而不是 PHP 格式的 JSON 示例会更有帮助。
    • 它最终奏效了!合成器错误... Shane,傻瓜,非常感谢您的帮助。
    • 什么是语法错误?并将问题标记为已回答。
    【解决方案3】:

    你的例子中有一些错别字(proint)在你循环的顶部:

    for (i=0; i < result.point.length; i++) {
       var info_window_content = result.point[i].Latitude + '<br />';
       info_window_content += result.point[i].Longitue + '<br />';
       // etc for any value in you json object
       // create your marker as is.
         ...
      var infowindow = new google.maps.InfoWindow({
                        content: info_window_content
                    });
      // etc, etc.
    

    您确实需要为每个标记添加一个 eventListener,就像您正在做的那样。我看不出有什么问题。 除了我会使用 infoWindow.open(map, marker) vs. this。

    可能有一种更有效的方法来做到这一点,即在 infoWindow.open(); 之后infoWindow.setContent(info_window_content)

    【讨论】:

    • 谢谢埃里克!我首先使用了“this”,以便根据标记定位信息窗口。我实施了您的解决方案,但由于内容保持不变,因此可能做错了什么。寻找一种方法来加载与标记关联的内容。
    【解决方案4】:

    试试这个,效果很好,我已经测试过了

    // Add markers
    
       for (i=0; i < result.point.length; i++) {
                        var latLng = new google.maps.LatLng(result.proint[i].Latitude, result.point[i].Longitude);                                    
                        var marker = new google.maps.Marker({
                            position: latLng,
                            title: i.toString()
                            //map: map
                        });                    
    
        // marker info start
                        var infowindow = new google.maps.InfoWindow();
    
        (function (marker, result.point[i]) {
                        // add click event
                        google.maps.event.addListener(marker,  'click',  function() {
                        var infoContent: "specific information associated to the marker clicked"
                        });
            infoWindow.setContent(infoContent);
                            infowindow.open(map, marker);
                        });
    
        // selected marker 4 infowindow 
        (marker, result.point[i]);
    
           markersArray.push(marker);
          }
    

    【讨论】:

      【解决方案5】:

      不完全确定您要做什么。您要在哪里/要加载什么内容?

      google.maps.event.addListener(marker, 'click', (function(event, index) {
        return function(){
        infowindow.content = markersArray[index].yourcontent;
        // or
        infowindow.content = yourcontentarray[index];
        infowindow.open(map,this);
        }
      })(marker,i));
      

      确保在函数之外声明标记和信息窗口变量。

      【讨论】:

      • 这段代码有几个缺陷。首先,当 event 应该是返回函数的参数时,它是 IIFE 的参数。这里的事件实际上等于传入的标记,而不是事件。标记甚至不需要传入。接下来,您应该使用 infowindow 的 setContent 方法,而不是直接设置 content 属性。虽然设置该属性会起作用,但它绕过了 MVCObject 的 KVO 优势。比如使用setContent会触发infowindow的content_changed事件,直接设置属性不会。
      猜你喜欢
      • 2016-07-27
      • 2013-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      • 2013-09-18
      • 2014-10-15
      • 2013-11-26
      相关资源
      最近更新 更多