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 badges2 Answers
Reset to default 8Well, 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
版权声明:本文标题:javascript - google maps v3 zoom to fit all markers(path) function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744093529a2532477.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论