【问题标题】:How to make both infowindow and setzoom working for Google map API v3如何使 infowindow 和 setzoom 都适用于 Google map API v3
【发布时间】:2013-12-14 21:40:08
【问题描述】:

首先我应该说我不太了解 javascript 或 Google API V3。无论如何,我一直在尝试包含一个带有标记的谷歌地图,以指示我使用 infowindow 访问过的地方。我在互联网上找到了一个示例,并将其复制到我的页面。然后我尝试添加一个功能以在单击标记时放大到该位置。更改代码后,我设法使缩放工作非常好。但是,信息窗口不会打开。在我使用它时打开之前;

infowindow.setContent(contentString); 
infowindow.open(map,marker);. 

但缩放无法正常工作。改成这个后;

infowindow.setContent(this.html);
infowindow.open(map, this);

缩放效果很好。但是我丢失了信息窗口。谁能看到我如何更改代码以使缩放和信息窗口都工作。如果有人可以帮助我,我会很高兴。我可以使用下面的代码进行调整,还是需要完全重写?提前致谢

<script>
//<![CDATA[
// this variable will collect the html which will eventually be placed in the    side_bar 
var side_bar_html = "";

// arrays to hold copies of the markers and html used by the side_bar 
// because the function closure trick doesnt work there 
var gmarkers = [];
var map = null;

function initialize() {
    // create the map
    var myOptions = {
        zoom: 7,
        center: new google.maps.LatLng(47.516231, 13.550072),
        mapTypeControl: true,
        mapTypeControlOptions: {
            style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
        },
        navigationControl: true,
        mapTypeId: google.maps.MapTypeId.TERRAIN
    }
    map = new google.maps.Map(document.getElementById("map_canvas"),
    myOptions);

    google.maps.event.addListener(map, 'click', function () {
        infowindow.close();
    });

    // Add markers to the map
    // add the points 

    var point = new google.maps.LatLng(48.208174, 16.373819);
    var marker = createMarker(point, "Vienna", '<div style="font-size:12px;font-weight:   bold;font-family:segoe ui, Trebuchet MS;">Vienna</div>')

    // put the assembled side_bar_html contents into the side_bar div
    document.getElementById("side_bar").innerHTML = side_bar_html;
}

var infowindow = new google.maps.InfoWindow({
    size: new google.maps.Size(150, 100)
});

// This function picks up the click and opens the corresponding info window
function myclick(i) {
    google.maps.event.trigger(gmarkers[i], "click");
}

// A function to create the marker and set up the event window function 
function createMarker(latlng, name, html) {
    var contentString = html;
    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        zIndex: Math.round(latlng.lat() * -100000) << 5
    });

    google.maps.event.addListener(marker, 'click', function () {
        infowindow.setContent(this.html);
        infowindow.open(map, this);
        map.setZoom(10);
        map.setCenter(marker.getPosition());
    });

    // save the info we need to use later for the side_bar
    gmarkers.push(marker);
    // add a line to the side_bar html
    side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length - 1) + ')">' + name + '<\/a><br>';
}
</script>

【问题讨论】:

  • 为什么把(map, marker)改成(map, this)?认为你应该只使用标记。
  • @putvande - 你为什么这么认为?他实际上应该使用this
  • 答:map.setCenter(this.getPosition()); map.setZoom(10); ..

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


【解决方案1】:

默认情况下,API 将平移地图,以便信息窗口完全可见(如果可能)。

这将通过使用动画来完成,当您设置缩放+中心时,此动画仍在运行,并将覆盖您的设置。

infowindowdisableAutoPan-选项设置为true以禁用自动平移,它应该可以按预期工作

【讨论】:

  • 下班回家,看到对我问题的答复让我很兴奋 :) 谢谢!!老实说,我认为我现在就可以做到这一点。但是,在尝试了“davidkonrad”和“Dr. Molle”的建议后,我仍然没有打开信息窗口。回答“putvande”;是的,缩放 + 信息窗口(几乎)有效。问题通常是它没有放大到点击标记的位置,而是另一个远离标记位置的位置
【解决方案2】:

主要问题是您将代码更改为使用标记的 .html 属性(google.maps.Marker 对象没有 .html 属性,但您可以添加它们)。

修复它的两个选项:

  • 使用您的原始代码:

    // A function to create the marker and set up the event window function 
    function createMarker(latlng, name, html) {
        var contentString = html;
        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            zIndex: Math.round(latlng.lat() * -100000) << 5
        });
    
        google.maps.event.addListener(marker, 'click', function () {
            infowindow.setContent(contentString); 
            infowindow.open(map,marker);
            map.setZoom(10);
            map.setCenter(marker.getPosition());
       });
    
       // save the info we need to use later for the side_bar
       gmarkers.push(marker);
       // add a line to the side_bar html
       side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length - 1) + ')">' + name + '<\/a><br>';
    }
    

working example

  • 将 .html 添加到 google.maps.Marker 并使用this

    // A function to create the marker and set up the event window function 
    function createMarker(latlng, name, html) {
        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            html: html,  // <---------------------------------------------- add this to the Marker
            zIndex: Math.round(latlng.lat() * -100000) << 5
        });
    
        google.maps.event.addListener(marker, 'click', function () {
            infowindow.setContent(this.html); 
            infowindow.open(map,this);
            map.setZoom(10);
            map.setCenter(this.getPosition());
       });
    
       // save the info we need to use later for the side_bar
       gmarkers.push(marker);
       // add a line to the side_bar html
       side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length - 1) + ')">' + name + '<\/a><br>';
    }
    

【讨论】:

  • 是的!那解决了它。它现在正在工作。谢谢“geocodezip”。非常感谢
猜你喜欢
  • 1970-01-01
  • 2015-04-04
  • 1970-01-01
  • 2012-02-18
  • 2011-11-06
  • 2012-08-19
  • 2016-02-10
  • 2011-05-30
  • 1970-01-01
相关资源
最近更新 更多