美文网首页
就要简单,高德安卓获取经纬度和计算两地距离

就要简单,高德安卓获取经纬度和计算两地距离

作者: 阿敏其人 | 来源:发表于2019-08-03 14:26 被阅读0次

    不要复杂,只想简简单单用个工具类,获取经纬度。
    然后,计算下两地距离。

    获取经纬度的工具类

    public class AmapUtils {
        //声明AMapLocationClientOption对象
        public static AMapLocationClientOption mLocationOption = null;
        private static AMapLocationClient mLocationClient =null;
        private static double juli ;
    
        //获取经纬度
        public static void getLatlon(AMapLocationListener ap){
    
            mLocationClient= new AMapLocationClient(NimApplication.getContext());
            mLocationClient.setLocationListener(ap);
            //初始化AMapLocationClientOption对象
            mLocationOption = new AMapLocationClientOption();
            //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
            mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //单次定位
            mLocationOption.setOnceLocationLatest(true);
            //设置是否返回地址信息(默认返回地址信息)
            mLocationOption.setNeedAddress(true);
            //设置是否强制刷新WIFI,默认为true,强制刷新。
            mLocationOption.setWifiActiveScan(false);
            //设置是否允许模拟位置,默认为false,不允许模拟位置
            mLocationOption.setMockEnable(false);
            //单位是毫秒,默认30000毫秒,建议超时时间不要低于8000毫秒。
            mLocationOption.setHttpTimeOut(20000);
            //关闭缓存机制
            mLocationOption.setLocationCacheEnable(false);
            //给定位客户端对象设置定位参数
            mLocationClient.setLocationOption(mLocationOption);
            //启动定位
            mLocationClient.startLocation();
    
        }
    
        /*private AMapLocationListener mLocationListener = new AMapLocationListener(){
    
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if(aMapLocation!=null){
                    if(aMapLocation.getErrorCode()==0){
                        int locationType = aMapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
                        double latitude = aMapLocation.getLatitude();//获取纬度
                        double longitude = aMapLocation.getLongitude();//获取经度
                        float accuracy = aMapLocation.getAccuracy();//获取精度信息
                        Log.e("定位",locationType+" +" +latitude+"+"+longitude+"+"+accuracy);
                        double distance = GetDistanceUtils.getDistance(118.924079
                                , 42.249544, longitude, latitude);
                        Log.e("定位",distance+"米");
                    }else{
    
    
                    }
                }
            }
        };*/
    }
    

    activity中使用

            AmapUtils.getLatlon(new AMapLocationListener() {
                @Override
                public void onLocationChanged(AMapLocation aMapLocation) {
                    if(aMapLocation!=null){
                        if(aMapLocation.getErrorCode()==0){
                            int locationType = aMapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
                            double latitude = aMapLocation.getLatitude();//获取纬度
                            double longitude = aMapLocation.getLongitude();//获取经度
                            float accuracy = aMapLocation.getAccuracy();//获取精度信息
    
                            Log.e("高德定位经纬度",locationType+" + " +latitude+" + "+longitude+" + "+accuracy);
                            Log.e("高德定位经纬度","当前经度:"+longitude);
                            Log.e("高德定位经纬度","当前纬度:"+latitude);
                            /*double distance = GetDistanceUtils.getDistance(118.924079
                                    , 42.249544, longitude, latitude);
                            Log.e("定位",distance+"米");*/
                        }else{
    
    
                        }
                    }
                }
            });
    

    相关的计算两地距离的工具类

    public class AmapDistanceUtils {
        private static final double EARTH_RADIUS = 6378137.0;
    
        public static double getDistance(double longitude, double latitue, double longitude2, double latitue2) {
            double lat1 = rad(latitue);
            double lat2 = rad(latitue2);
            double a = lat1 - lat2;
            double b = rad(longitude) - rad(longitude2);
            double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
            s = s * EARTH_RADIUS;
            s = Math.round(s * 10000) / 10000;
            return s;
        }
    
        private static double rad(double d) {
            return d * Math.PI / 180.0;
        }
    }
    

    .
    .
    .

    本文完。

    参考:https://blog.csdn.net/qq939782569/article/details/53994823

    相关文章

      网友评论

          本文标题:就要简单,高德安卓获取经纬度和计算两地距离

          本文链接:https://www.haomeiwen.com/subject/yqyjdctx.html