1.首先是在百度地图开发网站寻找Android地图SDK开发文档,进去根据教程注册和获取密钥。
?2.随后在manifest里面添加一些权限。
<!-- 访问网络,进行地图相关业务数据请求,包括地图数据,路线规划,POI检索等 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 获取网络状态,根据网络状态切换进行数据请求网络转换 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 读取外置存储。如果开发者使用了so动态加载功能并且把so文件放在了外置存储区域,则需要申请该权限,否则不需要 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- 写外置存储。如果开发者使用了离线地图,并且数据写在外置存储区域,则需要申请该权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 这个权限用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 这个权限用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<service android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote"/>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="自己的key" />
3.main代码如下
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
requestLocation();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Without Location Permissions!", Toast.LENGTH_SHORT).show();
finish();
} else {
Toast.makeText(this, "Got Location Permissions!", Toast.LENGTH_SHORT).show();
requestLocation();
}
break;
}
}
private void requestLocation() {
MyLocationListener myLocationListener = new MyLocationListener();
mLocationClient = new LocationClient(getApplicationContext());
mLocationClient.registerLocationListener(myLocationListener);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mapView = findViewById(R.id.bmapView);
baiduMap = mapView.getMap();
baiduMap.setMyLocationEnabled(true);
LocationClientOption option = new LocationClientOption();
option.setScanSpan(1000);//每一秒发送一次
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
option.setIsNeedAddress(true);
mLocationClient.setLocOption(option);
mLocationClient.start();//启动位置请求
}
private class MyLocationListener extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
StringBuffer currentPosition = new StringBuffer();
currentPosition.append("Longitude: ").append(bdLocation.getLongitude()).append("\n");
currentPosition.append("Latitude: ").append(bdLocation.getLatitude());
String s = "" + bdLocation.getLatitude();
if (bdLocation.getLocType() == BDLocation.TypeGpsLocation || bdLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
setMyLocation(bdLocation);
}
}
}
private void setMyLocation(BDLocation bdLocation) {
if (isFirstLocation) {
LatLng latLng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(latLng);
MyLocationData locationData = new MyLocationData.Builder().latitude(latLng.latitude).longitude(latLng.longitude).build();
baiduMap.animateMapStatus(update);
baiduMap.setMyLocationData(locationData); // 地图上显示定位点
isFirstLocation = false;
}
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocationClient.stop();
mapView.onDestroy();
}
}
4.结果如图
?
代码仓库:https://gitee.com/flowers-like-brocade/projects
|