Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过javascript更改缩放和锁定(谷歌地图javascript API v3)_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

通过javascript更改缩放和锁定(谷歌地图javascript API v3)

通过javascript更改缩放和锁定(谷歌地图javascript API v3),javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我正在使用Google Maps JavaScript API v3,并使用以下代码以JavaScript绘制初始地图: <script type="text/javascript"src="https://maps.googleapis.com/maps/api/js?key={APIKey}"></script> <script type="text/javascript"> function initialize() { var mapOpt

我正在使用Google Maps JavaScript API v3,并使用以下代码以JavaScript绘制初始地图:

<script type="text/javascript"src="https://maps.googleapis.com/maps/api/js?key={APIKey}"></script>
<script type="text/javascript">
  function initialize() {
    var mapOptions = {
    zoom: 8,
    center: new google.maps.LatLng(51.507351, -0.127758)
    };
    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  } 
</script>

map变量是initialize函数的局部变量。如果要从HTML“单击”事件访问它,它需要是全局的

然后这段代码就可以工作了(并且不会生成一个javascript错误,声明“map”未定义)


map变量是initialize函数的局部变量。如果要从HTML“单击”事件访问它,它需要是全局的

然后这段代码就可以工作了(并且不会生成一个javascript错误,声明“map”未定义)

function changeMap () {
     map.setCenter(new google.maps.LatLng(-34.397, 150.644));
     map.setZoom(15);
  }
// declare global map variable (outside of any functions)
var map = null;
function initialize() {
  var mapOptions = {
  zoom: 8,
  center: new google.maps.LatLng(51.507351, -0.127758)
  };
  // initialize the map variable after the page "load" event fires
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
} 
function changeMap () {
   map.setCenter(new google.maps.LatLng(-34.397, 150.644));
   map.setZoom(15);
}