admin管理员组文章数量:1026399
So, I've seen this solution on how to calculate bounds given center and zoom, but I'm trying to calculate bounds given center coordinates and a distance in miles.
How could I do this?
So, I've seen this solution on how to calculate bounds given center and zoom, but I'm trying to calculate bounds given center coordinates and a distance in miles.
How could I do this?
Share Improve this question edited May 23, 2017 at 12:21 CommunityBot 11 silver badge asked Apr 24, 2014 at 4:10 kambythetkambythet 7162 gold badges9 silver badges21 bronze badges1 Answer
Reset to default 4Use puteOffset to calculate a point at a distance east and west of the center. Add those two points to a google.maps.LatLngBounds and call map.fitBounds on that bounds.
proof of concept fiddle
code snippet:
// This example requires the Geometry library. Include the libraries=geometry
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis./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.puteOffset(map.getCenter(), distance, 90);
bounds.extend(east);
var west = google.maps.geometry.spherical.puteOffset(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.puteLength(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./maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=geometry&callback=initMap"
async defer></script>
So, I've seen this solution on how to calculate bounds given center and zoom, but I'm trying to calculate bounds given center coordinates and a distance in miles.
How could I do this?
So, I've seen this solution on how to calculate bounds given center and zoom, but I'm trying to calculate bounds given center coordinates and a distance in miles.
How could I do this?
Share Improve this question edited May 23, 2017 at 12:21 CommunityBot 11 silver badge asked Apr 24, 2014 at 4:10 kambythetkambythet 7162 gold badges9 silver badges21 bronze badges1 Answer
Reset to default 4Use puteOffset to calculate a point at a distance east and west of the center. Add those two points to a google.maps.LatLngBounds and call map.fitBounds on that bounds.
proof of concept fiddle
code snippet:
// This example requires the Geometry library. Include the libraries=geometry
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis./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.puteOffset(map.getCenter(), distance, 90);
bounds.extend(east);
var west = google.maps.geometry.spherical.puteOffset(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.puteLength(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./maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=geometry&callback=initMap"
async defer></script>
本文标签: javascriptGoogle maps v3 APIcalculate bounds given center coordinates and distanceStack Overflow
版权声明:本文标题:javascript - Google maps v3 API - calculate bounds given center coordinates and distance - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745647154a2161092.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论