Get driving distance between two points using Google Maps API

后端 未结 5 2170
Happy的楠姐
Happy的楠姐 2020-12-12 18:53

I\'m trying to get driving distance between two points using Google Maps API. Now, I have code which get direct distance:

This function get lat and

5条回答
  •  时光取名叫无心
    2020-12-12 19:36

    This is the method I got. In it, you can enter any manual distance from sessions or database.

    $origin      = urlencode($_SESSION['source1']);
    $destination = urlencode($_SESSION['dest1']);
    
    $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=$origin&destinations=$destination&key=Your api key";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    curl_close($ch);
    $response_a = json_decode($response, true);
    ?>
    

    Now the tough part was how to show distance. Well, with research, I got this answer:

    You can print this result anywhere. It's working for me.

提交回复
热议问题