地理定位成功后调整缩放级别?

时间:2012-07-02 06:36:22

标签: javascript google-maps-api-3

如何在地理位置功能成功(成功)的范围内调整缩放级别?

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);
 }
}

1 个答案:

答案 0 :(得分:2)

请检查以下代码,根据地理位置更改缩放级别:

 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);  
} 
}