I use to ajax to load my places from external json like this:
[{
"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,
}]
and my map load in page with a code like this:
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);
}
});
});
I have a selector on my HTML page that when user change it ajax send request to json and marker will show on map. now i need to remove all previous marker and load new marker on map without loading a new map.