TransWikia.com

Return geocode information with Carto

Geographic Information Systems Asked by Colton Shaw on May 8, 2021

I’m using the Carto API for a few functions and I’m looking for a way to return geocoded information similar to how Google does it here. With Google, you make a call with the Address like: https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY and it returns a JSON like:

{
   "results" : [
      {
         "address_components" : [
            ...
         ],
         "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4267861,
               "lng" : -122.0806032
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4281350802915,
                  "lng" : -122.0792542197085
               },
               "southwest" : {
                  "lat" : 37.4254371197085,
                  "lng" : -122.0819521802915
               }
            }
         },
         "place_id" : "ChIJtYuu0V25j4ARwu5e4wwRYgE",
         "plus_code" : {
            "compound_code" : "CWC8+R3 Mountain View, California, United States",
            "global_code" : "849VCWC8+R3"
         },
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

I’d like to do the same with Carto, but I’m only finding how to parse the information through a SQL Update, but not return it on a GET call.

One Answer

You can use a SELECT statement instead of an UPDATE. This API call...

https://USER_NAME.carto.com/api/v2/sql?q=select * from cdb_geocode_street_point('Gran Via 46', 'Madrid', 'Madrid', 'Spain')&api_key=API_KEY

...returns:

{
    "rows": [
        {
            "cdb_geocode_street_point": "0101000020E61000002F34D769A4A50DC0C425C79DD2354440"
        }
    ],
    "time": 0.531,
    "fields": {
        "cdb_geocode_street_point": {
            "type": "geometry",
            "dims": 4,
            "srid": -1
        }
    },
    "total_rows": 1
}

So if you want to get a more human-friendly response (instead of a wkb geometry) you could change the query using ST_AsText or a combination of ST_X/ST_Y. Bear in mind that Google Maps API gives you more metadata or Places information too.

Answered by ramiroaznar on May 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP