Python Forum
How to retrieve locality from google maps API
Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to retrieve locality from google maps API
#1
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:

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)
below is the json on google maps website:
"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"
}
and the link for your reference also :https://developers.google.com/maps/documentation/geocoding/start
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with nested maps Unkovic 10 1,337 Nov-01-2023, 03:45 PM
Last Post: Unkovic
  how to import files in Google Collab wihout downloading them to PC and to Google Disk sveto4ka 9 3,862 Jun-03-2020, 12:40 PM
Last Post: sveto4ka
  Best way to map trajectory data on Google Maps Gduffley 1 2,662 Feb-05-2020, 12:36 AM
Last Post: scidam
  Can't visualize maps using Gmaps mPlummers 0 3,543 Sep-11-2019, 02:38 PM
Last Post: mPlummers
  Search "Places near by me" or "where am I" in google maps barry76 1 2,669 Feb-07-2019, 04:10 PM
Last Post: snippsat
  Non-Geographic Heat Maps JackValadez 0 2,084 Oct-17-2018, 06:03 PM
Last Post: JackValadez
  How get attributes of maps from loop statement LB_994 3 3,150 Aug-21-2018, 03:24 PM
Last Post: LB_994
  python charmap codec can't decode byte X in position Y character maps to < undefined> owais 9 39,080 Apr-28-2018, 10:52 PM
Last Post: abadawi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020