如何使infowindow和setzoom适用于Google Maps API v3

时间:2013-11-28 18:53:17

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

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

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

但缩放功能无法正常工作。更改为此后;

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

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

<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>

2 个答案:

答案 0 :(得分:3)

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

修复它的2个选项:

  • 使用原始代码:

    // 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>';
    }
    

答案 1 :(得分:0)

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

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

disableAutoPan - infowindow选项设置为 true 以禁用自动平移,它应该按预期工作< / p>