DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

GeoCode

09.01.2010
| 2071 views |
  • submit to reddit
        //Mysql!

set @orig_lat=38.70032 ;
set @orig_lon=-9.38603;

/*Meters*/
set @dist=10;

set @mylat = @orig_lat;
set @mylon = @orig_lon;

/*1º = 69miles = 111Km or 111000 meters*/
set @unit = 111000;

set @lon1 = @mylon - @dist/abs(cos(radians(@mylat))*@unit);
set @lon2 = @mylon + @dist/abs(cos(radians(@mylat))*@unit);
set @lat1 = @mylat - (@dist/@unit);
set @lat2 = @mylat + (@dist/@unit);

SELECT 
  id, slug, latitude, longitude, councilId, @lon1, @lon2, @lat1, @lat2, @orig_lat, @orig_lon, @dist, @mylat, @mylon 
from 
  entity
where
    latitude
      between
        @lat1 AND @lat2
  AND
    longitude
      between
        @lon1 AND @lon2;