Say I have a center coordinate (1.22222, 2.3333) and width 25 km and height 12 km. From this I want to find the boundary points with specified center
Asked
Active
Viewed 202 times
-1
-
Possible duplicate of [Calculate distance between 2 GPS coordinates](https://stackoverflow.com/questions/365826/calculate-distance-between-2-gps-coordinates) – Jan 10 '19 at 13:21
1 Answers
1
On option is to use the geometry library computeOffset method.
var height = 12000; // m
var width = 25000; // m
var center = new google.maps.LatLng(1.22222, 2.3333);
var bounds = new google.maps.LatLngBounds();
var top = google.maps.geometry.spherical.computeOffset(center, height / 2, 0);
var bottom = google.maps.geometry.spherical.computeOffset(center, height / 2, 180);
var left = google.maps.geometry.spherical.computeOffset(center, width / 2, -90);
var right = google.maps.geometry.spherical.computeOffset(center, width / 2, 90);
bounds.extend(top);
bounds.extend(left);
bounds.extend(bottom);
bounds.extend(right);
code snippet:
html,
body,
#map {
width: 100%;
height: 100%;
padding: 0px;
margin: 0px;
}
<div id="map"></div>
<script>
function initMap() {
var height = 12000; // m
var width = 25000; // m
var center = new google.maps.LatLng(1.22222, 2.3333);
var bounds = new google.maps.LatLngBounds();
var top = google.maps.geometry.spherical.computeOffset(center, height / 2, 0);
var bottom = google.maps.geometry.spherical.computeOffset(center, height / 2, 180);
var left = google.maps.geometry.spherical.computeOffset(center, width / 2, -90);
var right = google.maps.geometry.spherical.computeOffset(center, width / 2, 90);
bounds.extend(top);
bounds.extend(left);
bounds.extend(bottom);
bounds.extend(right);
var map = new google.maps.Map(document.getElementById('map'), {
center: center,
zoom: 2,
mapTypeId: 'terrain'
});
var centerMarker = new google.maps.Marker({
map: map,
position: center
})
var rect = new google.maps.Rectangle({
map: map,
bounds: bounds
});
map.fitBounds(bounds);
}
</script>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry&key=
AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap"></script>

geocodezip
- 158,664
- 13
- 220
- 245
-
How can I set the above as map boundary and go to the zoom level to make this as the boundary? – vishnu Jan 14 '19 at 13:06
-
That sounds like a different question (you need to dynamically size your map div to make that happen) – geocodezip Jan 14 '19 at 13:34