我用Ajax加載來自外部的JSON我的地方是這樣的:,刪除先前標記,加載新的地方
[{
"id":"1",
"title": "Stockholm",
"lat": 59.3,
"lng": 18.1,
},
{
"id":"2",
"title": "Oslo",
"lat": 59.9,
"lng": 10.8,
},
{
"id":"2",
"title": "Copenhagen",
"lat": 55.7,
"lng": 12.6,
}]
,並在頁面我的地圖加載有這樣的代碼:
var MapInitialized = false,
$map = $('#googleMap .map');
var mapProp = {
center:new google.maps.LatLng(35.6891970, 51.3889740),
zoom:12,
disableDefaultUI: true,
zoomControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false,
animation: google.maps.Animation.DROP,
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'bestfromgoogle']
}
};
var map = new google.maps.Map($map[0], mapProp);
var infoWindow = new google.maps.InfoWindow();
$("select").change(function(){
$.ajax({
type: "GET",
url: "places.json",
dataType: "json",
beforeSend: function(){ $(".mapLoading").fadeIn();},
complete: function(){ $(".mapLoading").fadeOut();},
success: function(response){
var LatLngList = new Array();
var marker;
$.each(response, function(key, data) {
Point = new google.maps.LatLng(data.lat, data.lng);
LatLngList.push(Point);
console.log(LatLngList);
marker = new google.maps.Marker({
position: Point,
map: map,
title: data.title
});
});
var bounds = new google.maps.LatLngBounds();
for (var i = 0, LtLgLen = LatLngList.length; i < LtLgLen; i++) {
bounds.extend (LatLngList[i]);
}
map.fitBounds (bounds);
}
});
});
我在我的HTML頁面上有一個選擇器,當用戶更改它時,ajax發送請求到json和標記將顯示在地圖上。現在我需要刪除所有以前的標記並在地圖上加載新標記而不加載新地圖。
可能重複刷新標記](http://stackoverflow.com/questions/18443941/google-maps-refresh-markers) – geocodezip
[Google Map API - 刪除標記]的可能重複(http://stackoverflow.com/questions/16482949/google -Map-API雷莫ving-markers) – geocodezip