I have a map with a lot of markers. However, I want to be able to change the center / zoom, without deleting the markers. I do this when a user search for an address and click "search".
I have the following center function which works, however, it also deletes all my markers!
function centerMap(longitude, latitude) {
var gMap = new google.maps.Map(document.getElementById('map-canvas'));
gMap.setZoom(10);
gMap.setCenter(new google.maps.LatLng(longitude, latitude));
}
My markup is the classic:
<div id="map-canvas" style="width: 900px; height: 700px;" />
Whole code when clicking the button:
$('#searchCityBtn').click(function () {
var query = $('#addressBox').val();
var url = '<%= ResolveUrl("~/services/geolocationservice.asmx/getlatitudeandlongitude") %>';
$.ajax({
type: "POST",
data: "{'query':'" + query + "'}",
url: url,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (d) {
var jsonResp = JSON.parse(d.d);
centerMap(jsonResp.Item2,jsonResp.Item1);
},
error: function () {
alert('error guys');
}
});
});
Of course, I have tried to get the map by doing this:
function centerMap(longitude, latitude) {
var gMap = document.getElementById('map-canvas');
gMap.setZoom(10);
gMap.setCenter(new google.maps.LatLng(longitude, latitude));
}
But, then I get the errors no function called setZoom or setCenter exists.
How to solve this issue? :)