Are most web and smartphone applications that show you how far other users are from you essentially based on a user's latitude and longitude? That is, do these apps basically derive a user's latitude and longitude from their country and postal code and then use an algorithm to create a sorted list of all users who are near them, closest first? I believe the answer is "yes" but I want to make sure before I build this feature into my Django application.
For those countries that don't use postal codes, I would imagine the latitude and longitude are derived from the city/region/country tuple they reside in.
Yes, they map IP to lat/long, and lat/long to city, and city to country, unless they have access to a GPS device/sensor for the exact position (+/- 1m).
The thing is, if you have an ip, you can convert that into a number (biginteger)
74.88.21.55 ==> (a.b.c.d) ==> d * 255^0 + c * 255^1 + b * 255^2 +a * 255^3 = x
then you can query a database.
All you need is a lookup-table:
T_Lookup
IP_Range_Start IP_Range_End Latitute Longitue
Then you can query like this:
SELECT Latitude, Longitude FROM T_Lookup
WHERE x BETWEEN IP_Range_Start AND IP_Range_End
Then you can calculate the distance between the two points using the haversine formula.
You can grab C code here:
http://aimbots.net/tutorials/7680-getting-country-ip-address.html
And an IP to country csv you find here:
http://ip-to-country.webhosting.info/node/view/6
Apps that display the locations of users relative to other users generally gather their data from either GPS data or IP address location data.
This Wiki article provides a nice explanation of geolocation.
They use the Galactic Coordinate system, which does in fact use longitude, l, and latitude, b.
http://en.wikipedia.org/wiki/Galactic_coordinate_system
Related
I have a .csv file with Twitter profiles including information such as username, name, description etc. One column is geolocation. In this text the user may have a country (i.e., UK), a city or town (i.e., Cambridge), an actual address (5 Tyrian Place, WR5 TY1), a state (i.e, California, CA) or something silly (i.e., West of Hell).
Is there an API/library/automatic way of taking this information and deriving the country? For example, if the location is Cambridge the output should be UK, if the address is in the UK, the output should be UK, etc.
Google has a reverse geocoding service which you can access through their Maps API:
https://developers.google.com/maps/documentation/geocoding/start
They let you make 2500 free requests per day. One nice feature is it will give you correct latitude, longitude, state, country, etc for things like "Golden Gate Bridge" and "The Big Apple." Twitter users enter all sorts of (sarcastic) phrases for their location -- like "West of Hell," "Mars," etc -- and Google will reverse geocode that as well. Though, that may not be very useful.
As another level of checking, you can compare the user's timezone ("utc_offset"), if it is present, to the place that Google returns. It's a bit involved and requires that you compare the timezone's latitude boundaries to the latitude and longitude in Google's response.
I'm working on a geolocation based personal project where I'd like to fetch the suppliers based on the user's latitude & longitude value. And the deal is suppliers have variable supply radius, few suppliers supply only within 5km of their radius while some may supply across the entire city.
The general way to go about this is for each supplier calculate the distance between the supplier & the user. If it is less than or equal to it's supply radius then display that supplier in the results.
But this might be very slow, so I thought I'd split the city into four zones(pick four latitude & longitude values from google maps for North East West South) & whenever a supplier is added I'll do the math & assign the zones to which they can supply in the database. Now whenever I get the user's latitude & longitude I'd determine the zone & fetch suppliers that can supply to that zone, do the distance calculation & filter them out. This way I do the calculation on less number of suppliers instead of the entire list.
But is it a good idea or can I do better ?
In you are using Postgres/Postgis, you can make use of spatial indexes, and then use ST_DWithin(geom1, geom2, distance) type queries see ST_DWithin docs. The spatial index will partition the space for you, making this kind of query very efficient and avoid you having to come up with any spatial partitioning scheme of your own.
Another operator you can use is the <-> operator, which is very efficient with a spatial index and is used in the order by clause, to get the nearest y things to some point x, (k nearest neighbour search) see <-> operator docs. One caveat for this operator to work properly with the index, the point you are searching for, needs to be a constant, as it sounds like it would be in your case.
If I have a latitude and longitude, how do I find out the country of that location..
If found out about Google Reverse Geocoding, but unfortunately, it requires the service to be used along with Google Maps, which is not my case..
Is there a static database or something which I can refer to? It would be better if I have a static database as opposed to a service..
Without a service, it is a lot of work.
First, you need the country polygons with assigned country codes. One country will have on average approx 50.000 vertices.
Then it's a simple point-in-polygon search.
Use a spatial index to limit the country polygon to search.
It becomes a bit more difficult if you have to consider enclaves.
You can use Bing Maps Api, it doesn't require displaying maps as far as I know,
http://dev.virtualearth.net/services/v1/geocodeservice/geocodeservice.asmx/ReverseGeocode?latitude=22.98&longitude=35.43637&key=[YOU_KEY]&culture=%22en-us%22&format=json
You can use the geonames.org "Country code / reverse geocoding" webservice, for example:
http://api.geonames.org/countryCode?lat=47.03&lng=10.2&username=demo
-> AT
I have stored users - including their addresses - and need to find all users who live within a certain distance of a specific location.
I am using geocoder and have stored longitude and latitude in the Users table.
How do I find these user?
To do a query based on lat/lon, do this:
User.near([39.41, 90.23], 10)
That will find users within 10 miles of the lat/lon pair.
Here are the docs: https://github.com/alexreisner/geocoder
I've got a latitude/longitude value.... How can I search and get the city?
If you're looking for free (as freedom) sources, you can use Geonames API findNearbyPlaceName.
For example the following returns nearest Placename:
http://api.geonames.org/findNearbyPlaceName?lat=47.3&lng=9&username=demo
More information is available here
http://www.geonames.org/export/web-services.html#findNearbyPlaceName
Another option is getting data from Freebase. Instead of single point it takes bounded box:
http://api.freebase.com/api/service/geosearch?location=[30.2,50.4,30.6,50.8]&location_type=/location/citytown&inside=true&indent=1
Using the google maps api, here is an example to get the address in XML format.
http://maps.google.com/maps/api/geocode/xml?latlng={latlng}&sensor={sensor}®ion={region}
Where latlng = 0,0 sensor = false, and region = country code, so for my old address it would be
http://maps.google.com/maps/api/geocode/xml?latlng=-43.893792,171.7592620&sensor=false®ion=nz
Then you can use that XML to get whatever details you need, including the City