Get my computer's current city and state from terminal How can I get my computer's location to the granularity of city and  country using the command-line?

I know I could get the IP of the correct network interface and do an IP lookup but I wonder if the system has a quicker way to access this. The system clock has the location so I am wondering if it's possible to get it that way.
Command line tool

I've made quick modification to an excellent robmathers app WhereAmI. You can find it on my github.

Once You compile it and run, that's the output:

Latitude: 40.714232
Longitude: -73.9612889
Accuracy (m): 65.000000
Timestamp: 30.04.2015, 11:37:45 CEST
Location: 277 Bedford Avenue, Brooklyn, NY 11211, United States


If You want only the city and the country - changes in parsing json should be made (or grep the output). Sample json data can be found here.

Note: This is very quick implementation, but there shouldn't be any errors

Info from preferences

You can get info from preferences by running:

defaults read /Library/Preferences/.GlobalPreferences.plist com.apple.preferences.timezone.selected_city


You'll need to parse the output, because it looks like:

{
CountryCode = PL;
GeonameID = 3081368;
Latitude = "51.1";
LocalizedNames =     {
    pl = "Wroc\\U0142aw";
};
Longitude = "17.03333";
Name = "Wroc\\U0142aw";
Population = 634893;
TimeZoneName = "Europe/Warsaw";
Version = 1;
}