Looking to extract specific information from a reverse geocode response using Google Maps JavaScript API v3.
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
}
}
The formatted address is displayed in the popup as expected, but I'm interested in retrieving other details like the street name or route. Despite attempts with JSON.parse(json);
, an error keeps popping up in the console:
SyntaxError: JSON.parse: unexpected character
In a PHP environment, I would utilize several for each
loops. Can a similar approach be taken in JavaScript?
Here's a snippet of sample data:
{
"results" : [
{
"address_components" : [
{
"long_name" : "131",
"short_name" : "131",
"types" : [ "street_number" ]
},
{
"long_name" : "Stubbington Avenue",
"short_name" : "Stubbington Ave",
"types" : [ "route" ]
},
...
],
"formatted_address" : "131 Stubbington Avenue, Portsmouth PO2, UK",
...
}
],
"status" : "OK"
}
Addtionally, here's a link to my developer page showcasing the full code.
To simplify, how can I isolate "Stubbington Avenue" from the provided dataset?