如何使用百度地图实现任意定位

如何使用百度地图实现任意定位

在上篇博文中讲到了如何实现当前定位,链接地址:
https://blog.csdn.net/qq_31236027/article/details/104200700

那么接下来来讲讲如何实现任意的定位

首先我们都知道了BDAbstractListener是一个很重要的定位抽象类,它只有一个onReceiveLocation方法但是这个方法足以用来进行任意定位。

其次,要想进行任意定位我们需要用到Latlng这个类来封装经纬度此外我们还需要重写BDAbstractListener的构造方法并将latlng的实例传进来来进行定位
代码如下

 ...
 public class MyLocationListener extends BDAbstractLocationListener {
      LatLng latLng1;
      public MyLocationListener(LatLng latLng1){
         super();
         this.latLng1 = latLng1;
      }
 }

之后重写onReceiveLocation方法,整体代码如下

private BaiduMap map;
...
 public class MyLocationListener extends BDAbstractLocationListener {
      LatLng latLng1;
      public MyLocationListener(LatLng latLng1){
         super();
         this.latLng1 = latLng1;
      }
      @Override
      public void onReceiveLocation(BDLocation location){
      	   //处理定位数据的工具
      	  MyLocationData locData = new MyLocationData.Builder().accuracy(bdLocation.getRadius()).direction(100)
      	 .latitude(latLng1.latitude).longitude(latLng1.longitude).build();
      	  //设置位置信息
      	  map.setMyLocationData(locData);
      	  //设置定位图标及定位动画
      	  MapStatus.Builder builder = new MapStatus.Builder();
      	  builder.target(latLng1).zoom(18.0f);
          map.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
      }
 }

至此,进行任意定位的前期工作就做好了
接下来就要写地理编码(地址->坐标)的方法,该方法的参数只有一个那就是定位的地址(注意:必须是百度地图能够识别的地址),代码如下:

 ...
 public void getThere(String address){
     //进行正地理编码要用到的类
     GeoCoder geoCoder = GeoCoder.newInstance();
     //对地址进行分割
     if(address.contains("省")){
          address = address.split("省")[1];
     }
     if(!address.contains("市")){
         Toast.makeText(this, "地址有误!", Toast.LENGTH_SHORT).show();
         return;
     }
     String city = address.split("市")[0];
     address = address.split("市")[1];
     String finalAddress = address;
     geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
            @Override
            public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
                if (geoCodeResult == null || geoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
                    //因为第一次编码的值为空需要进行再次编码
                    geoCoder.geocode(new GeoCodeOption().address(finalAddress).city(city));
                }else {
                    //获取定位数据
                    latLng = geoCodeResult.getLocation();
                    //进行定位
                    locationClient = new LocationClient(MainActivity.this.getApplicationContext());  //声明locationClient类
                    myListener = new MyLocationListener(latLng);
                    locationClient.registerLocationListener(myListener);
                    locationClient.start();
                }
            }
    
            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
                return;
            }
    });
    //发起正地理编码请求
    geoCoder.geocode(new GeoCodeOption().address(address).city(city));
 }

现在,我们的任意定位的结构已经设计好了,接下来只要调用getThere方法传入参数即可。

自己做的demo效果如下:
在这里插入图片描述
————————————结束语————————————
如果有什么问题欢迎在评论区里讨论

发布了3 篇原创文章 · 获赞 4 · 访问量 1799

猜你喜欢

转载自blog.csdn.net/qq_31236027/article/details/104235004
今日推荐