百度地图的使用-定位—逆地理编码(即坐标转地址)

发布时间:2019-06-29 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了百度地图的使用-定位—逆地理编码(即坐标转地址)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

先上效果:

  1. 定位+拖动定位
  2. 定位动画
  3. 动画结束显示地址

效果

实现思路

  1. 中心点不变,在百度地图图层上覆盖自定义的定位布局
    (TextView+ImageView+ImageView)
  2. 拖动地图时,隐藏地址显示,定位标示落下来后显示地址
  3. 拿到百度地图的拖动监听 setOnMapstatusChangeListener
  4. 拿到中心点经纬度,逆地理编码(即坐标转地址)mapStatus.target

clipboard.png

具体实现:

布局:

在主界面布局上覆盖自己定位用的布局location_marker

<?XMl version="1.0" encoding="utf-8"?>
<andROId.support.constraint.ConstraintLayout >"http://schemas.android.COM/apk/res/android"
    >"http://schemas.android.com/apk/res-auto"
    >"http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivITy">

    <com.baidu.mapapi.map.Mapview
        android:id="@+id/bmapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true" />
    <include layout="@layout/location_marker"/>

</android.support.constraint.ConstraintLayout>
location_marker布局:三个控件
<?xML version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    >"http://schemas.android.com/apk/res/android"
    >"http://schemas.android.com/apk/res-auto"
    >"http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/iv_shadow"
        android:layout_width="22dp"
        android:layout_height="4dp"
        android:layout_centerHorizontal="true"
        android:src="@drawable/location_shadow"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <ImageView
        android:id="@+id/iv_location"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:src="@drawable/location_ic_select"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@id/iv_shadow"/>

    <TextView
        android:padding="2dp"
        android:background="@drawable/Shape_buttn_text"
        android:id="@+id/tv_describe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/iv_location"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


</android.support.constraint.ConstraintLayout>

拖动监听

  1. 创建地理编码检索实例;
  2. 创建地理编码检索监听者;
  3. 开始向上的动画
  4. 停止拖动后,发起地理编码检索
mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {
            @override
            public void onMapStatusChangeStart(MapStatus mapStatus) {
                mSeArch = GeoCoder.newInstance();
                mSearch.setOnGetGeoCodeResultListener(listener);
                startUpAnimation(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        //动画开始时,隐藏地址显示
                        tv_describe.setVisibility(View.INVISIBLE);
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });

            }

            @Override
            public void onMapStatusChangeStart(MapStatus mapStatus, int i) {

            }

            @Override
            public void onMapStatusChange(MapStatus mapStatus) {

            }

            @Override
            public void onMapStatusChangeFinish(MapStatus mapStatus) {

                mSearch.reverseGeoCode(new ReverseGeoCodeOption()
                        .location(mapStatus.target));



            }
        });

地理位置编码检索监听实现:

OnGetGeoCoderResultListener listener = new OnGetGeoCoderResultListener() {

        public void onGetGeoCodeResult(GeoCodeResult result) {

            if (result == null || result.error != Searchresult.ERRORNO.NO_ERROR) {
                //没有检索到结果
            }

            //获取地理编码结果
        }

        @Override

        public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {

            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                //没有找到检索结果
                Toast.makeText(MainActivity.this,"没有找到检索结果",Toast.LENGTH_SHORT).show();
            }

            //获取反向地理编码结果
            String address = result.getAddress();
            System.out.PRintln(address+"---------");
            tv_describe.setText(address);
            startDownAnimation(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    tv_describe.setVisibility(View.VISIBLE);
                    bounce();
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

        }
    };

三种动画的具体实现:

需要一个变量纪录是否向上
    private boolean isUp = false;

    /**
     * 向上移动动画
     */
    public void startUpAnimation(Animation.AnimationListener listener){
        if (isUp){
            return;
        }
        Animation animation = new TranslateAnimation(
                0,
                0,
                0  ,
                - 80);
        animation.setDuration(500);
        animation.setFillAfter(true);//设置为true,动画转化结束后被应用
        animation.setInterpolator(new AccelerateDecelerateInterpolator());
        iv_location.startAnimation(animation);//开始动画
        if(listener != null){
            animation.setAnimationListener(listener);
        }

        isUp = true;
    }

    /**
     * 向下移动动画
     */
    public void startDownAnimation(Animation.AnimationListener listener){
        if(isUp){
            Animation animation = new TranslateAnimation(
                    0,
                    0,
                    -80,
                    0);
            animation.setDuration(500);
            animation.setFillAfter(true);//设置为true,动画转化结束后被应用
            animation.setInterpolator(new AccelerateInterpolator(15));
            if(listener != null){
                animation.setAnimationListener(listener);
            }
            iv_location.startAnimation(animation);//开始动画
            isUp = false;
        }
    }

    /**
     * 弹跳动画
     */
    public  void bounce() {
        if (iv_location.getVisibility() == View.VISIBLE) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(iv_location, "translationY", 0, -30, 0);
            animator.setInterpolator(new EasingInterpolator(EasingInterpolator.Ease.ELASTIC_IN_OUT));

            animator.setDuration(1000);
            animator.setRepeatMode(ValueAnimator.REVERSE);
            animator.start();
        }
    }

定位方面参考之前文章,更新一下之前写的MyLocationListener

新添加一个变量纪录是否第一次定位
private boolean isFirst = true;
    class MyLocationListener extends BDAbstractLocationListener {


        @Override
        public void onReceiveLocation(BDLocation bdLocation) {

            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(bdLocation.getRadius())
                    // 此处设置开发者获取到的方向信息,顺时针0-360
                    .direction(bdLocation.getDirection()).latitude(bdLocation.getLatitude())
                    .longitude(bdLocation.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);

            String addr = bdLocation.getAddrStr();    //获取详细地址信息
            String country = bdLocation.getCountry();    //获取国家
            String province = bdLocation.getProvince();    //获取省份
            String city = bdLocation.getCity();    //获取城市
            String district = bdLocation.getDistrict();    //获取区县
            String street = bdLocation.getStreet();    //获取街道信息
//            showMyLocate(locData);

            if(isFirst){
                // 开始移动百度地图的定位地点到中心位置
                LatLng ll = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
                MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll,16);
                mBaiduMap.aniMATEMapStatus(u);
                isFirst = false;
            }

        }
    }
}

脚本宝典总结

以上是脚本宝典为你收集整理的百度地图的使用-定位—逆地理编码(即坐标转地址)全部内容,希望文章能够帮你解决百度地图的使用-定位—逆地理编码(即坐标转地址)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。