0

How can I adjust the zoom level within the function if the geolocation is successful (success)?

function createMap(lat, lng) {
var mapOptions = { center: new google.maps.LatLng(lat, lng), 
  zoom: 12,
  scrollwheel: false,
  mapTypeId: google.maps.MapTypeId.ROADMAP };
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

function initialize() {
if(navigator.geolocation) {
  success = function(position) {
    createMap(position.coords.latitude, position.coords.longitude);
  };
  error = function() { createMap(99.648493, -99.410812); }

  navigator.geolocation.getCurrentPosition(success, error);
 }
}
4
  • Hope you can pass one more argument to createmap called zoom level. If success one zoomlevel and if error then other zoom level.
    – Kiran
    Jul 2, 2012 at 6:39
  • also if map variable is global you could just call map.setZoom after createMap
    – slawekwin
    Jul 2, 2012 at 6:46
  • If you add the definition of createMap to your code listing, I'm sure someone will suggest something along the lines of @Matrix's comment. Jul 2, 2012 at 6:48
  • @AndrewLeach Thanks. sorry I am really green when it comes to JavaScript and Google Maps API for that matter, forgive the sloppiness. Jul 2, 2012 at 6:55

1 Answer 1

2

Please check the following code to change zoom level based on geolocation:

 function createMap(lat, lng, zoomVal) {
 var mapOptions = { center: new google.maps.LatLng(lat, lng),    
zoom: zoomVal,   
scrollwheel: false,  
 mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);  
function initialize() { 
if(navigator.geolocation) {  
 success = function(position) {    
 createMap(position.coords.latitude, position.coords.longitude,15);  
 };   
error = function() {
 createMap(99.648493, -99.410812,12); 
}    
navigator.geolocation.getCurrentPosition(success, error);  
} 
} 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.