Toby Cronin
Toby Cronin

Reputation: 558

Google Maps API Rectangle

I'm building a web app that makes use of the Google Maps API and have run across a small math problem.

I have a center point in lat/lng and an area in kilometres. How would I calculate the NE and SW lat/lng points for a bounding box? Thanks.

Upvotes: 1

Views: 879

Answers (1)

bjornd
bjornd

Reputation: 22943

Consider using methods of Projection class http://code.google.com/apis/maps/documentation/javascript/reference.html#Projection to convert values from latang format to km and back.

I can suggest the following algo:

  1. Convert center point to km.
  2. Subtract a half of rectangle's width from x coord.
  3. Subtract a half of rectangle's height from y coord.
  4. Add a half of rectangle's width to x coord.
  5. Add a half of rectangle's height to y coord.
  6. You have two points now, just convert them to latlng.

Upvotes: 1

Related Questions