【问题标题】:Google maps individual marker infowindow - how to?谷歌地图个人标记信息窗口 - 如何?
【发布时间】:2015-10-03 11:28:55
【问题描述】:

我有这个工作的多标记脚本。标记打印在地图上,但是当我点击任何标记时,我可以看到相同的信息窗口。这个循环有问题:

    function bindInfoWindow(marker, map, infowindow, content) {
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(content);
        infowindow.open(map, marker);
    });
}
function initialize() {
var map;
var locations = <?php echo json_encode($location); ?>;

var bounds = new google.maps.LatLngBounds();
var mapOptions = {
    mapTypeId: 'roadmap'
};
// Display a map on the page
map = new google.maps.Map(document.getElementById("googlemap"), mapOptions);

// Multiple Markers
var markers = locations;        
// Display multiple markers on a map
var infoWindow = new google.maps.InfoWindow();

// Loop through our array of markers & place each one on the map 
for( i = 0; i < markers.length; i++ ){
loc_array = markers[i].split(",");
    var position = new google.maps.LatLng(loc_array[1], loc_array[2]);
    bounds.extend(position);
    marker = new google.maps.Marker({
        position: position,
        map: map,
        draggable: false,
        raiseOnDrag: true,
        title: loc_array[0]
    });
    // Info Window Content
    content=loc_array[0] + " - <a class='ac' onclick='move("+ loc_array[4] +");' href='#'>" + <?php echo json_encode($lang['view_profile']);?> + "</a><span class='p'>" + <?php echo json_encode($lang['phone']);?> + ": " + loc_array[6] + " </span><span class='p'>" + <?php echo json_encode($lang['address']);?> + ": " + loc_array[5] + ", " + loc_array[7] + ", " + loc_array[8] + "</span>";
    bindInfoWindow(marker, map, infoWindow, content);

    //infowindow = new google.maps.InfoWindow();
    console.log(content);
    // Allow each marker to have an info window    
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infoWindow.setContent(content);
            infoWindow.open(map, marker);
        }
    })(marker, i));

    // Automatically center the map fitting all markers on the screen
    map.fitBounds(bounds);
}
bounds.extend(marker.position); 
// Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
   map.fitBounds(bounds);
    google.maps.event.removeListener(boundsListener);
});
}

当我在控制台中打印个人信息时,我可以看到不同的信息窗口,但在地图上都是一样的:

console.log(content);

我在身体负载时调用 initialize() 请帮助我哪里错了,谢谢!

【问题讨论】:

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


【解决方案1】:

首先通过以下方式全局声明infowindow

var infoWindow = new google.maps.InfoWindow();

然后在创建标记和内容字符串后调用这个函数:

bindInfoWindow(marker, map, infoWindow, content);

这是函数bindInfoWindow()的定义

function bindInfoWindow(marker, map, infowindow, content) {
        google.maps.event.addListener(marker, 'click', function() {
            infowindow.setContent(content);
            infowindow.open(map, marker);
        });
    }

【讨论】:

  • #sunil,谢谢,我用你的代码更新了代码,但没有任何变化。请查看我在现场编辑的完整代码,包括您的更改。所有标记的弹出信息窗口仍然相同。请检查我更新的代码,谢谢
  • 你没有在调用 bindInfoWindow() 函数后删除你的代码,..Remove this code // 允许每个标记有一个信息窗口 google.maps.event.addListener(marker, 'click' , (function(marker, i) { return function() { infoWindow.setContent(content); infoWindow.open(map, marker); } })(marker, i));
  • 哦,是的 :) 现在它正在运行 Sunil。非常感谢您的帮助!
猜你喜欢
  • 2011-08-21
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多