Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use fitBounds of GMaps.js

I am using GMaps.js and I have several points to show, I need to show all and set the zoom to the point, I was watching the documentation and requests an array of google.maps.LatLng type. I have the following code

for (x in geo){
var item  = new google.maps.LatLng(geo[x]["latitud"], geo[x]["longitud"]);
arrayBound.push(item);
}
map = new GMaps({
div: '#divMapa',
lat: latWS,
lng: lngWS,
zoom: 13
});

map.fitBounds(arrayBound);
like image 429
Enrique Ardavin Avatar asked Dec 12 '13 18:12

Enrique Ardavin


People also ask

How do I reference Google Maps?

The required elements for referencing from Google Maps are: Map publisher (Year of publication) Title of map section, Sheet number or tile, scale. Available at: DOI or URL (Accessed: date).

What is used to calculate the viewport of the map?

Longitude + (result. Span.

Why won't my Google Maps work on my phone?

Clear the app's cache & data On your Android phone or tablet, open the Settings app . Tap Apps & notifications. Follow the steps on your device to find the Maps app. After you select the app, storage & cache options should be available.


1 Answers

I didn't find the solution obvious, but, here is an example that works with v0.4.11:

var latlngs = [{lat:"39.158542", lng:"-84.423903"}, {lat:"39.4339357", lng:"-84.9850474"}];
var bounds = [];
for (var i in latlngs) {
  var latlng = new google.maps.LatLng(latlngs[i].lat, latlngs[i].lng);
  bounds.push(latlng);
  map.addMarker({
    lat: latlngs[i].lat,
    lng: latlngs[i].lng
  });
}
map.fitLatLngBounds(bounds);
like image 133
josephdpurcell Avatar answered Oct 13 '22 10:10

josephdpurcell