# google maps - Calculate distance between two lat long and show in meters/ km using php

Get the solution ↓↓↓I want to calculate distance between two latitude and longitude,if output distance less than km than show distance in meters like google map "190m"; as of now it is showing in miles

```
<?php
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if($miles <1km){
echo 'away from'.$miles .'meters';// distance in meters
}
else{
echo 'away from'$miles.'km';
}
}
echo distance('17.360527','78.473542','17.361012','78.473062','m');
?>
```

### Answer

#### Solution:

I Think This will Works For You :

```
<?php
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
// Convert Miles To KM
$dis_in_km = $miles * 1.609344;
if($dis_in_km < 1){
// Convert Miles to Miters
$dis_in_m = $miles * 1609.344;
echo 'away from '. round($dis_in_m,2).' meters';
}else{
echo 'away from '.round($dis_in_km,2).' km';
}
}
echo distance('17.360527','78.473542','17.361012','78.473062','m');
?>
```

**Share solution ↓**

### Additional Information:

**Date the issue was resolved:**

Link To Answer People are also looking for solutions of the problem: filter_sanitize_string deprecated

### Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

### Similar questions

Find the answer in similar questions on our website.

### Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.