Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
v:2.0:shipping:custom_code:google_maps_distance [2020/06/11 22:20] – [Generate Google Maps Platform API Key] marijav:2.0:shipping:custom_code:google_maps_distance [2020/12/09 20:44] (current) – [Build custom shipping code] adam
Line 56: Line 56:
    
 // Get address coordinates from Google Maps // Get address coordinates from Google Maps
-const maps_url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=' + origin_address + '&destinations=' + customer_address + '&units=' + unit_type + '&key=' + google_maps_api_key;+const maps_url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins='encodeURIComponent(origin_address+ '&destinations='encodeURIComponent(customer_address+ '&units=' + unit_type + '&key=' + google_maps_api_key;
 let distance = -1; let distance = -1;
 await request(maps_url, function (error, response, body) { await request(maps_url, function (error, response, body) {
Line 82: Line 82:
   - If you need Imperial measurements rather than Metric, update the ''unit_type'' string to be ''%%'imperial'%%'' instead.   - If you need Imperial measurements rather than Metric, update the ''unit_type'' string to be ''%%'imperial'%%'' instead.
   - Google returns the distance value in metres in their API. The ''convert_distance'' variable allows you to set whether this should be converted to kilometres/miles (based on what you've set for ''unit_type''). Set to false to leave it in metres.   - Google returns the distance value in metres in their API. The ''convert_distance'' variable allows you to set whether this should be converted to kilometres/miles (based on what you've set for ''unit_type''). Set to false to leave it in metres.
-  - Next, you'll need to set the shipping rate and error that is displayed to customers. The snippet above sets a rate that is $5 plus 45¢ per mile/km, but you'll obviously want to customise that to show your own rates. Check out [[..:shipping:custom_code:|the documentation here on interacting with our rates]].\\ \\ Specifically you'll be editing this section:<code javascript>//Begin Custom Logic+  - Next, you'll need to set the shipping rate and error that is displayed to customers. The snippet above sets a rate that is $5 plus 45¢ per mile/km, but you'll obviously want to customise that to show your own rates. Check out [[..:custom_code:|the documentation here on interacting with our rates]].\\ \\ Specifically you'll be editing this section:<code javascript>//Begin Custom Logic
 if (distance > -1) { if (distance > -1) {
     // Distance was calculated     // Distance was calculated

Site Tools