Google maps set zoom from hidden input value-Collection of common programming errors
I have a form with a hidden input that saves user’s current zoom level
However, when I try to use the zoom value from the input after I submit my form it doesn’t use the input’s value
var zoom = $("#zoom").val();
var point;
var gmap = new GMap2(document.getElementById("gmap"));
if (zoom == "") {
point = new google.maps.LatLng(latitude,longitude);
gmap.setCenter(point, 13);
} else {
console.log(zoom); //shows 15
point = new google.maps.LatLng(latitude,longitude);
gmap.setCenter(point, zoom); //here the zoom is undefined
}
-
The value of ‘zoom’ from the input field is a string, and the function requires a number. Use
parseInt()
to cast the field value as a numeric value.var zoom = parseInt($("#zoom").val()); var point; var gmap = new GMap2(document.getElementById("gmap")); // point assignment is abstracted from your zoom level logic point = new google.maps.LatLng(latitude,longitude); gmap.setCenter(point, (typeof zoom == 'number') ? zoom : 13);
Originally posted 2013-11-09 22:55:54.