java百度地图地点检索_百度地图的使用-定位—逆地理编码(即坐标转地址)...

先上效果:

定位+拖动定位

定位动画

动画结束显示地址

7e2f55c000bb4b72d22dcfe139c5d5d1.gif

实现思路

中心点不变,在百度地图图层上覆盖自定义的定位布局

(TextView+ImageView+ImageView)

拖动地图时,隐藏地址显示,定位标示落下来后显示地址

拿到百度地图的拖动监听 setOnMapStatusChangeListener

拿到中心点经纬度,逆地理编码(即坐标转地址)mapStatus.target

ca852576d3525b7fbcc036a3b62be3fe.png

具体实现:

布局:

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

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">

android:id="@+id/bmapView"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:clickable="true" />

location_marker布局:三个控件

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">

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"/>

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"/>

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" />

拖动监听

创建地理编码检索实例;

创建地理编码检索监听者;

开始向上的动画

停止拖动后,发起地理编码检索

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;

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值