HTML5 获取用户经纬度及距离计算公式
发布时间:01/15 来源: 浏览:
关键词:
HTML5 获取用户经纬度及距离计算公式估计初次使用这个功能的都不,其实我们可以使用百度地址插件即可实现,也可以不用了,具体如下。
在做项目时需要做定位获取经纬度并计算距离,这里使用 HTML5 获取经纬度。
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>利用html5获取经纬度</title> </head> <body> <span id="support">将下面的经纬度输入谷歌地图:</span> <div id="show"> 纬度:<span id="latitude"></span><br /> 经度:<span id="longitude"></span><br /> 经纬度准确度:<span id="accuracy"></span><br /> 海拔:<span id="altitude"></span><br /> 海拔高度的精确度:<span id="altitudeAcuracy"></span><br /> 朝向:<span id="heading"></span><br /> 速度:<span id="speed"></span> </div> <script type="text/javascript"> var doc = document, latitude = doc.getElementById('latitude'), longitude = doc.getElementById('longitude'), accuracy = doc.getElementById('accuracy'), altitude = doc.getElementById('altitude'), altitudeAcuracy = doc.getElementById('altitudeAcuracy'), heading = doc.getElementById('heading'), speed = doc.getElementById('speed'), support = doc.getElementById('support'), showDiv = doc.getElementById('show'); function lodeSupport(){ if(navigator.geolocation){ support.innerHTML = '将下面的经纬度输入谷歌地图(纬度 经度)查看自己位置:'; showDiv.style.display = 'block'; navigator.geolocation.getCurrentPosition(updataPosition,showError); }else{ support.innerHTML = '对不起,浏览器不支持!'; showDiv.style.display = 'none'; } } function updataPosition(position){ var latitudeP = position.coords.latitude, longitudeP = position.coords.longitude, accuracyP = position.coords.accuracy, altitudeP = position.coords.altitude, altitudeAcuracyP = position.coords.altitudeAcuracy, headingP = position.coords.heading, speedP = position.coords.speed; latitude.innerHTML = latitudeP; longitude.innerHTML = longitudeP; accuracy.innerHTML = accuracyP; altitude.innerHTML = altitudeP; altitudeAcuracy.innerHTML = altitudeAcuracyP; heading.innerHTML = headingP; speed.innerHTML = speedP; } function showError(error) { switch(error.code) { case error.PERMISSION_DENIED: showDiv.innerHTML="用户拒绝访问地理位置" break; case error.POSITION_UNAVAILABLE: showDiv.innerHTML="地理位置信息无法获取" break; case error.TIMEOUT: showDiv.innerHTML="获取位置时间超时" break; case error.UNKNOWN_ERROR: showDiv.innerHTML="我擦,这是一个未知的错误" break; } } window.addEventListener('load', lodeSupport , true); </script> </body> </html>
两个经纬度之间距离计算公式
/** * @desc 根据两点间的经纬度计算距离 * @param float $lat 纬度值 * @param float $lng 经度值 */ public function getDistance($lat1, $lng1, $lat2, $lng2) { $earthRadius = 6367000; $lat1 = ($lat1 * pi() ) / 180; $lng1 = ($lng1 * pi() ) / 180; $lat2 = ($lat2 * pi() ) / 180; $lng2 = ($lng2 * pi() ) / 180; $calcLongitude = $lng2 - $lng1; $calcLatitude = $lat2 - $lat1; $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2); $stepTwo = 2 * asin(min(1, sqrt($stepOne))); $calculatedDistance = $earthRadius * $stepTwo; return round($calculatedDistance); }