0

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();
req.setMethod('GET');
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*';
req.setEndPoint(url);
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

2

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.

| improve this answer | |
  • 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", – Nitish Kumar Mar 4 at 16:29
  • Probably, you do not set authorization header in GET request, please, read the documentation Google provides. – Mariia Illarionova Mar 4 at 18:18
  • Thanks, I solved it – Nitish Kumar Mar 4 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.