0

I want to get the lat long of 8 locations at a distance of x meters around a marker placed on google maps. Please refer to the image to get an idea.

Please see the image to get an idea

Basically I want to get an Array of 8 Locations(N, NE, E, SE, S, SW, W, NW) arounds the placed marker. These points can be of x meter from the marker.

Saadi
  • 1,292
  • 13
  • 22

1 Answers1

2

One option is to use the geometry library computeOffset method to compute positions at the desired distance from the center point every 45 degrees:

code snippet:

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var bounds = new google.maps.LatLngBounds();
  var centerPt = map.getCenter();
  var marker = new google.maps.Marker({
    position: centerPt,
    map: map
  })
  for (var angle = 0, i = 0; angle < 360; angle += 45, i++) {
    var marker = new google.maps.Marker({
      position: google.maps.geometry.spherical.computeOffset(centerPt, 100, angle),
      map: map,
      title: "marker #" + i + ", at " + angle + " degrees"
    })
    bounds.extend(marker.getPosition());
  }
  map.fitBounds(bounds);

}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>
geocodezip
  • 158,664
  • 13
  • 220
  • 245