Pawan Kumawat

Calculate distance between 2 address using lat and long

According To me it’s one of important requirement in many projects related to address. To Calculate distance between two address using latitude and longitude use this method.

 

function LongLatDistance($Latitude1, $Longitude1, $Latitude2, $Longitude2) {
    $theta = $Longitude1 - $Longitude2;
    $miles = (sin(deg2rad($Latitude1))*sin(deg2rad($Latitude2)))+(cos(deg2rad($Latitude1))*cos(deg2rad($Latitude2)) * cos(deg2rad($theta)));
    $miles = acos($miles);
    $miles = rad2deg($miles);
    $result['miles'] = $miles * 60 * 1.1515;
    $result['feet'] = $result['miles'] * 5280;
    $result['yards'] = $result['feet'] / 3;
    $result['kilometers'] = $result['miles'] * 1.609344;
    $result['meters'] = $result['kilometers'] * 1000;
    return $result;
}
$distance = LongLatDistance($lat1='27.3766', $lon1='75.5580', $lat2='26.9124', $lon1='75.7873' );

var_dump($distance);
OUTPUT
array(5) {
    ["miles"]=>float(35.033155471693)
    ["feet"]=>float(184975.06089054)
    ["yards"]=>float(61658.35363018)
    ["kilometers"]=>float(56.380398559437)
    ["meters"]=>float(56380.398559437)
}

 

Enjoy.

Categories

Related Blogs

Add same product to cart twice instead of changing quantity in Cart

By default, WooCommerce allows customers to change the quantity of a product in the cart by updating the quantity input field on the cart page. If the quantity is increased, the product will be added to the cart only once with the updated quantity. However, if you want to allow customers to add the same product multiple times as separate items in the cart, you’ll need to customize the cart behavior.

Ultimate Guide to Custom Meta Boxes in WordPress

In WordPress, `add_meta_boxes` is an action hook that allows developers to add custom meta boxes to different post types’ edit screens. Meta boxes are additional sections on the post editor screen where you can add and display custom fields or other types of data related to the post.

Hide Products Based On Custom Field / ACF Value : WooCommerce

To hide products in WooCommerce based on a custom field or Advanced Custom Fields (ACF) value, you’ll need to use some custom code. Specifically, you’ll have to hook into the WooCommerce product query and modify it to include your custom field or ACF value as a condition for hiding products.