mravkata
mravkata

Reputation: 21

How to calculate google maps max and min Lat And Long

I work on web application using Google Maps JavaScript API.

And i have this problem:

When the user open the app it generates map with some custom markers. The markers are stored in Database ( SQL ) with latitude and longitude. I have the user location and radius lets say 10km.

I need to get from database only the markers that are 10km ( radius ) from the user.

So in my where clause i need specific values for lang. and long. borders.

Is there some Google API function or other method to calculate that.

The example:

I have this SQL struct: table Markers

CODE                int         
LOCATION_LATITUDE   float           
LOCATION_LONGITUDE  float   

Now in JS i generate map with center = user location.

And now i want to add only markers from table Markers that are in 10km radius of the map center( user location ) In table Markers i have markers from all around the globe.

Upvotes: 0

Views: 1257

Answers (1)

mravkata
mravkata

Reputation: 21

Ok. I found the solution. -> Haversine formula

Let's say that point with coordinates ( 37, -122 ) is the center of the map. So, the where statement will be

SELECT code FROM MARKERS WHERE ( 6371 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) < 10

Where 6371 is the Earth’s radius in km ( 3959 in miles ), and 10 is radius of the search area.

Upvotes: 1

Related Questions