Geolocation – How Twitter Implements Geolocation Without User Permission

geolocation

Most sites I know of that use geolocation cause the browser to show a dialog asking the user permission to gather location info. This is like it can be seen here:

http://html5demos.com/geo and the way it works with Google Maps.

But Twitter seems to be able to do it another way. From twitter.com, when you compose a Tweet, you can click a little button, and it will add your location to your Tweet.

But in the case of the twitter implementation, there is no user interruption.

How is this done?

Best Answer

The twitter website seems to do a XHR request to https://twitter.com/account/geo_location.json which returns the user's current location as a json object. Since location detail is about city-level, I'd guess they are using the client's IP address to locate them using a IP-Locations database.

There are numerous commercial and free API's available to match IP addresses to approximate locations, such as http://ipinfodb.com/ip_location_api.php, if you want to do something similar.