项目要求
根据百度地图开发者文档,编写百度地图定位程序,在地图上标出自己现在的位置。
项目源码地址
码云仓库地址:https://gitee.com/si–yue/BaiduMap
项目过程
-
目录结构
-
此次设计主要参考百度官方文档&百度文档&获取密钥进行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;
}
MyLocationConfiguration configuration = new MyLocationConfiguration(
MyLocationConfiguration.LocationMode.NORMAL, false, mMarker);
mBaiduMap.setMyLocationConfiguration(configuration);