【问题标题】:Google Maps custom infobox谷歌地图自定义信息框
【发布时间】:2013-02-16 09:12:24
【问题描述】:

我正在尝试按照此示例合并一个自定义信息框,但我的代码不起作用。有人可以看看我哪里出错了吗?

我已经评论了示例代码的开始/结束位置以及我尝试调用它的位置。

function initialize() {

var mapOptions = {
  zoom: 12,
  center: new google.maps.LatLng(52.204872,0.120163),
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  styles: styles,
  scrollwheel: false
};

var map = new google.maps.Map(document.getElementById('map_canvas'),mapOptions);

setMarkers(map, sites);
            infowindow = new google.maps.InfoWindow({
    content: "loading..."

});
}

var sites = [
    // List all locations for each pin
['The Frontroom', 52.202977,0.138938, 1, '<p>The Frontroom. <br/>23-25 Gwydir Street, Cambridge, CB1 2LG <br/>01223 305 600</p>'],
];

function setMarkers(map, markers) {

for (var i = 0; i < markers.length; i++) {
    var sites = markers[i];
    var siteLatLng = new google.maps.LatLng(sites[1], sites[2]);
    var marker = new google.maps.Marker({
        position: siteLatLng,
        map: map,
        title: sites[0],
        zIndex: sites[3],
        html: sites[4],
        icon: "http://visualartscambridge.org/wp-content/uploads/2013/03/map-pin.png"
    });
    // Begin example code to get custom infobox
    var boxText = document.createElement("div");
    boxText.style.cssText = "border: 1px solid black; margin-top: 8px; background: yellow; padding: 5px;";

    var myOptions = {
             content: boxText
            ,disableAutoPan: false
            ,maxWidth: 0
            ,pixelOffset: new google.maps.Size(-140, 0)
            ,zIndex: null
            ,boxStyle: { 
              background: "url('tipbox.gif') no-repeat"
              ,opacity: 0.75
              ,width: "280px"
             }
            ,closeBoxMargin: "10px 2px 2px 2px"
            ,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
            ,infoBoxClearance: new google.maps.Size(1, 1)
            ,isHidden: false
            ,pane: "floatPane"
            ,enableEventPropagation: false
    };
    // end example code for custom infobox

    google.maps.event.addListener(marker, "click", function () {

        infowindow.setContent(this.html);
        // Call myOptions when marker is clicked and opened
        infowindow.open(map, myOptions, this);
    });
}
}

google.maps.event.addDomListener(window, 'load', initialize);

【问题讨论】:

  • 控制台窗口的输出是什么?
  • 页面加载没有错误,当我单击一个图钉时,我收到以下错误:'Uncaught TypeError: Object # has no method 'get''
  • 也许有指向特定页面的链接?
  • 是的,visualartscambridge.org/home - 地图在底部
  • 只是一个疯狂的猜测,但您可以尝试在顶部而不是在您的 initialize 函数中声明您的 map 变量吗?

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


【解决方案1】:

同时删除这部分现有代码:

google.maps.event.addListener(marker, "click", function () {

    infowindow.setContent(this.html);
    // Call myOptions when marker is clicked and opened
    infowindow.open(map, myOptions, this);
});

用 InfoBox 示例中的这个替换它:

var ib = new InfoBox(myOptions);

google.maps.event.addListener(marker, "click", function (e) {
  ib.open(map, this);  // change the map variable appropriately
});

working example

对于多个点,使用函数闭包(一个createMarker函数)来维护标记和信息框之间的关联:

function createMarker(site, map){
    var siteLatLng = new google.maps.LatLng(site[1], site[2]);
    var marker = new google.maps.Marker({
        position: siteLatLng,
        map: map,
        title: site[0],
        zIndex: site[3],
        html: site[4] /* ,
        icon: "http://visualartscambridge.org/wp-content/uploads/2013/03/map-pin.png" this icon no longer available */
    });
    // Begin example code to get custom infobox
    var boxText = document.createElement("div");
    boxText.style.cssText = "border: 1px solid black; margin-top: 8px; background: yellow; padding: 5px;";
    boxText.innerHTML = marker.html;

    var myOptions = {
             content: boxText
            ,disableAutoPan: false
            ,maxWidth: 0
            ,pixelOffset: new google.maps.Size(-140, 0)
            ,zIndex: null
            ,boxStyle: { 
              background: "url('http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.12/examples/tipbox.gif') no-repeat"
              ,opacity: 0.75
              ,width: "280px"
             }
            ,closeBoxMargin: "10px 2px 2px 2px"
            ,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
            ,infoBoxClearance: new google.maps.Size(1, 1)
            ,isHidden: false
            ,pane: "floatPane"
            ,enableEventPropagation: false
    };
    // end example code for custom infobox

    google.maps.event.addListener(marker, "click", function (e) {
     ib.close();
     ib.setOptions(myOptions);
     ib.open(map, this);
    });
    return marker;
}

function setMarkers(map, markers) {

 for (var i = 0; i < markers.length; i++) {
   createMarker(markers[i], map);
 }
}

working example with all the points from the live example

【讨论】:

【解决方案2】:

请确保 this 是否具有属性 html。因为它指的是当时的标记。

google.maps.event.addListener(marker, "click", function () 
{

    infowindow.setContent(this.html);
    // Call myOptions when marker is clicked and opened
    infowindow.open(map, myOptions, this);
});

同时尝试限制你的全局变量。请尝试此Fiddle 一次。

【讨论】:

  • 你说得对,我做了那个改变。非常感谢您指出这一点。
【解决方案3】:

好吧,比从http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.9/ 获取 inbox.js 文件包含在您的 html/jsp 中。您也需要从代码中删除 infowindow 对象

并使用

修改您的代码
function setMarkers(map, markers) {
  ......// same as your code
  var ib = new InfoBox(myOptions);
  google.maps.event.addListener(marker, "click", function (e) {
        ib.open(map, this);
    });
  .....//same as your code
}

如果您第一次没有成功,那么我们可能需要尝试几次。请每次发布您修改后的代码和结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多