gpt4 book ai didi

javascript - 不带地理编码的 Google map 标记聚类器 Plus

转载 作者:行者123 更新时间:2023-11-28 09:49:20 24 4
gpt4 key购买 nike

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

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

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

所以我的问题是,如何绕过 JS 的地理编码功能并以这种方式构建 map ?

谢谢

我正在使用的代码:

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

最佳答案

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

关于javascript - 不带地理编码的 Google map 标记聚类器 Plus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11291824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com