How to calculate distance from lat/long in php?

后端 未结 9 1400
余生分开走
余生分开走 2020-12-09 06:18

What I am trying to do is I have entries in the database which have a lat/long stored with them. I want to calculate the distance between users lat/long and entries lat/long

9条回答
  •  青春惊慌失措
    2020-12-09 06:48

    Use distance-matrix google api to calculate distance between latitude and longitude.

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, 
        'https://maps.googleapis.com/maps/api/distancematrix/json?origins='.$prev_add.'&destinations='.$curr_add.'&key=keyyouhavetogenerate'
    );
    $content = curl_exec($ch);
    $array = json_decode($content);
    $obj = json_decode($content, TRUE);
    
    $distance = $obj['rows'][0]['elements'][0]['distance']['text'];
    

    to use this api you will need key,to know more about how to generate key visit https://developers.google.com/maps/documentation/distance-matrix/intro

提交回复
热议问题