高德地图计算两地距离

   private AMapLocationClient mLocationClient = null;
    private AMapLocationListener mLocationListener = null;
    private double d1,d2,d3,d4;
    private LatLng latLng1,latLng2;

 //进行定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        mLocationListener = new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if (aMapLocation != null) {
                    if (aMapLocation.getErrorCode() == 0) {
                        // String p = aMapLocation.getProvince();
                        String c = aMapLocation.getCity();
                        String d = aMapLocation.getDistrict();
                        String s = aMapLocation.getStreet();
                        String n = aMapLocation.getStreetNum();
                        d1 = aMapLocation.getLatitude();
                        d2 = aMapLocation.getLongitude();
                        textView499.setText("当前位置:" + c + " " + d + " " + s + " " + n);
                        Log.e("tttt",d1+"");
                        Log.e("tttt",d2+"");
                        latLng1 = new LatLng(d1,d2);

                        chaxun();
                    } else {
                        Log.e("AmapError", "location Error, ErrCode:"
                                + aMapLocation.getErrorCode() + ", errInfo:"
                                + aMapLocation.getErrorInfo());
                    }
                }
            }
        };
        mLocationClient.setLocationListener(mLocationListener);
        mLocationClient.startLocation();

    }
   public void chaxun(){
       GeocodeSearch geocodeSearch = new GeocodeSearch(getApplicationContext());
       GeocodeQuery query = new GeocodeQuery("临沂", "linyi");
       geocodeSearch.getFromLocationNameAsyn(query);

       geocodeSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
           @Override
           public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {

           }
           @Override
           public void onGeocodeSearched(GeocodeResult result, int rCode) {
               if (rCode == 1000) {
                   if (result != null && result.getGeocodeAddressList() != null
                           && result.getGeocodeAddressList().size() > 0) {
                       GeocodeAddress address = result.getGeocodeAddressList().get(0);
                       String[] s = address.getLatLonPoint().toString().split(",");
                       Log.e("tttt",s[0]);
                       Log.e("tttt",s[1]);
                       Log.e("tttt",address.getFormatAddress());
                       latLng2 = new LatLng(Double.parseDouble(s[0]),Double.parseDouble(s[1]));
                       double distance = AMapUtils.calculateLineDistance(latLng1,latLng2);
                       Log.e("tttt",distance+"");
                       Log.e("tttt",distance/1000+"KM");
                   } else {
                   }
               }
           }
       });
   }
先定位目前的位置,再根据输入的目的地进行两地之间的距离计算。(前提是先导入高德地图相应的sdk包)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值