Never been to DZone Snippets before?

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

GeoCode (See related posts)

//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;


You need to create an account or log in to post comments to this site.


Click here to browse all 7718 code snippets

Related Posts