-1
A
回答
0
示例脚本...
$(function() {
var locations = {};//A repository for markers (and the data from which they were constructed).
//initial dataset for markers
var locs = {
1: { info:'11111. Some random info here', lat:-37.8139, lng:144.9634 },
2: { info:'22222. Some random info here', lat:46.0553, lng:14.5144 },
3: { info:'33333. Some random info here', lat:-33.7333, lng:151.0833 },
4: { info:'44444. Some random info here', lat:27.9798, lng:-81.731 }
};
var map = new google.maps.Map(document.getElementById('map_2385853'), {
zoom: 1,
maxZoom: 8,
minZoom: 1,
streetViewControl: false,
center: new google.maps.LatLng(40, 0),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var auto_remove = true;//When true, markers for all unreported locs will be removed.
function setMarkers(locObj) {
if(auto_remove) {
//Remove markers for all unreported locs, and the corrsponding locations entry.
$.each(locations, function(key) {
if(!locObj[key]) {
if(locations[key].marker) {
locations[key].marker.setMap(null);
}
delete locations[key];
}
});
}
$.each(locObj, function(key, loc) {
if(!locations[key] && loc.lat!==undefined && loc.lng!==undefined) {
//Marker has not yet been made (and there's enough data to create one).
//Create marker
loc.marker = new google.maps.Marker({
position: new google.maps.LatLng(loc.lat, loc.lng),
map: map
});
//Attach click listener to marker
google.maps.event.addListener(loc.marker, 'click', (function(key) {
return function() {
infowindow.setContent(locations[key].info);
infowindow.open(map, locations[key].marker);
}
})(key));
//Remember loc in the `locations` so its info can be displayed and so its marker can be deleted.
locations[key] = loc;
}
else if(locations[key] && loc.remove) {
//Remove marker from map
if(locations[key].marker) {
locations[key].marker.setMap(null);
}
//Remove element from `locations`
delete locations[key];
}
else if(locations[key]) {
//Update the previous data object with the latest data.
$.extend(locations[key], loc);
if(loc.lat!==undefined && loc.lng!==undefined) {
//Update marker position (maybe not necessary but doesn't hurt).
locations[key].marker.setPosition(
new google.maps.LatLng(loc.lat, loc.lng)
);
}
//locations[key].info looks after itself.
}
});
}
var ajaxObj = {//Object to save cluttering the namespace.
options: {
url: "........",//The resource that delivers loc data.
dataType: "json"//The type of data tp be returned by the server.
},
delay: 10000,//(milliseconds) the interval between successive gets.
errorCount: 0,//running total of ajax errors.
errorThreshold: 5,//the number of ajax errors beyond which the get cycle should cease.
ticker: null,//setTimeout reference - allows the get cycle to be cancelled with clearTimeout(ajaxObj.ticker);
get: function() { //a function which initiates
if(ajaxObj.errorCount < ajaxObj.errorThreshold) {
ajaxObj.ticker = setTimeout(getMarkerData, ajaxObj.delay);
}
},
fail: function(jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
ajaxObj.errorCount++;
}
};
//Ajax master routine
function getMarkerData() {
$.ajax(ajaxObj.options)
.done(setMarkers) //fires when ajax returns successfully
.fail(ajaxObj.fail) //fires when an ajax error occurs
.always(ajaxObj.get); //fires after ajax success or ajax error
}
setMarkers(locs);//Create markers from the initial dataset served with the document.
//ajaxObj.get();//Start the get cycle.
// *******************
//test: simulated ajax
/*
var testLocs = {
1: { info:'1. New Random info and new position', lat:-37, lng:124.9634 },//update info and position and
2: { lat:70, lng:14.5144 },//update position
3: { info:'3. New Random info' },//update info
4: { remove: true },//remove marker
5: { info:'55555. Added', lat:-37, lng:0 }//add new marker
};
setTimeout(function() {
setMarkers(testLocs);
}, ajaxObj.delay);
*/
// *******************
});
相关问题
- 1. 添加标记 - 谷歌地图API V3
- 2. 如何把谷歌地图v3的fancybox谷歌地图v3
- 3. 谷歌地图V3错误
- 4. 谷歌地图API V3 2.0.6
- 5. GWT +谷歌地图API V3
- 6. 谷歌地图v3 OverlayView.getProjection()
- 7. 谷歌地图API V3 Over_Query_Limit
- 8. 谷歌地图v2与v3
- 9. 谷歌地图V3 - 英里
- 10. 添加/动态地在谷歌地图V3
- 11. 如何将自定义图标添加到谷歌地图v3
- 12. Javascript v3谷歌地图旋转地图
- 13. 谷歌地图API V3在地图
- 14. 当谷歌地图v3 markercluster中按下关闭按钮时,infowindow未关闭。
- 15. 演示显示多个谷歌地图(谷歌地图API v3)
- 16. 通过搜索框添加谷歌地图V3用户输入
- 17. 在infowindow里添加alt标签,用于谷歌地图v3 javascript
- 18. 谷歌地图api v3从阵列添加多义线
- 19. 在标记谷歌地图v3上添加onclick事件
- 20. 谷歌地图v3通过jQuery添加标记$ .ajax和json
- 21. 如何在谷歌地图v3中添加值
- 22. 使用Json在谷歌地图V3中添加多个标记
- 23. 动态添加InfoWindows和标记谷歌地图V3
- 24. A到回调添加到谷歌地图API V3
- 25. 谷歌地图v3 - 添加制造商导致重复标记
- 26. 添加获取指南链接到InfoWindow谷歌地图V3
- 27. 谷歌地图v3 - 土地类型
- 28. 地理位置与谷歌地图V3
- 29. 在谷歌地图中添加标记谷歌地图反应
- 30. 淡入淡出地图/在谷歌地图添加透明图层v3