AS实现百度地图定位

Android Studio实现百度地图定位

AS实现百度定位在 百度地图开方平台官网中详细讲解了AS的配置和项目实现步骤及代码。从 注册获取密钥AS配置(注意这里要实现定位,下载开发包要包含一个第一行的定位服务)再到显示地图最后显示定位

实现界面展示:

可能是网络不好,所以显示的定位没加载出来。

布局:
<com.baidu.mapapi.map.MapView  
    android:id="@+id/bmapView"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:clickable="true" />
事件:

注意,在官方给的代码的基础上,要显示自己的定位,还需要在获取定位数据时(MyLocationListener中)添加一段代码,在第一次加载地图时更新地图显示状态,即把地图显示移到定位所在地。

if (ifFrist) {
    LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
    MapStatus.Builder builder = new MapStatus.Builder();
    builder.target(ll);
    builder.zoom(18.0f);
    mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
    //放大层级
    ifFrist = false;
}

如果要用图显示自己的位置,则先在onCreate中声明,再在获取数据时生成:

//在onCreate中
mMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_start);
//在MyLocationListener中
//三个参数:LocationMode(定位模式:罗盘,跟随),enableDirection(是否允许显示方向信息)
// ,customMarker(自定义图标)
MyLocationConfiguration configuration = new MyLocationConfiguration(
        MyLocationConfiguration.LocationMode.NORMAL, false, mMarker);
mBaiduMap.setMyLocationConfiguration(configuration);

最后附上完整源码(码云仓库):
链接: https://gitee.com/yangjy11/AS-BaiduMap.

猜你喜欢

转载自blog.csdn.net/qq_44841678/article/details/106313517