Hi I need to calculate the distance of the first location to the second location using google MAP API but I am getting this Error [System.HttpResponse[Status=OK, StatusCode=200]]. I have tried the below code. please help me where I am doing wrong.

HttpRequest req = new HttpRequest();
Http http = new Http();
String url = 'https://maps.googleapis.com/maps/api/distancematrix/json' + '?origins=' + recordName.Checked_In_Location__Latitude__s,recordName.Checked_In_Location__Longitude__s + '&destinations=' + recordName.Checked_Out_Location__Latitude__s,recordName.Checked_Out_Location__Longitude__s + '&mode=driving' + '&sensor=false' + '&language=en' + '&units=imperial';
//I have also tried this but did not work.
//String url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&key=*API Name*';
HTTPResponse resp = http.send(req); 
system.debug('resp====>' + resp);

Please share with me any link(I have tried google itself) or reference from I can get help. Thanks in advance


You do not separate your latitude and longitude with coma, need to be: recordName.Checked_Out_Location__Latitude__s + ',' + recordName.Checked_Out_Location__Longitude__s

The same goes for destination location too.

  • Hey Mariia, Thanks for your quick reply. It worked...I got a response. could you please tell me why I am getting this error despite using a paid API - "error_message" : "You must use an API key to authenticate each request to Google Maps Platform APIs. For additional information, please refer to g.co/dev/maps-no-account", Mar 4 '20 at 16:29
  • Probably, you do not set authorization header in GET request, please, read the documentation Google provides. Mar 4 '20 at 18:18
  • Thanks, I solved it Mar 4 '20 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.