create a new map with a jquery function
i'm using google maps v3 and i want create a new map associate to a event with a combo but i don't know why don't work, take a look to my code.
Note i debug the code and works great the problem it's that the map doesn't show
$(document).ready(initialize);
var map;
function initialize() {
x = $(".comboMap");
x.change(buildMap);
}
function buildMap(){
$.ajax({
async: false,
type: "POST",
url: "SaveIndicators.aspx/findMapParameters",
data: "{idMapa: '" + $(".comboMap").val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: response
});
}
/***Sets the values in the ma开发者_运维百科p****/
function response(values) {
var location = values.d.split(",");
var lat = parseFloat(location[0]);
var lng = parseFloat(location[1]);
var latLng = new google.maps.LatLng(lat, lng);
var mapDiv = $("#map-canvas")[0];
map = new google.maps.Map(mapDiv, {
center: latLng,
zoom: location[2],
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
});
}
You need to convert your string value for the zoom level to an integer:
function response(values) {
var location = values.d.split(",");
var lat = parseFloat(location[0]);
var lng = parseFloat(location[1]);
var zoom = parseInt(location[2]);
var latLng = new google.maps.LatLng(lat, lng);
var mapDiv = $("#map_canvas")[0];
map = new google.maps.Map(mapDiv, {
center: latLng,
zoom: zoom,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
});
}
I would have thought it would have cast the value passing the string, but it didn't. I had to use parseInt() to make an integer to get the Map constructor to treat it correctly.
have you included a reference to google maps script in the header of your page?
are you getting any javascript errors when the page loads?
are you sure your service call is returning data and in the correct format? it may be returning XML instead of JSON.
EDIT:
i notice you are doing var mapDiv = $("#map-canvas")[0]; what is the reason for treating it like an array? you are asking for it by name, so there should only be one on the page anyway....
精彩评论