Get co-ordinates from a physical address

Hi. I’m trying to get longitude and latitude coordinates from a given address but I’m having issues with getting the coordinates. I used this website (Overview  |  Geocoding API  |  Google Developers) but I still have issues.![|624x443]

That’s my code. Hospital’s text is the address and TitleL is just a label to which I update it’s contents to the coordinates to see if my code is working. Any suggestions will be appreciated! Thanks!

Hi @shahdiva075w1eo, welcome to Thunkable!

Please see my tutorial video and post about how to ask for help with APIs:

One thing I mention is that it’s helpful if you post the URL you are using and also the full text of a sample JSON response.

I would be surprised if you could get a property from the JSON response that contains any text you type into a text input component. So that’s at least one thing that will need to change. That kind of information would usually be included in the URL itself.

The other thing to keep in mind when troubleshooting something like this is to put each data value into a separate label and make sure they are what you expect them to be. So that means checking the value of the longitude and latitude before worrying about whether or not they work in a Map component. And also doing error checking. My video explains that as well.

3 Likes

Hi. This really helped me understand JSON and how to use WEB API. So, thank you for that. But do you have any recommendations of which database for me to use if I want to find the coordinates of an address that the user inputs? By database I mean like the /v2/measurements that was used in the video. If there is no database available, could you please send a video of how to find a locationID of a given address? Thanks!

You’re welcome! What you’re wanting to do is called geocoding. It looks like you’re already working with the Google Maps Geocoding API. If you look at the sample request and response on that page, it should give you an idea about how to get started creating a url that works in a browser (always step 1 of working with an API!):

1 Like

Thanks for replying! I figured out the issue. Its with my API Key. I tried looking for some other API Keys but when I test them out by putting the complete URL in the URL section of Google, I get a message saying that the API key is invalid. I have right now hardcoded an address since I’m currently just trying to make to make it work with hardcoded addresses and will later go on to the address the user inputs. Do you have any suggestions with where to get a valid API KEY? Thanks so much for helping, I really appreciate it.

You don’t “look” for API keys. You register for them. Sometimes an API is free and other times (often), it’s a paid service.

2 Likes

Thank you so much for your time and patience! I got it work so that it marks the longitude and latitude of any given address! However, it marks any address. Like if I type 850, it actually marks an address. Is that something I can fix or would that just be the user’s fault in the end? Thank you so much for helping me out! You don’t know how much I was struggling on this!

2 Likes

So you’re saying that the Google Maps API returns a lat/long pair for anything you type in? If that’s true then there is probably a JSON property that has a value that indicates whether or not the address is valid.

Can you post the full text of the JSON response you get when you type in “850”?

Wow, you’re right. “850” returns a valid JSON response:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "424",
               "short_name" : "424",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Main Street",
               "short_name" : "Main St",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Ridgefield",
               "short_name" : "Ridgefield",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Fairfield County",
               "short_name" : "Fairfield County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Connecticut",
               "short_name" : "CT",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "06877",
               "short_name" : "06877",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "424 Main St, Ridgefield, CT 06877, USA",
         "geometry" : {
            "location" : {
               "lat" : 41.28204969999999,
               "lng" : -73.4982009
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 41.2833986802915,
                  "lng" : -73.49685191970849
               },
               "southwest" : {
                  "lat" : 41.2807007197085,
                  "lng" : -73.4995498802915
               }
            }
         },
         "place_id" : "ChIJp36vpyWqwokRtH0WoJarqY4",
         "plus_code" : {
            "compound_code" : "7GJ2+RP Ridgefield, CT, USA",
            "global_code" : "87H87GJ2+RP"
         },
         "types" : [ "establishment", "food", "point_of_interest", "restaurant" ]
      }
   ],
   "status" : "OK"
}

I’m not sure what to suggest. I did find these articles about geocoding & address validation:

1 Like