0

我有一张带有两个标记的地图,一个距离很远,为了不缩放太远,我需要为全尺寸的地图制作一个页面,我想在地图之间动态更改,这样用户就不必切换到一个额外的窗口以查看其他位置。这是我想如何做到这一点的图像。我非常感谢所有人的帮助和经验。

这是js代码。

(function() {

window.onload = function() {

    // Creating a new map
    var map = new google.maps.Map(document.getElementById("map"), {
      center: new google.maps.LatLng(10.2000, -84.4000),
      disableDefaultUI: false,
      scrollwheel: false,
      zoom: 8,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });


    // Creating the JSON data
    var json = [
        {
            "title": "map 1",
            "lat": 9.93379260367826,
            "lng": -84.22572178326419,
            "description": "<br><br><strong>map 1</strong>Description<br><br>"
        },
        {
            "title": "map 2",
            "lat": 10.575049399803566,
            "lng": -85.58282425643927,
            "description": "<br><br><strong>map 2</strong>Description<br><br>"
        },
    ]

    // Creating a global infoWindow object that will be reused by all markers
    var infoWindow = new google.maps.InfoWindow();

    // Looping through the JSON data
    for (var i = 0, length = json.length; i < length; i++) {
        var data = json[i],
            latLng = new google.maps.LatLng(data.lat, data.lng);

        // Creating a marker and putting it on the map
        var marker = new google.maps.Marker({
            position: latLng,
            map: map,
            title: data.title,
            icon: 'style/images/mapMarker.png'
        });



        // Creating a closure to retain the correct data, notice how I pass the current data in the loop into the closure (marker, data)
        (function(marker, data) {

            // Attaching a click event to the current marker
            google.maps.event.addListener(marker, "click", function(e) {
                infoWindow.setContent(data.description);
                infoWindow.open(map, marker);
            });


        })(marker, data);

    }

}
    })();

单击时,两个全窗口大小的地图会相互更改。

4

1 回答 1

0

有两个选项卡或链接,当单击它们时调用一个 JavaScript 函数,该函数只需map.setCenter();根据需要处理标记 1 或标记 2 的坐标。

于 2012-10-22T08:15:52.220 回答