-1

I'm using code to get distance between two points, it works fine but the problem is the code returns data with KM characters(e.g 56.0 km), so I want to use the pure number to get it in Mile or to use it for comparing with other values

 public function getdistance(Request $request)
 {
  $currentaddress =$request->currentadd;
  $from = '4429 North Broadway, Chicago, IL, United States';
  $remFrom = str_replace(',', '', $from); //Remove Commas
 $from = urlencode($remFrom);
$to = $currentaddress;
$remTo = str_replace(',', '', $to); //Remove Commas
  $to = urlencode($remTo);
 $data = file_get_contents("http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&language=en-EN&sensor=false");
  $data = json_decode($data,true);
 $distance = $data['rows'][0]['elements'][0]['distance']['text'];

  return response()->json([$distance]);
   }
Aken Roberts
  • 13,012
  • 3
  • 34
  • 40
  • You could multiply the result by `0.62137119` before you return it? – Tomasz Lloyd Jun 26 '18 at 17:07
  • I think you didn't get what I mean, I cant multiple it because there is km characters –  Jun 26 '18 at 17:09
  • [https://stackoverflow.com/questions/28536733/convert-latitude-and-longitude-into-kms](https://stackoverflow.com/questions/28536733/convert-latitude-and-longitude-into-kms) – Muhammad Usman Jun 26 '18 at 17:10

2 Answers2

2

You can specify a units parameter in the API URL to use imperial units (feet and miles) instead of metric. Note from the docs:

This unit system setting only affects the text displayed within distance fields. The distance fields also contain values which are always expressed in meters.

http://maps.googleapis.com/maps/api/distancematrix/json?origins=$from&destinations=$to&language=en-EN&sensor=false&units=imperial

Aken Roberts
  • 13,012
  • 3
  • 34
  • 40
  • I know, when I used this in pure php not in laravel I was able to multiple it like this $mile = $distance * 0.62137; but in laravel can't, don't know why and the result comes with km characters –  Jun 26 '18 at 17:13
  • I don't see anything Laravel-specific that would be affecting the result being in KM. Try comparing your vanilla PHP code to your Laravel code and see what's different -- perhaps you had the `units` specified earlier, or hit a different API URL. – Aken Roberts Jun 26 '18 at 17:17
0

Use $distance = $data['rows'][0]['elements'][0]['distance']['value'];, that will be the distance in meters as a number.

You can convert that to miles by dividing by 1609.34.

Response (for this request):

{
   "destination_addresses" : [ "New York, NY, USA" ],
   "origin_addresses" : [ "4429 North Broadway, Chicago, IL 60640, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "1,294 km",
                  "value" : 1293988    // <============ distance in meters
               },
               "duration" : {
                  "text" : "12 hours 24 mins",
                  "value" : 44618      // <============ duration in seconds
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}
geocodezip
  • 158,664
  • 13
  • 220
  • 245