Geocoding: Get the street name from Google API
Hi!
I already manage to get a response from the google API.
{
"results" : [
{
"address_components" : [
{
"long_name" : "299",
"short_name" : "299",
"types" : [ "street_number" ]
},
{
"long_name" : "Sidney Street",
"short_name" : "Sidney St",
"types" : [ "route" ]
},
{
"long_name" : "Cambridgeport",
"short_name" : "Cambridgeport",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Cambridge",
"short_name" : "Cambridge",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Middlesex County",
"short_name" : "Middlesex County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Massachusetts",
"short_name" : "MA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "USA",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "02139",
"short_name" : "02139",
"types" : [ "postal_code" ]
},
{
"long_name" : "4825",
"short_name" : "4825",
"types" : [ "postal_code_suffix" ]
}
],
"formatted_address" : "299 Sidney St, Cambridge, MA 02139, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 42.355591,
"lng" : -71.1080628
},
"southwest" : {
"lat" : 42.3554331,
"lng" : -71.1082861
}
},
"location" : {
"lat" : 42.355512,
"lng" : -71.10815669999999
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 42.3568610302915,
"lng" : -71.1068254697085
},
"southwest" : {
"lat" : 42.3541630697085,
"lng" : -71.1095234302915
}
}
},
"partial_match" : true,
"place_id" : "ChIJt4pIW_t544kRaWhnaLrB61Q",
"types" : [ "premise" ]
}
],
"status" : "OK"
}
Unfortunately the order of the "address_components" are always different, depending on the address I am searching for. So what I am basically trying to do is search for the record where "types" equals "route" and then get the "long_name" for that. But there must be an easier solution, no?
1 reply
-
Hi Jan,
This is what I came up with...
let myObj := {results: [{address_components: [{long_name: "299",short_name: "299",types: ["street_number"]}, {long_name: "Sidney Street",short_name: "Sidney St",types: ["route"]}, {long_name: "Cambridgeport",short_name: "Cambridgeport",types: ["neighborhood", "political"]}, {long_name: "Cambridge",short_name: "Cambridge",types: ["locality", "political"]}, {long_name: "Middlesex County",short_name: "Middlesex County",types: ["administrative_area_level_2", "political"]}, {long_name: "Massachusetts",short_name: "MA",types: ["administrative_area_level_1", "political"]}, {long_name: "USA",short_name: "US",types: ["country", "political"]}, {long_name: "02139",short_name: "02139",types: ["postal_code"]}, {long_name: "4825",short_name: "4825",types: ["postal_code_suffix"]}],formatted_address: "299 Sidney St, Cambridge, MA 02139, USA",geometry: {bounds: {northeast: {lat: 42.355591,lng: -71.1080628},southwest: {lat: 42.3554331,lng: -71.1082861}},location: {lat: 42.355512,lng: -71.1081567},location_type: "ROOFTOP",viewport: {northeast: {lat: 42.3568610302915,lng: -71.1068254697085},southwest: {lat: 42.3541630697085,lng: -71.1095234302915}}},partial_match: true,place_id: "ChIJt4pIW_t544kRaWhnaLrB61Q",types: ["premise"]}],status: "OK"};let myCnt := 0;while item(myObj.results, 0).item(address_components, myCnt).item(types, 0) != "route" and myCnt < 15 domyCnt := myCnt + 1end;item(myObj.results, 0).item(address_components, myCnt).long_name
Content aside
- 4 yrs agoLast active
- 1Replies
- 344Views
