Google Maps API

36 views
Skip to first unread message

Kenneth

unread,
Mar 11, 2022, 2:30:12 PM3/11/22
to web2py-users
Hello,

has anybody used Google Maps after 2018, looks like you need an api key to use the geocode function. Geocode doesn't seam to be updated for this?


Kenneth

Rob Paire

unread,
Mar 16, 2022, 5:55:30 PM3/16/22
to web2py-users
Hi Kenneth,
I used to use Google, but switched to Mapquest for this kind of thing. Below is a bit of code I hope you find useful. You still need an API key but its easy to obtain, and there is no charge for light usage.

async function mapquest(){
    //This function is used to calculate Travel Time & Miles to Jobsite
    //https://developer.mapquest.com/forum/i-only-need-miles
    //There is an info.statusCode and info.messages node that will give details about any known failure. 
    {{#Embedded Python}}
    {{if cust_addr_rec.CITY and cust_addr_rec.STATE:}}    
            let url = 'https://www.mapquestapi.com/directions/v2/routematrix?json={"locations":["Richfield, PA","{{=cust_addr_rec.CITY}}, {{=cust_addr_rec.STATE}}"]}&outFormat=json&key='enter-your-api-key-here';
            let obj = null;
            try {
                obj = await (await fetch(url)).json();
                document.querySelector("#FLD_MILES").innerHTML = ('Miles:'+(obj.distance[1]).toFixed(0)+'  Hrs:'+(obj.time[1]/3600).toFixed(2));
                document.querySelector("#FLD_MILES").focus();
                document.querySelector("#FLD_MILES").blur();
            } catch(e) {
                document.querySelector("#FLD_MILES").innerHTML = ('error:'+e);
                console.log('error');
                console.log(e);
            }
     {{pass}}
}

-Rob
Reply all
Reply to author
Forward
0 new messages