通过javascript更改缩放和锁定(谷歌地图javascript API v3)
我正在使用Google Maps JavaScript API v3,并使用以下代码以JavaScript绘制初始地图:通过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
<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);
}