我正在使用Google地图并使用jQuery Ajax从数据库表中加载标记。现在一切都很适合我,但有两件事,我不知道如何实施谷歌地图:
bounds_changed
时清除所有以前的标记。第2点:
我尝试过这段代码,但没有运气。你可以看到我在我的代码中添加了。
var infowindow = new google.maps.InfoWindow({});
infowindow.close();
我正在使用latitude
&获取json数据longitude
:
[
{"latitude":"23.046100780353495","longitude":"72.56860542227514"},
{"latitude":"23.088427701737665","longitude":"72.49273109366186"},
{"latitude":"23.061264193197644","longitude":"72.68224525381811"},
{"latitude":"22.977212139977677","longitude":"72.52191352774389"},
{"latitude":"23.002180435752084","longitude":"72.47590827872045"},
{"latitude":"23.108638843843046","longitude":"72.49444770743139"}
]
使用Ajax的Google代码:
google.maps.event.addListener(map, 'bounds_changed', function() {
var bounds = map.getBounds();
var ne = bounds.getNorthEast();
var sw = bounds.getSouthWest();
$.ajax({
type: "POST",
url: 'get-locations.php',
data: 'ne='+ne+'&sw='+sw,
success:function(data){
var infowindow = new google.maps.InfoWindow({});
var objects_array = JSON.parse(data); // This is an array of objects.
var totalLocations = objects_array.length;
for (var i = 0; i < totalLocations; i++) {
var obj = objects_array[i]; // This is one object from the array.
// Init markers
var marker = new google.maps.Marker({
position: new google.maps.LatLng(obj.latitude, obj.longitude),
map: map,
icon: 'http://localhost/google/marker.png', // Default icon
title: obj.title
});
// Process multiple info windows
(function(marker, i) {
// Add click event
google.maps.event.addListener(marker, 'click', function() {
//console.log(objects_array[i].info_data);
//infowindow.close();
infowindow = new google.maps.InfoWindow({
content: '<div class="MarkerPopUp"><div class = "MarkerContext">'+objects_array[i].info_data+'</div></div>'
});
infowindow.open(map, this);
});
})(marker, i);
}
}
});
});
任何想法如何做到这两点。我会感激,如果你引导我的例子:)。
感谢。
答案 0 :(得分:1)
使用以下代码清除所有标记。见google help for marker
var markers = [];
var infowindow = null;
// Sets the map on all markers in the array.
function setAllMap(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
setAllMap(null);
}
google.maps.event.addListener(map, 'bounds_changed', function() {
var bounds = map.getBounds();
var ne = bounds.getNorthEast();
var sw = bounds.getSouthWest();
$.ajax({
type: "POST",
url: 'get-locations.php',
data: 'ne='+ne+'&sw='+sw,
success:function(data){
var infowindow = new google.maps.InfoWindow({});
var objects_array = JSON.parse(data); // This is an array of objects.
var totalLocations = objects_array.length;
clearMarkers(); // clear all markers before addd new markers
for (var i = 0; i < totalLocations; i++) {
var obj = objects_array[i]; // This is one object from the array.
// Init markers
var marker = new google.maps.Marker({
position: new google.maps.LatLng(obj.latitude, obj.longitude),
map: map,
icon: 'http://localhost/google/marker.png', // Default icon
title: obj.title
});
//// Add marker object to array .
markers.push(marker);
// Process multiple info windows
(function(marker, i) {
// Add click event
google.maps.event.addListener(marker, 'click', function() {
if (infowindow) {
infowindow.close();
}
infowindow = new google.maps.InfoWindow({
content: '<div class="MarkerPopUp"><div class = "MarkerContext">'+objects_array[i].info_data+'</div></div>'
});
infowindow.open(map, this);
});
})(marker, i);
}
}
});
});