地图-获取两点之间的距离(经纬度)
2016-09-21 15:43
549 查看
获取两点之间的距离,两个经纬度直接的距离
手机上网时,经常遇到当前某个地点离我现在的距离有多少米,在做项目时,有的是从接口中返回的值,有的只能自己写了,哈哈!!!例如:
代码如下:
function newCommon(){ //这段代码是网上搜的,但是忘记了那个网站 this.getDistanceOfJW = function(location1,location2){ function Rad(d){ return d * Math.PI / 180.0; } function GetDistance(lat1,lng1,lat2,lng2){ var radLat1 = Rad(lat1); var radLat2 = Number(Rad(lat2)); var a = Number(radLat1 - radLat2); var b = Rad(lng1) - Rad(lng2); var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2))); s = s *6378.137 ; s = Math.round(s * 10000) / 10; return s; } return GetDistance(location1["lat"],location1["lng"],location2["lat"],location2["lng"]) ; } this.getnums = function(num){ var intNum = num*10; intNum = parseInt(intNum); return intNum/10; } } var commoms=new newCommon(); //经纬度 需要一致 例如百度的 如果两者之间经纬度不同,需要转换 var option1 = {"lat":116.357889,"lng":39.961256}; var option2 = {"lat":116.432054,"lng":39.937803}; var getM=commoms.getDistanceOfJW(option1,option2), getMS; //判断 如果获取的数小于1000米就使用米,则使用千米 if(getM < 1000){ getMS=getM+'m'; }else{ getMS=commoms.getnums(getM/1000)+'km'; } console.log(getMS);
注:
经纬度需要一致,不同之间的经纬度需要转换
相关文章推荐
- 如何获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)
- 如何获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)
- 如何获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)
- 如何获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)
- 地图两点之间获取距离公式
- iOS百度地图根据经纬度获取两点之间的距离
- 高德地图设置中心点和缩放比例,获取两点之间距离
- 根据经纬度获取两点之间的距离(php&mysql)
- 如何获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)
- php获取两点经纬度之间的距离
- 如何获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)
- 已知用经纬度表示的两点,求两点之间的直线距离
- c# 根据经纬度 求两点之间的距离
- 根据经纬度测算两点之间的距离
- 计算两点经纬度之间的距离
- 【转】php获取两经纬度之间的距离
- 利用JS实现的根据经纬度计算地球上两点之间的距离
- 利用JS实现的根据经纬度计算地球上两点之间的距离
- 如何计算地图上两点之间的距离
- 怎么根据经维度计算两点之间的距离,怎么根据经纬度与距离计算目标地点的经纬度