Google Map Distance Matrix

I have what I think is a small problem - in this example from Google Maps Distance Matrix (tell distance between two locations), I have put the HTMl elements in a container.content field, then the javascript code on the form code side. I can’t figure out where to put this code:

(per Distance Matrix Service  |  Maps JavaScript API  |  Google Developers):

<script async defer
src="https://maps.googleapis.com/maps/api/js? 
key=YOUR_API_KEY">
</script>

Thoughts? Here is a demo of the project -
googlemapsCindy.appstudio.zip (45.2 KB)

in this demo, I tried to just get the file that the src url above references and put it in the project. Didn’t work either - now I get this error about Over Query Limit, although when I run the HTML version of the demo I don’t get this error - works fine.

It needs to go into extraheaders in Project Properties.

Have you checked the Chrome console? There might be more information on the message from Google. Also, when you say it works on the HTML version - as opposed to what?

I took the Google example (an HTML page with javascript in it and ran it (not using appStudio) - worked nicely. It feels to me that the appStudio version can’t find the url that was in the tags (with the async).

Cindy

Cynthia L. Corritore, Ph.D.

Professor, Business Intelligence and Analytics
Business Intelligence & Analytics Department
Heider College of Business
Creighton University
Omaha, NE USA
402-612-1260

(Attachment google.html is missing)

You need the <script> tag in extraheaders.

OK- added to the extra header.
When run, I get this - the line it is referring to is the first one in the Init() function.

Added around the extra header. Ran the app - got this - line 91 is the first line of the Init() function.

Is this complete string in your extraheaders?

yes

<script>async defer src="https://maps.googleapis.com/maps/api/js?key=MyAPIKey&callback=initMap"</script>

Send me your project - let me have a look.

here you do - current project. googlemapsCindy.appstudio.zip (11.6 KB)

HTML version:
google.html.zip (2.0 KB)

Thanks - you don’t have the string in extraheaders right. It should look like you had it in your original message.

So, put the correct one in, and google will load.

@cindycc I flagged your most recent post because you left your google key in it… you may want to edit your post.

Also, if you don’t mind, please tell me why you need the distance stuff (I do a ton of work with lat/long and I may have easier ways to accomplish what you want).

Thanks. My students want to be able to tell the distance between two addresses.

Re: running app with corrected extra header: I did - still get the same errors. Attached is the one I am running. Project1.appstudio.zip (26.3 KB)

To break the process down. What you end up doing is what’s called “geocoding” and geocoding converts an address to latitude and longitude. Once you have the lat/lon of two addresses you can calculate the distance between them yourself (without paying google).

On mobile apps you’re typically calculating the distance between where you are and where something is. You would use the geocoding to get the lat/lon of the other piece and the users current location you can get via javascript. Here’s a link that has a far better explanation than anything I can type! :wink:

The following function will do the math in calculating distances for you.

  distance(lat1, lon1, lat2, lon2, unit)
  {
    //:::                                                                         :::
    //:::  Definitions:                                                           :::
    //:::    South latitudes are negative, east longitudes are positive           :::
    //:::                                                                         :::
    //:::  Passed to function:                                                    :::
    //:::    lat1, lon1 = Latitude and Longitude of point 1 (in decimal degrees)  :::
    //:::    lat2, lon2 = Latitude and Longitude of point 2 (in decimal degrees)  :::
    //:::    unit = the unit you desire for results                               :::
    //:::           where: 'M' is statute miles (default)                         :::
    //:::                  'K' is kilometers                                      :::
    //:::                  'N' is nautical miles                                  :::
    //:::                                                                         :::
    var radlat1 = Math.PI * lat1/180;
    var radlat2 = Math.PI * lat2/180;
    var theta = lon1-lon2;
    var radtheta = Math.PI * theta/180;
    var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
    dist = Math.acos(dist);
    dist = dist * 180/Math.PI;
    dist = dist * 60 * 1.1515;
    if (unit=="K") { dist = dist * 1.609344;}
    if (unit=="N") { dist = dist * 0.8684; }
    return dist;
  }
1 Like

Can you be specific? What errors are you seeing? (google should be there now)

I get the ‘can’t find google’ popup and same errors in the console (I removed some commented code so the error shows up on a different line).

Now that we have the math out of the way, the error that you’re encountering is quite common… especially with async defer.

When you load the google maps stuff it creates an object called “google” and if you try to do anything on/with that object before it’s created then you’ll get that error. You see this quite a bit in mobile apps that pull in code from a CDN (online code repository) when they don’t have online access.

What you always need to do is check if “google” is defined BEFORE you try to use it.

If, by chance, you’re using jquery you can load the google code dynamically.

  loadMapsApi()
  {
    // if not online or google maps are alreday loaded then just return 
    if (navigator.connection.type === Connection.NONE)
    {
        return;
    }
    if( typeof google === 'undefined' || typeof google.maps === 'undefined' )
    {
      // if online and maps not already loaded then load maps api
      $.getScript('https://maps.googleapis.com/maps/api/js?key=<API_KEY>&sensor=true&callback=maps.onMapsApiLoaded');
    }    
  }

You might have another look at your extraheaders. When I pasted the correct value in there, the problems with ‘google’ went away. Send me another copy of your app if you need a second set of eyes.