12

I have a point (X,Y) and I want to create a square , Google maps LatLngBounds object so to make geocode requests bias only into this LatLngBound region.

How can I create such a LatLngBounds square with center the given point? I have to find the NE and SW point. But how can I find it given a distance d and a point (x,y)?

Thanks

Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147
maiky
  • 3,503
  • 7
  • 28
  • 28

3 Answers3

39

You can also getBounds from a radius defined as a circle and leave the trig to google.

new google.maps.Circle({center: latLng, radius: radius}).getBounds();

wprater
  • 1,022
  • 11
  • 21
  • 5
    Thank you! This worked for me: `var latLng = new google.maps.LatLng(32.79503, -117.24142);/*San Diego*/ var radius = 2000;/*meters*/ var circle = new google.maps.Circle({center: latLng, radius: radius}); var defaultBounds= circle.getBounds();` – Ryan Feb 09 '13 at 23:27
6

well that's very complicated. for a rough box try this:

if (typeof(Number.prototype.toRad) === "undefined") {
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  }
}

if (typeof(Number.prototype.toDeg) === "undefined") {
  Number.prototype.toDeg = function() {
    return this * 180 / Math.PI;
  }
}

var dest = function(lat,lng,brng, dist) {
    this._radius = 6371;
    dist = typeof(dist) == 'number' ? dist : typeof(dist) == 'string' && dist.trim() != '' ? +dist : NaN;
    dist = dist / this._radius;
    brng = brng.toRad();  
    var lat1 = lat.toRad(),
        lon1 = lng.toRad();

    var lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) + Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));
    var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) * Math.cos(lat1), Math.cos(dist) - Math.sin(lat1) * Math.sin(lat2));
    lon2 = (lon2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
    return (lat2.toDeg() + ' ' +  lon2.toDeg());
}

    var northEastCorner = dest(centreLAT,centreLNG,45,10);
    var southWestCorner = dest(centreLAT,centreLNG,225,10);

EDIT

The above was they way to do it way back in 2011 when I wrote it. These days the google maps api has come on a loooong way. The answer by @wprater is much neater and uses some of the newer api methods.

herostwist
  • 3,778
  • 1
  • 26
  • 34
  • 2
    Ok but explain a bit more some words about the code so the other viewers will be able to understand. what is brng variable and what it does and when inserting 10 as dist, it is kilometers? what does it mean when you put 45,10 for NE corner and 225, 10 for SW corner? – maiky Apr 20 '11 at 12:18
  • ok `brng` is the bearing or angle. so, using 45,10 we are calculating the location of a point 10km north east of your starting point. 225,10 will give you the south west location. these are the two points needed for a google bounds object. – herostwist Apr 20 '11 at 12:48
  • To those that recently down-voted my answer - check when it was made - Apr 2011, back then the solution using `google.maps.Circle` wasn't available. – herostwist Apr 03 '13 at 09:02
1

Wouldn't it work to simply add/subtract d/2 to your x/y locations?

Given x,y as the center point:

NW = x-(d/2),y-(d/2)
SE = x+(d/2),y+(d/2)

Don't trust me on this, though - I am terrible at math :)

This assumes d as a "diameter", rather than a radius. If "d" is the radius, don't bother with the divide-by-two part.

Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
Earl B
  • 45
  • 1
  • 5