How to get the MapOptions object from a map with Google Maps API v3

99封情书 提交于 2020-01-02 02:34:20

问题


In Google Maps api v2 you could get parameters such as the map type, zoom etc directly from the map object. In version 3 you have the setOptions method to set some parameters, but there is no getOptions() or options to retrieve them.


回答1:


You can access those properties via methods on the Map class:

  • getZoom()
  • getMapTypeId()
  • getCenter()
  • etc..



回答2:


You can also access options using the get method on the map as an MVCObject as shown in this example

// create map
var myLatlng = new google.maps.LatLng(-33, 151);
var myOptions = {
  center: myLatlng,
  zoom: 5
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

map.setOptions({
  streetViewControl: false,
  zoom: 6,
  zoomControl: false,
  }
);

document.getElementById("center").value = map.get('center');
document.getElementById("streetViewControl").value = map.get('streetViewControl');
document.getElementById("zoom").value = map.get('zoom');
document.getElementById("zoomControl").value = map.get('zoomControl');
#map_canvas {
  width: 50%;
  height: 200px;
  float: left;
}

input {
  width: 90px;
  }
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<div id="map_canvas"></div>

<input type="text" id="center" /> center<br>
<input type="text" id="streetViewControl" /> streetViewControl<br>
<input type="text" id="zoom" /> zoom<br>
<input type="text" id="zoomControl" /> zoomControl<br>
...


来源:https://stackoverflow.com/questions/3369842/how-to-get-the-mapoptions-object-from-a-map-with-google-maps-api-v3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!