我正在使用jQuery Google Maps v3 plugin,我想添加带有goMap插件的MarkerClusterer,但是我打算这样做:
$('#world-map').goMap(
{
markers: mapMarkers,
zoom: 2,
icon: 'http://localhost/eproj/white-marker.png',
streetViewControl: true,
});
$.goMap.ready(function()
{
var markers = [];
for (var i in $.goMap.markers)
{
var temp = $($.goMap.mapId).data($.goMap.markers[i]);
markers.push(temp);
}
var markerclusterer = new MarkerClusterer($.goMap.map, markers, clustererOpts);
});
查看地图(当前有35个标记)时,我看不到任何生效,除了所有标记均已绘制。在Firebug中,我看不到正在记录任何错误,因此我不知道为什么它不能与goMap一起使用。
任何人?甚至没有一个简单的解决方案?
编辑:我不能放在jsFiddle上,似乎弄乱了向jsFiddle添加JS资源。
这是
mapMarkers
变量:[{"id":"9073a1977c8d5d6d7eb1666d03af4a66","address":"3 Essex St, Jersey City, Hudson, United States"},{"id":"f63f77a0b2d0986f1f0e94bde07ff8ef","address":"96 Vesey St, New York, New York, United States"},{"id":"57d5cb180425670f3e9c7b6e62fb88ac","address":"38 E Broadway, Manhattan, New York, United States"},{"id":"bb464b47ca54a23edc3dee4a5f4299ad","address":"322 Tait Ave, Sanger, California, United States"},{"id":"980f9d1e9e466d988f4851117a268e25","address":"1495 Morton Ave, Parsons, Kansas, United States"},{"id":"389888acd972d01783547837841d5294","address":"160 N Washington St, Clinton, Missouri, United States"}]
即使没有标记聚类器生效,标记也彼此非常接近,在地图缩放为1时。
最佳答案
[新编辑]
对于此问题,最简单的解决方法是通过破解jquery.gomap-1.3.2.js
源代码,如下所示:
在初始化函数的myOptions变量上添加markerCreated属性
之前
var myOptions = {
....
scrollwheel: opts.scrollwheel,
zoom: opts.zoom
};
后
var myOptions = {
....
scrollwheel: opts.scrollwheel,
zoom: opts.zoom,
markerCreated: opts.markerCreated //patched here
};
更改并添加createMarker函数的方法:
之前
createMarker: function(marker) {
....
options.position = marker.position ? marker.position : new google.maps.LatLng(marker.latitude, marker.longitude);
var cmarker = new google.maps.Marker(options);
if (marker.html) {
....
},
后
createMarker: function(marker) {
....
options.position = marker.position ? marker.position : new google.maps.LatLng(marker.latitude, marker.longitude);
var goMap = this; //patched here
var cmarker = new google.maps.Marker(options);
if(goMap.opts.markerCreated) goMap.opts.markerCreated(cmarker); //patched here
if (marker.html) {
....
},
然后在您自己的代码中更改为:
$('#world-map').goMap(
{
markers: mapMarkers,
zoom: 2,
icon: 'http://localhost/eproj/white-marker.png',
streetViewControl: true,
});
至:
$(function() {
var markerclusterer;
var markers = [];
var mapMarkers = [{"id":"9073a1977c8d5d6d7eb1666d03af4a66","address":"3 Essex St, Jersey City, Hudson, United States"},
{"id":"f63f77a0b2d0986f1f0e94bde07ff8ef","address":"96 Vesey St, New York, New York, United States"},
{"id":"57d5cb180425670f3e9c7b6e62fb88ac","address":"38 E Broadway, Manhattan, New York, United States"},
{"id":"bb464b47ca54a23edc3dee4a5f4299ad","address":"322 Tait Ave, Sanger, California, United States"},
{"id":"980f9d1e9e466d988f4851117a268e25","address":"1495 Morton Ave, Parsons, Kansas, United States"},
{"id":"389888acd972d01783547837841d5294","address":"160 N Washington St, Clinton, Missouri, United States"}
];
$("#map").goMap({
markerCreated: function(marker) {
markers.push(marker);
//updated solution for slow MarkerClusterer loading
if(markers.length == mapMarkers.length) {
new MarkerClusterer($.goMap.map, markers);
}
//prev solution
//if(!markerclusterer) markerclusterer = new MarkerClusterer($.goMap.map, markers);
//markerclusterer.addMarker(marker);
},
markers: mapMarkers
});
});
删除
$.goMap.ready
部分如果此黑客有效/请立即让我
谢谢
[编辑]
我认为这是因为在创建MarkerCluster时,它没有通过正确的markers对象参数传递。
这是因为您的mapMarkers变量在创建标记对象之前需要先进行地理编码,与此同时,在代码中,您必须在对标记进行大致地理编码之前强制创建MarkerCluster(返回具有position属性的标记对象)
相信我,当您将mapMarkers变量替换为标记对象(我的意思是已知位置)时,您会看到群集正确显示
尝试这个:
[{
latitude: 40.713094,
longitude: -74.033904,
id: 'test1',
title: 'marker title 1'
},{
latitude: 40.712638,
longitude: -74.03429,
id: 'test2',
title: 'marker title 2'
}]
[旧]
也许这样的帮助:
goMap Markercluster Soluton
抱歉,无法编写出更简单的解决方案;这;)
提示:尝试查看页面源代码
关于jquery - 具有MarkerClusterer的goMap不会对当前 map 产生任何影响,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7587295/