Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 804
Laurenţiu Lozan Avatar asked Jul 30 '10 08:07

Laurenţiu Lozan


People also ask

How do I get data from Google Maps API?

Go to APIs & Services → Dashboard → Enable APIs & Services at the top and Choose Maps Javascript API from the API Library. This will open up the Map JavaScript API page, and Enable it.

How is the global namespace reffered for marker object in version 3 of the API as compared to version 2?

Probably the most noticeable change in the Maps JavaScript API v3 is the introduction of the google. maps namespace. The v2 API places all objects in the Global namespace by default, which can result in naming collisions. Within v3, all objects are located within the google.


2 Answers

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>
...
like image 67
user2314737 Avatar answered Oct 03 '22 19:10

user2314737


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

  • getZoom()
  • getMapTypeId()
  • getCenter()
  • etc..
like image 36
RedBlueThing Avatar answered Oct 03 '22 19:10

RedBlueThing