I am capturing users current location lat long and comparing it with specific location lat long(location lat long stored in db) and comparing both, if user lat long is more than 200 meters show message you are outside 200 meters, but in output I am getting wrong value it is showing 122 km which is not correct.

Code:

function check_distance($lat1, $lat2, $long1, $long2) {
        $url = "https://maps.googleapis.com/maps/api/distancematrix/json?key=my_api_key" . $lat1 . "," . $long1 . "&destinations=" . $lat2 . "," . $long2 . "&mode=driving&language=en";
        $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);
        $dist_value = $response_a['rows'][0]['elements'][0]['distance']['value'];
        if($distance_value>200 meters){
        // show more than 200 meters from specific location 
        }
    }

echo check_distance($lat1, $lat2, $long1, $long2);
0

There are 0 best solutions below