Jul-23-2018, 07:57 AM
I am searching list of projects from csv to find the lat long of the project using google maps api. Lat long i am getting are corrects but how to get locality from there json.
below is my code:
below is the json on google maps website:
and the link for your reference also :https://developers.google.com/maps/documentation/geocoding/start
below is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
mport requests import csv filename = "new.csv" f = open (filename, "r" ) data = f.read() lst = data.split( "\n" ) for i in lst: address = i api_key = "my-api-key" api_response = requests.get( 'https://maps.googleapis.com/maps/api/geocode/json?address={0}&key={1}' . format (address, api_key)) api_response_dict = api_response.json() if api_response_dict[ 'status' ] = = 'OK' : latitude = api_response_dict[ 'results' ][ 0 ][ 'geometry' ][ 'location' ][ 'lat' ] longitude = api_response_dict[ 'results' ][ 0 ][ 'geometry' ][ 'location' ][ 'lng' ] locality = api_response_dict[ "results" ][ 0 ][ "address_components" ][ "types" :[ "locality" , "political" ]] print (latitude, "," ,longitude, "," ,locality, "," , ">" ,i) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
"results" : [ { "address_components" : [ { "long_name" : "1600" , "short_name" : "1600" , "types" : [ "street_number" ] }, { "long_name" : "Amphitheatre Pkwy" , "short_name" : "Amphitheatre Pkwy" , "types" : [ "route" ] }, { "long_name" : "Mountain View" , "short_name" : "Mountain View" , "types" : [ "locality" , "political" ] }, { "long_name" : "Santa Clara County" , "short_name" : "Santa Clara County" , "types" : [ "administrative_area_level_2" , "political" ] }, { "long_name" : "California" , "short_name" : "CA" , "types" : [ "administrative_area_level_1" , "political" ] }, { "long_name" : "United States" , "short_name" : "US" , "types" : [ "country" , "political" ] }, { "long_name" : "94043" , "short_name" : "94043" , "types" : [ "postal_code" ] } ], "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA" , "geometry" : { "location" : { "lat" : 37.4224764 , "lng" : - 122.0842499 }, "location_type" : "ROOFTOP" , "viewport" : { "northeast" : { "lat" : 37.4238253802915 , "lng" : - 122.0829009197085 }, "southwest" : { "lat" : 37.4211274197085 , "lng" : - 122.0855988802915 } } }, "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA" , "types" : [ "street_address" ] } ], "status" : "OK" } |