Android 高德获取经纬度工具类

来源:互联网 发布:好用的吹风机知乎 编辑:程序博客网 时间:2024/06/06 17:33

public class GaoDeUtils {    //声明AMapLocationClientOption对象    public static AMapLocationClientOption mLocationOption = null;    private static AMapLocationClient mLocationClient =null;    private static double juli ;    public static void getJuLi(AMapLocationListener ap){        mLocationClient= new AMapLocationClient(MyApplication.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 = GetJuLiUtils.getDistance(118.924079                            , 42.249544, longitude, latitude);                    Log.e("定位",distance+"米");                }else{                                                       }            }        }    };


以上就能获取定位并且能够计算两地的距离了。

0 0