Google Maps API:计算折线的中心/缩放
我有一个根据用户数据动态生成的叠加层,因此我需要知道如何找到该叠加层的中心。
目前,我只是使用叠加层的第一个坐标,但这实际上并不代表叠加层的中心。因此,当我加载地图时,它不会以叠加层为中心。
有没有人有一个好的方法可以通过计算中心而不是对其进行硬编码来使地图在覆盖层上居中?
var latlng = new google.maps.LatLng(38.269239, -122.276010);
var myOptions = {
zoom: 15,//Calculate this somehow?
center: latlng,// Calculate value from array of coordinates?
mapTypeId: google.maps.MapTypeId.HYBRID
};
I have an overlay that is dynamically generated from user data, so I need to know how to find the center of that overlay.
Currently, I am just using the first coordinates from the overlay, but that really does not represent the center of the overlay. Therefore when I load the map, it is not centered on the overlay.
Does anyone have a good method for centering the map on the overlay, by calculating the center, not hard coding it?
var latlng = new google.maps.LatLng(38.269239, -122.276010);
var myOptions = {
zoom: 15,//Calculate this somehow?
center: latlng,// Calculate value from array of coordinates?
mapTypeId: google.maps.MapTypeId.HYBRID
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有坐标列表,则可以循环遍历它们并将它们添加到 LatLngBounds 对象。这是 V3 API 的示例,但 V2 中的概念类似:
之后,您可以通过以下方式获取中心:
或者,您可以直接调用
map.fitBounds()
,这将居中围绕边界中心的地图并调整缩放,以便整个边界完全适合视口。If you have a list of coordinates, you can loop over them and add them to a LatLngBounds object. Here is a example for the V3 API, but the concept in V2 is similar:
After that, you can get the center with:
Or alternatively, you can call
map.fitBounds()
directly, which will center the map around the center of the bounds and adjust the zoom, so that the whole bounds will fit exactly into the view port.基于@tux21b,
Based on @tux21b,