谷歌地图v3neneneba API-计算边界给定的中心坐标和距离
Google maps v3 API - calculate bounds given center coordinates and distance
因此,我已经看到了关于如何计算给定中心和缩放的边界的解决方案,但我正在尝试计算给定中心坐标和以英里为单位的距离的边界。
我怎么能这么做?
使用computeOffset计算中心东西距离处的点。将这两点添加到google.maps.LatLngBounds中,并在该边界上调用map.fitBounds。
概念验证小提琴
代码片段:
// This example requires the Geometry library. Include the libraries=geometry
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=geometry">
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {lat: 37.04831, lng: -95.297565}
});
var distance = 10000; // 10 km
var bounds = new google.maps.LatLngBounds();
var east = google.maps.geometry.spherical.computeOffset(map.getCenter(), distance, 90);
bounds.extend(east);
var west = google.maps.geometry.spherical.computeOffset(map.getCenter(), distance, 270);
bounds.extend(west);
map.fitBounds(bounds);
var polyline = new google.maps.Polyline({
path: [east, west],
map: map
});
var lineLength = google.maps.geometry.spherical.computeLength(polyline.getPath());
var infowindow = new google.maps.InfoWindow();
infowindow.setContent("line length is "+(lineLength/1000).toFixed(2)+" km");
infowindow.setPosition(map.getCenter());
infowindow.open(map);
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=geometry&callback=initMap"
async defer></script>
相关文章:
- EaseJS拖放;放下(动画CC)电影剪辑的鼠标坐标
- 如何在d3.js中返回路径的y坐标
- 在谷歌地图上获取事件的x,y坐标
- 通过ajax将坐标传递到php服务器端,并在处理后检索到javascript
- 当带有渲染器的DOM元素不在屏幕顶部时,移动了场景的坐标
- 如何在JS Leatflet绘图插件中获取圆的坐标
- 点击(右键点击)使用传单地图库获取图像覆盖的像素坐标
- 坐标之间的音差地理位置距离
- 将 GPS 坐标转换为距离,并以十进制度为单位将距离转换为半径
- javascript中两个画布坐标之间的距离
- AngularJS获取多个坐标之间的距离
- 使用数组计算坐标之间的距离,并设置Y轴和比例- D3.js
- 加载坐标时的距离误差
- 使用Javascript有效地从坐标数组中获得距离最短的一对坐标
- 在arcgis中通过当前点、角度和距离获得新的点坐标
- 根据提示存储坐标并计算距离
- 如何在驱动模式下使用javascript查找两个坐标之间的距离
- 在坐标数组上循环并计算距离javascript
- 谷歌地图v3neneneba API-计算边界给定的中心坐标和距离
- 计算距离一定米的两个坐标之间的经纬度