Google Maps API with PHP to find distance between two locations

前端 未结 3 1507
礼貌的吻别
礼貌的吻别 2021-01-07 05:23

On my current project, which is a delivery system, I have a list of available delivery drivers, which is shown on an orders page. But what I need is to show the distance eac

相关标签:
3条回答
  • 2021-01-07 05:40

    You can easily calculates the distance between two address using Google Maps API and PHP.

    $addressFrom = 'Insert from address';
    $addressTo = 'Insert to address';
    $distance = getDistance($addressFrom, $addressTo, "K");
    echo $distance;
    

    You can get the getDistance() function codes from here - http://www.codexworld.com/distance-between-two-addresses-google-maps-api-php/

    0 讨论(0)
  • 2021-01-07 05:41

    this will out seconds between 2 location

    $origin =urlencode('Optus Sydney International Airport, Airport Drive, Mascot NSW 2020, Australia');
    $destination = urlencode('Sydney NSW, Australia');
    $url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=$origin&destinations=$destination&key=your_key";
    $data = @file_get_contents($url);
    $data = json_decode($data,true);
    echo $data['rows'][0]['elements'][0]['duration']['value'];
    
    0 讨论(0)
  • 2021-01-07 05:58

    Assuming that you want driving distance and not straight line distance, you can use the Directions web service: You need to make an http or https request from your PHP script and the parse the JSON or XML response.

    Documentation: https://developers.google.com/maps/documentation/directions/

    For example: Boston,MA to Concord,MA via Charlestown,MA and Lexington,MA (JSON) http://maps.googleapis.com/maps/api/directions/json?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|Lexington,MA&sensor=false

    Boston,MA to Concord,MA via Charlestown,MA and Lexington,MA (XML) http://maps.googleapis.com/maps/api/directions/xml?origin=Boston,MA&destination=Concord,MA&waypoints=Charlestown,MA|Lexington,MA&sensor=false

    Note that there are usage limits.

    0 讨论(0)
提交回复
热议问题