I'm trying to find the ['locality', 'political'] value in the reverse geocoding Google Maps API.
I could achieve the same in Javascript as follows:
var formatted = data.results;
$.each(formatted, function(){
if(this.types.length!=0){
if(this.types[0]=='locality' && this.types[1]=='political'){
address_array = this.formatted_address.split(',');
}else{
//somefunction
}
}else{
//somefunction
}
});
Using Python, I tried the following:
url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng='+lat+','+long+'&result_type=locality&key='+MAPS_API_KEY
results = json.loads(urllib.request.urlopen(url).read().decode('utf-8'))
city_components = results['results'][0]
for c in results:
if c['types']:
if c['types'][0] == 'locality':
print(c['types'])
This gives me a bunch of errors. I'm not able to get my head around iterating through the response object to find the ['locality', 'political'] value to find the related City short_name. How do I go about solving this?
Str indices must be integererror.