PHP calculates the distance between two longitude and latitude locations

Source: Internet
Author: User
Tags cos pow return sin

PHP calculates the distance between two specified longitude and latitude locations, which are used to compute the latitude and longitude of a given location, calculate the business area near it, and the distance between a given location and a nearby business district. The following are the specific function codes and usage examples.

about how to obtain the latitude and longitude of an address, can refer to this site article:

Google Map Third edition based on geographical location to obtain latitude and longitude method

<?php
/**
* Find the distance between two known latitude and longitude, Unit m
* @param lng1,lng2 Longitude
* @param lat1,lat2 latitude
* @return float distance, Unit m
* @author www. alixixi.com
**/
function getdistance ($lng 1, $lat 1, $lng 2, $lat 2) {
	//convert angle to Fox degree
	$radLat 1=deg2rad ($ LAT1); the//deg2rad () function converts the angle to radians
	$radLat 2=deg2rad ($lat 2);
	$radLng 1=deg2rad ($lng 1);
	$radLng 2=deg2rad ($lng 2);
	$a = $radLat 1-$radLat 2;
	$b = $radLng 1-$radLng 2;
	$s =2*asin (sqrt pow (sin ($a/2), 2) +cos ($radLat 1) *cos ($radLat 2) *pow (sin ($b/2), 2)) *6378.137*1000;
	return $s;
}

Here to calculate the "Shanghai Yan ' an west road 2055 Lane" to "Shanghai Jingansi" Distance:

2055, Yanan West Road, Shanghai, China latitude and longitude degree: 31.2014966,121.40233369999998

Shanghai Jingansi Warp Latitude: 31.22323799999999,121.44552099999998

The

echo getdistance (31.2014966,121.40233369999998,31.22323799999999,121.44552099999998);

The result is: 4970.4248747365, about 4970 meters, about 10 miles, I used to travel back and forth in this 2.1-line experience, should be more or less.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.