Home > database >  Resolve complete address using Google Maps SDK without UI
Resolve complete address using Google Maps SDK without UI

Time:11-01

I have a list of addresses, not formatted, and not necessarily written correctly.

I would like to iterate on these broken addresses strings, and receive a more structured and complete addresses using one of Google Maps SDKs

basically I have 2 questions:

  1. which SDK would be best for this task? (there is a list of 40)
  2. how can I use it without UI? (all the solutions I saw included UI and a search box)

CodePudding user response:

If you're actually using google-maps SDK, you're probably looking for the geocoding API

It can be used without UI as it can also be fetch as json via this endpoint:

https://maps.googleapis.com/maps/api/geocode/outputFormat?parameters

exemple:

https://maps.googleapis.com/maps/api/geocode/json?address=1600 Amphitheatre Parkway,
 Mountain View, CA&key=YOUR_API_KEY

or via JS api:

geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': 'Mountain View, CA'}, function(results, status) {
      if (status == 'OK') {
        console.log(results);
      } else {
        alert('Geocode was not successful for the following reason: '   status);
      }
    });

results:

{
   "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",
         "plus_code": {
            "compound_code": "CWC8 W5 Mountain View, California, United States",
            "global_code": "849VCWC8 W5"
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}
  • Related