【问题标题】:Google maps marker - infowindow谷歌地图标记 - 信息窗口
【发布时间】:2016-01-11 06:41:04
【问题描述】:

我需要在 InfoWindow 中显示 match.offer.text。 每个标记都有不同的 match.offer.text。

这是我的代码:

var markerImageURL, lat, lng;
if (myoffer) {
    markerImageURL = 'assets/img/markers/marker_my.png';
    lat = match.lat;
    lng = match.lng;
} else {
    markerImageURL = 'assets/img/markers/marker_' + match.strength + '.png';
    lat = match.offer.lat;
    lng = match.offer.lng;
}

var marker = new google.maps.Marker({
    position: new google.maps.LatLng(lat, lng),
    map: window.googleMap,
    icon: {
        size: new google.maps.Size(54,56),
        url: markerImageURL
    },
    draggable: false,
    visible: true
});

var infowindow = new google.maps.InfoWindow({
    content: match.offer.text,
    maxWidth: 300
});

window.googleMapMarkers.push(marker);

if(!myoffer) {
    window.MVC.Events.GoogleMap.prototype.showInfoWindow(marker, infowindow, match);
}

点击标记后触发的事件:

marker.addListener('click', function() {
    infowindow.open(window.googleMap, marker);
}

请帮帮我。

【问题讨论】:

  • 您的开发者工具控制台中是否出现任何错误?你的代码目前在做什么?
  • 请提供一个Minimal, Complete, Tested and Readable example 来证明您的问题。
  • Jaromanda X:当我给内容一个字符串时,它会在任何地方显示相同的文本 content: "text", 当内容是控制台中的“match.offer.text”时,我有:Potentially unhandled rejection [1] window.MVC.Events.GoogleMap.prototype.addGoogleMapMarker@file:///D:/Zlecenia/...

标签: javascript google-maps infowindow


【解决方案1】:

内容将应用于 Open 上的标记,因此您的代码会将循环中最后一项中的内容应用于所有标记,在您的 openWindow 函数中将内容添加到单个 infoWindow 对象,即

Maps API 也有自己的点击事件事件包装器

function initMarkers(){
   //create markers
   var marker = new google.maps.Marker();

   google.maps.event.addListener(marker, 'click', openInfoWindow(marker, i));
}

var infowindow = new google.maps.InfoWindow();
function openInfoWindow(marker,index){
         return function(e) {

            //Close other info window if one is open
            if (infowindow) {
                infowindow.close();
            }

            var content = marker.offer.text;

            infowindow.setContent(content);

            setTimeout(function() {
                infowindow.open(map, marker);
            }, 200);
        }
}

【讨论】:

    猜你喜欢
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    相关资源
    最近更新 更多