我是靠谱客的博主 踏实香菇,这篇文章主要介绍 百度地图的使用-定位—逆地理编码(即坐标转地址),现在分享给大家,希望可以做个参考。

先上效果:

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

效果

实现思路

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

clipboard.png

具体实现:

布局:

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="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>
复制代码
1
2
location_marker布局:三个控件
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="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. 停止拖动后,发起地理编码检索
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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)); } });

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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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) { } }); } };

三种动画的具体实现:

复制代码
1
需要一个变量纪录是否向上
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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

复制代码
1
新添加一个变量纪录是否第一次定位
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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; } } } }

最后

以上就是踏实香菇最近收集整理的关于 百度地图的使用-定位—逆地理编码(即坐标转地址)的全部内容,更多相关内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部