添加在Google Maps API v3中打开信息窗口的链接

时间:2013-11-15 17:16:16

标签: javascript google-maps-api-3

无法添加将在我的Google地图上居中并打开信息窗口的链接。标记及其信息窗口在地图本身内正常工作。

真正的问题是构建一个我的onclick函数可以正确引用的对象。我的面向对象的Javascript知识很不稳定,我只是没有看到解决方案。

地图是通过在页面加载时调用的函数加载的,我有一个单独的函数,在单击页面中的href时调用该函数。 代码如下。

function addMap(addressesJSON, id){
    var addresses = eval('(' + addressesJSON + ')');
    var cenLat = 41.677389;
    var cenLng = -72.384294;
    var latLow = 41.4;
    var lngLow = -72.8;
    var latHigh = 41.8;
    var lngHigh = -71.9;
    if (addresses.length){
        for (var i in addresses){
            addresses[i].lat = parseFloat(addresses[i].lat);
            addresses[i].lng = parseFloat(addresses[i].lng);
            if (i == 0){
                latLow = addresses[i].lat;
                latHigh = addresses[i].lat;
                lngLow = addresses[i].lng;
                lngHigh = addresses[i].lng;
            } else {
                if (addresses[i].lat < latLow){
                    latLow = addresses[i].lat;
                }
                if (addresses[i].lat > latHigh){
                    latHigh = addresses[i].lat;
                }
                if (addresses[i].lng < lngLow){
                    lngLow = addresses[i].lng;
                }
                if (addresses[i].lng > lngHigh){
                    lngHigh = addresses[i].lng;
                }
            }
            address = "<span style=\"color: #0000ff\">" + addresses[i].name + "</span><br/>" + addresses[i].address + "<br/>Directions: <a href=\"#\" onClick=\"GPopUp('http://maps.google.com/maps?daddr=" + addresses[i].address + "&f=li&hl=en&ie=UTF8&om=1')\">To</a> - <a href=\"#\" onClick=\"popUp('http://maps.google.com/maps?saddr=" + addresses[i].address + "&f=li&hl=en&ie=UTF8&om=1')\">From</a>";
            addresses[i].address = address;
        }
        cenLat = (latLow + latHigh) / 2;
        cenLng = (lngLow + lngHigh) / 2;
    }

    var mapOptions = {
        center: new google.maps.LatLng(cenLat, cenLng),
        zoom: 12,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    // Display a map on the page
    var map = new google.maps.Map(document.getElementById(id), mapOptions);
    var bounds = new google.maps.LatLngBounds();
    var infowindow =  new google.maps.InfoWindow();

    for (var i in addresses){
        var letter = addresses[i].letter;
        var data = addresses[i].address;
        var myLatlng = new google.maps.LatLng(addresses[i].lat, addresses[i].lng);

        var latlng = new google.maps.LatLng(addresses[i].lat, addresses[i].lng);
        bounds.extend(latlng);

        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: addresses[i].name,
            icon: "http://maps.google.com/mapfiles/marker" + letter + ".png"
        });

        (function (marker, data) {
            google.maps.event.addListener(marker, "click", function () {
                // Center on marker
                map.setCenter(marker.getPosition());
                // Set the data for the info window
                infowindow.setContent(data);
                // show the infowindow
                infowindow.open(map, marker);
            });
        })(marker, data);
    }
    map.fitBounds(bounds);
}

然后点击了我要打开相应信息窗口的函数。

function moveCenter(lat, lng, letter){
    google.maps.event.trigger(marker[letter], "click");
}

有关如何构建标记对象数组并让我的moveCenter函数看到它的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您似乎只需将此行添加到代码中:

markers[letter] = marker;

并更改您的功能:

function moveCenter(lat, lng, letter){
    google.maps.event.trigger(markers[letter], "click");
}

(确保markers数组在全局范围内)

markers = [];
function addMap(addressesJSON, id){
    var addresses = eval('(' + addressesJSON + ')');
    var cenLat = 41.677389;
    var cenLng = -72.384294;
    var latLow = 41.4;
    var lngLow = -72.8;
    var latHigh = 41.8;
    var lngHigh = -71.9;
    if (addresses.length){
        for (var i in addresses){
            addresses[i].lat = parseFloat(addresses[i].lat);
            addresses[i].lng = parseFloat(addresses[i].lng);
            if (i == 0){
                latLow = addresses[i].lat;
                latHigh = addresses[i].lat;
                lngLow = addresses[i].lng;
                lngHigh = addresses[i].lng;
            } else {
                if (addresses[i].lat < latLow){
                    latLow = addresses[i].lat;
                }
                if (addresses[i].lat > latHigh){
                    latHigh = addresses[i].lat;
                }
                if (addresses[i].lng < lngLow){
                    lngLow = addresses[i].lng;
                }
                if (addresses[i].lng > lngHigh){
                    lngHigh = addresses[i].lng;
                }
            }
            address = "<span style=\"color: #0000ff\">" + addresses[i].name + "</span><br/>" + addresses[i].address + "<br/>Directions: <a href=\"#\" onClick=\"GPopUp('http://maps.google.com/maps?daddr=" + addresses[i].address + "&f=li&hl=en&ie=UTF8&om=1')\">To</a> - <a href=\"#\" onClick=\"popUp('http://maps.google.com/maps?saddr=" + addresses[i].address + "&f=li&hl=en&ie=UTF8&om=1')\">From</a>";
            addresses[i].address = address;
        }
        cenLat = (latLow + latHigh) / 2;
        cenLng = (lngLow + lngHigh) / 2;
    }

    var mapOptions = {
        center: new google.maps.LatLng(cenLat, cenLng),
        zoom: 12,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    // Display a map on the page
    var map = new google.maps.Map(document.getElementById(id), mapOptions);
    var bounds = new google.maps.LatLngBounds();
    var infowindow =  new google.maps.InfoWindow();

    for (var i in addresses){
        var letter = addresses[i].letter;
        var data = addresses[i].address;
        var myLatlng = new google.maps.LatLng(addresses[i].lat, addresses[i].lng);

        var latlng = new google.maps.LatLng(addresses[i].lat, addresses[i].lng);
        bounds.extend(latlng);

        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: addresses[i].name,
            icon: "http://maps.google.com/mapfiles/marker" + letter + ".png"
        });
        markers[letter] = marker;

        (function (marker, data) {
            google.maps.event.addListener(marker, "click", function () {
                // Center on marker
                map.setCenter(marker.getPosition());
                // Set the data for the info window
                infowindow.setContent(data);
                // show the infowindow
                infowindow.open(map, marker);
            });
        })(marker, data);
    }
    map.fitBounds(bounds);
}