我有兩個標記具有相同的座標,所以我試圖逼近它們的座標以便它們都顯示。我有下面的代碼似乎不工作。奇怪的是,在瀏覽器控制檯中,我甚至沒有看到任何錯誤。沒有,但根本沒有標記顯示。通過四捨五入來逼近標記座標
for (var i = 0; i < locations.length; i++) {
var item = locations[i];
var a = 0.01;
var newLat = item[1] + -.00004 * Math.cos((+a*i)/180 * Math.PI); // x
var newLng = item[2] + -.00004 * Math.sin((+a*i)/180 * Math.PI); // Y
var finalLatLng = new google.maps.LatLng(newLat,newLng);
var marker = new google.maps.Marker({
position: finalLatLng,
map: map,
title: item[0],
link: item[3],
address: item[5],
phone: item[6],
mobile: item[7],
website: item[8],
image: item[9],
readmore: item[10],
icon: item[11],
type: item[12],
infoWindowIndex: i
});
marker.category = locations[i][12];
gmarkers.push(marker);
google.maps.event.addListener(marker, 'click', function (event) {
var ua = navigator.userAgent;
var event = (ua.match(/iPad/i)) ? "touchstart" : "click";
// prevent ghost clicks on ipad
infoBox.setContent('<div class="marker-wrapper animated fadeInDown"><div class="marker-title"> <a href="'+ this.link +'">'+ this.title +'</a> </div><div class="marker-content"><div class="two_third popup-content"><ul>'+ this.address +''+ this.phone +''+ this.mobile +''+ this.website +'</ul></div>'+ this.image +''+ this.readmore +'<div class="clearboth"></div> <div class="close" onClick=\'javascript:infoBox.close();\'><span class="icon-cancel"></span></div></div><span class="icon-down-dir"></span></div>');
infoBox.open(map, this);
map.setCenter(this.position);
map.panTo(this.position);
}); // end click even
} //end for
其他一些信息:
我使用JSON搶標記信息
我使用MarkerClusterer的集羣。
我也嘗試通過傳遞一個新的var到位置參數來編輯代碼,我得到錯誤**Uncaught InvalidValueError: setPosition: not a LatLng or LatLngLiteral: not an Object**
。
for (var i = 0; i < locations.length; i++) {
var item = locations[i];
var myLatLng = new google.maps.LatLng(item[1], item[2]);
var marker = new google.maps.Marker({
position: myLatLng + (Math.random() -.5)/1500,
map: map,
title: item[0],
link: item[3],
address: item[5],
phone: item[6],
mobile: item[7],
website: item[8],
image: item[9],
readmore: item[10],
icon: item[11],
type: item[12],
infoWindowIndex: i
});
marker.category = locations[i][12];
gmarkers.push(marker);
google.maps.event.addListener(marker, 'click', function (event) {
var ua = navigator.userAgent;
var event = (ua.match(/iPad/i)) ? "touchstart" : "click";
// prevent ghost clicks on ipad
infoBox.setContent('<div class="marker-wrapper animated fadeInDown"><div class="marker-title"> <a href="'+ this.link +'">'+ this.title +'</a> </div><div class="marker-content"><div class="two_third popup-content"><ul>'+ this.address +''+ this.phone +''+ this.mobile +''+ this.website +'</ul></div>'+ this.image +''+ this.readmore +'<div class="clearboth"></div> <div class="close" onClick=\'javascript:infoBox.close();\'><span class="icon-cancel"></span></div></div><span class="icon-down-dir"></span></div>');
infoBox.open(map, this);
map.setCenter(this.position);
map.panTo(this.position);
}); // end click even
} //end for
任何線索如何解決這個問題?
我建議看着spidifier插件(https://github.com/jawj/OverlappingMarkerSpiderfier),在這裏演示:http://jawj.github.com/OverlappingMarkerSpiderfier/demo.html - spidifier plugn也可以很好地與markerclusterer插件 – adaam 2014-08-29 14:44:29