1

使用 Casey P Thomas 的解决方案在单个位置处理多个标记:

示例: http: //maps.caseypthomas.org/ex/MarkerClustererPlus/exCoincidentMarkers_SharedInfowindow_wGeocoding.html

它工作得很好,并将内容组合到一个信息窗口中,但是似乎对破坏地图的 api 存在地理编码限制。我什至不需要对地址进行地理编码,因为我有一个要传入的坐标列表。

所以我的问题是,我怎样才能绕过 JS 的地理编码功能并以这种方式构建地图?

谢谢

我正在使用的代码:

        var map;

    //marker clusterer
    var mc;
    var mcOptions = {gridSize: 20, maxZoom: 17};

    //global infowindow
    var infowindow = new google.maps.InfoWindow();

    //geocoder
    var geocoder = new google.maps.Geocoder(); 

    //Content and Geos
    var address = new Array(<?php echo $tweetgeos;?>);
    var content = new Array(<?php echo $tweets;?>);


    function createMarker(latlng,text) {
        var marker = new google.maps.Marker({
            position: latlng
        });

        ///get array of markers currently in cluster
        var allMarkers = mc.getMarkers();


        //check to see if any of the existing markers match the latlng of the new marker
        if (allMarkers.length != 0) {
            for (i=0; i < allMarkers.length; i++) {
                var existingMarker = allMarkers[i];
                var pos = existingMarker.getPosition();

                if (latlng.equals(pos)) {
                    text = text + " & " + content[i];
                }
            }
        }

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

        return marker;
    }


    function geocodeAddress(address,i) {
        geocoder.geocode( {'address': address}, function(results, status) {

            if (status == google.maps.GeocoderStatus.OK) {

                var marker = createMarker(results[0].geometry.location,content[i]);
                mc.addMarker(marker);

            } else { 
                alert("Geocode was not successful for the following reason: " + status); 
        } 

        });
    }

    function initialize(){
        var options = { 
            zoom: 5, 
            center: new google.maps.LatLng(51.50733,-0.12768), 
            mapTypeId: google.maps.MapTypeId.ROADMAP 
        }; 

        map = new google.maps.Map(document.getElementById('map'), options); 


        //marker cluster
        mc = new MarkerClusterer(map, [], mcOptions);
        for (i=0; i<address.length; i++) { 
            geocodeAddress(address[i],i);
        }
    }

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

1 回答 1

0

您可以直接使用坐标初始化标记,而无需调用 Geocoding API。

// Change address Array to an Array of JS Objects
var address = [
    {
        latitude: lat1,
        longitude: lng1,
    },
    {
        latitude: lat2,
        longitude: lng2,
    },
    ...
];

// In function:createMarker()
var myLatLng = new google.maps.LatLng(latlng.latitude, latlng.longitude);
var marker = new google.maps.Marker({
    position: myLatLng,
});

// In function:initialize()
for (i=0; i<address.length; i++) { 
    createMarker(address[i], content[i]);
}
于 2012-07-02T10:36:01.550 回答