一、引用百度SDK
1.登录平台
2.创建应用
3.获取密匙
4.下载开发包
5. 添加注册远程服务和配置应用Key
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"/>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="NDnYRd8LK85vGbLeRVaV23x9RccY5M3p"/>
二、在AS中完成
1.布局mainactivity.xml
public class MainActivity extends AppCompatActivity {
public LocationClient mLocationClient;
TextView tv_positionText;
@Override
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, "没有定位权限!", Toast.LENGTH_LONG).show();
finish();
} else {
requestLocation();
}
}
}
private void requestLocation() {
initLocation();
mLocationClient.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocationClient.stop();
}
private void initLocation() {
setContentView(R.layout.activity_main);
mLocationClient = new LocationClient(getApplicationContext());
mLocationClient.registerLocationListener(new MyLocationListener());
tv_positionText = findViewById(R.id.tv_positionText);
LocationClientOption option = new LocationClientOption();
option.setScanSpan(1000);
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
option.setIsNeedAddress(true);
mLocationClient.setLocOption(option);
}
class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
StringBuffer currentPosition = new StringBuffer();
currentPosition.append("Longitude:").append(bdLocation.getLongitude()).append("\n");
currentPosition.append("Latitude:").append(bdLocation.getLatitude()).append("\n");
currentPosition.append("Country:").append(bdLocation.getCountry()).append("\n");
currentPosition.append("Province:").append(bdLocation.getProvince()).append("\n");
currentPosition.append("City:").append(bdLocation.getCity()).append("\n");
currentPosition.append("District:").append(bdLocation.getDistrict()).append("\n");
currentPosition.append("Street:").append(bdLocation.getStreet()).append("\n");
currentPosition.append("Address:").append(bdLocation.getAddrStr());
tv_positionText.setText(currentPosition);
}
}
2.界面程序MainActivity.java
public class MainActivity extends AppCompatActivity {
LocationClient mLocationClient;
MapView mapView;
BaiduMap baiduMap;
boolean isFirstLocate = true;
TextView tv_Lat;
TextView tv_Lon;
TextView tv_Add;
@Override
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, "没有定位权限!", Toast.LENGTH_LONG).show();
finish();
} else {
requestLocation();
}
}
}
private void requestLocation() {
initLocation();
mLocationClient.start();
}
private void initLocation() {
mLocationClient = new LocationClient(getApplicationContext());
mLocationClient.registerLocationListener(new MyLocationListener());
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mapView = findViewById(R.id.bmapView);
baiduMap = mapView.getMap();
tv_Lat = findViewById(R.id.tv_Lat);
tv_Lon = findViewById(R.id.tv_Lon);
tv_Add = findViewById(R.id.tv_Add);
LocationClientOption option = new LocationClientOption();
option.setScanSpan(1000);
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
option.setIsNeedAddress(true);
mLocationClient.setLocOption(option);
}
private class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
tv_Lat.setText(bdLocation.getLatitude()+"");
tv_Lon.setText(bdLocation.getLongitude()+"");
tv_Add.setText(bdLocation.getAddrStr());
if(bdLocation.getLocType()==BDLocation.TypeGpsLocation || bdLocation.getLocType()==BDLocation.TypeNetWorkLocation){
navigateTo(bdLocation);
}
}
}
private void navigateTo(BDLocation bdLocation) {
if(isFirstLocate){
LatLng ll = new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude());
MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll);
baiduMap.animateMapStatus(update);
isFirstLocate = false;
}
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onResume();
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocationClient.stop();
mapView.onDestroy();
}
}
三、结果展示
|