没有地理编码的Google Maps Marker Clusterer Plus

时间:2012-07-02 10:24:18

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

使用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);

1 个答案:

答案 0 :(得分:0)

您可以直接使用坐标初始化标记,而无需调用地理编码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]);
}