nullox
nullox

Reputation: 395

shell script to get latitude and longitude of device's current location from google maps api

Is there a way to get the geolocation(lantitude and longtitude) of a server's current location via shell script from the server.

So the server calls an API in google maps and provide the IP information etc. to that API. The API will return the lan and long of the server's current location.

Upvotes: 0

Views: 1200

Answers (1)

hennes
hennes

Reputation: 9342

Not sure if it's officially supported in the Google Maps API. But there are numerous free web APIs that do exactly what you want. The following snippet uses curl and grep to query http://freegeoip.net/.

$ curl -s --get http://freegeoip.net/json/74.201.113.118 | grep -Po '(?<="latitude":)[^,}]*'
33.7516
$ curl -s --get http://freegeoip.net/json/74.201.113.118 | grep -Po '(?<="longitude":)[^,}]*'
-84.3915

Note, however, that those IP-based locations will not be very accurate. I guess you can count on the city being correct at most. It's not the same as locating a device via GPS.

Upvotes: 2

Related Questions