admin 管理员组

文章数量: 1086019

I've put together a little snippet on js fiddle so you can see what I am working with.

Basically I am trying to hook up a "Zoom" button so that once a path is created you can click the zoom button and the map zooms to fit the path. All of the answers I have found work by having an array of markers which I do not have. Any suggestions would be greatly appreciated.

/

I've put together a little snippet on js fiddle so you can see what I am working with.

Basically I am trying to hook up a "Zoom" button so that once a path is created you can click the zoom button and the map zooms to fit the path. All of the answers I have found work by having an array of markers which I do not have. Any suggestions would be greatly appreciated.

http://jsfiddle/A3NBZ/

Share Improve this question edited Oct 1, 2012 at 18:21 JSK NS 3,4462 gold badges28 silver badges42 bronze badges asked Oct 1, 2012 at 18:21 ShaneShane 5581 gold badge6 silver badges17 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

Well, in fact you do have an array of markers! It's stored in the Polyline that you're creating when the user is clicking on the map. To retrieve the points on which the user has clicked, simply use Polyline.getPath(). You can then add those points (as geocodezip mentioned) to a LatLngBounds object and use google.maps.Map.fitBounds() to adjust the map view to the given bounds.

Here's a simple implementation of the zoom method, based on the code example you've provided (you can see it working here).

function zoom() {
  var bounds = new google.maps.LatLngBounds();
  geodesic.getPath().forEach(function(latLng) {
    bounds.extend(latLng);
  });
  map.fitBounds(bounds);                    
}

Similar to the examples you have seen with markers, add all the google.maps.LatLngs in the path to a google.maps.LatLngBounds object (using bounds.extend()), then call map.fitBounds on the resulting bounds object.

updated jsfiddle

本文标签: javascriptgoogle maps v3 zoom to fit all markers(path) functionStack Overflow