I use google maps api 3 to get city from coordinates. I read the ReverseGeocoding but I did not understand how to have the correct city value from this type of result: http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false
Asked
Active
Viewed 3.3k times
11
-
so, your question is "How to parse the JSON" ? – Raptor Jul 24 '13 at 09:58
-
yes! That is, ho I can parse the array with JS? – Tab Jul 24 '13 at 12:33
2 Answers
19
this Funktion returns the Name of a requested City at lat/long. As this Script is from end of 2012. Worked fine for me that time. Returns "unknown" when the API doesn't find any.
function get_api ($lat, $long) {
$get_API = "http://maps.googleapis.com/maps/api/geocode/json?latlng=";
$get_API .= round($lat,2).",";
$get_API .= round($long,2);
$jsonfile = file_get_contents($get_API.'&sensor=false');
$jsonarray = json_decode($jsonfile);
if (isset($jsonarray->results[1]->address_components[1]->long_name)) {
return($jsonarray->results[1]->address_components[1]->long_name);
}
else {
return('Unknown');
}
}
edit: and a jquery.
<p id="city"></p>
<script>
$(document).ready( function () {
// define lat / long
var lat = 37.42;
var long = -122.08;
$.ajax({
type: 'GET',
dataType: "json",
url: "http://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+long+"&sensor=false",
data: {},
success: function(data) {
$('#city').html(data);
$.each( data['results'],function(i, val) {
$.each( val['address_components'],function(i, val) {
if (val['types'] == "locality,political") {
if (val['long_name']!="") {
$('#city').html(val['long_name']);
}
else {
$('#city').html("unknown");
}
console.log(i+", " + val['long_name']);
console.log(i+", " + val['types']);
}
});
});
console.log('Success');
},
error: function () { console.log('error'); }
});
});
</script>

de_nuit
- 650
- 7
- 13
-
-
5This code assumes that the desired result is in the 2nd address_components entry of the second result, there is no guarantee that is (or will always be even if it works now) the desired result. You should really look for the result with the [type](https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingAddressTypes) of the data you are looking for (locality: indicates an incorporated city or town political entity), and return that. – geocodezip Jul 24 '13 at 13:29
-
since OP does not specify programming language, your PHP code may not be suitable for OP. *sidenote:* `return` is not a function, thus `return 'Unknown';` is more accurate – Raptor Jul 24 '13 at 13:43
-
[example of parsing out the "locality" (and the "sublocality") using the Google Maps Javascript API v3](http://www.geocodezip.com/v3_GoogleEx_geocoding-reverse2postcodeA.html). – geocodezip Jul 24 '13 at 13:48
-
jeah ur right. that why i added a date (2012) - the thing is that all scripts i found this time were working with a specified depth to look in the json. i werent able to use jquery for this and i knew where to look at (special case, sure). – de_nuit Jul 24 '13 at 14:18
-
In addition to `locality` and `sublocality`, depending on which countries you care about, you may need to also look out for `administrative_area_level_X` (e.g. `administrative_area_level_1` in Taiwan) and this may even change over time. – miguev Jul 08 '16 at 15:19
5
In HTML5 page we can get city name as follows:
<script src="//maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
<script>
(function() {
if(!!navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geocoder = new google.maps.Geocoder();
var geolocate = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
console.log(position.coords.latitude + ', ' + position.coords.longitude);
geocoder.geocode({'latLng': geolocate}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var result;
if (results.length > 1) {
result = results[1];
} else {
result = results[0];
}
//console.log(result);
console.log(result.address_components[2].long_name + ', ' + result.address_components[3].long_name);
}
});
});
</script>

San
- 666
- 7
- 27