大家好,又见面了,我是你们的朋友全栈君。
将百度地图官网下载的SDK的压缩包解压得到如下文件:
将BaiduLBS_Android.jar复制到libs目录下,并点击同步
版本比较老的Gradle可能没有这一句,需要手动添加,然后同步
在main 目录下新建jniLibs文件夹,并将百度地图给的sdk里剩下的so文件导入
<!———————-开始代码部分—————————–>
AndroidManifest.xml中
添加权限如下:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WAKE_LOCK"/>
在<application>标签中声明
<!--百度地图秘钥--> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="你在百度地图申请的秘钥" />
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"> </service>
activity_main.xml 布局文件代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true"/> </LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity { public LocationClient mLocationCLient; private MapView mapView; private BaiduMap baiduMap; private boolean isFirstLocate = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mLocationCLient = new LocationClient(getApplicationContext()); mLocationCLient.registerLocationListener(new MyLocationListener()); SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); mapView = (MapView) findViewById(R.id.bmapView); baiduMap = mapView.getMap(); // 让我显示在地图上 baiduMap.setMyLocationEnabled(true); List<String> permissionList = new ArrayList<>(); if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest. permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION); } if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest. permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { permissionList.add(Manifest.permission.READ_PHONE_STATE); } if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest. permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE); } if (!permissionList.isEmpty()) { String[] permissions = permissionList.toArray(new String[permissionList.size()]); ActivityCompat.requestPermissions(MainActivity.this, permissions, 1); } else { requestLocation(); } } private void navigateTo(BDLocation location){ if (isFirstLocate){ LatLng ll=new LatLng(location.getLatitude(),location.getLongitude()); MapStatusUpdate update= MapStatusUpdateFactory.newLatLng(ll); baiduMap.animateMapStatus(update); update=MapStatusUpdateFactory.zoomTo(16f); baiduMap.animateMapStatus(update); isFirstLocate=false; } MyLocationData.Builder locationBuilder=new MyLocationData.Builder(); locationBuilder.latitude(location.getLatitude()); locationBuilder.longitude(location.getLongitude()); MyLocationData locationData=locationBuilder.build(); baiduMap.setMyLocationData(locationData); } private void requestLocation() { initLocation(); mLocationCLient.start(); } private void initLocation() { LocationClientOption option = new LocationClientOption(); option.setScanSpan(1000); option.setIsNeedAddress(true); mLocationCLient.setLocOption(option); } @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(); baiduMap.setMyLocationEnabled(false); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case 1: if (grantResults.length > 0) { for (int result : grantResults) { if (result != PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "必须同意所有权限才能使用", Toast.LENGTH_SHORT).show(); finish(); return; } } requestLocation(); } else { Toast.makeText(this, "未知错误", Toast.LENGTH_SHORT).show(); finish(); } break; default: } } public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(final BDLocation location) { if (location.getLocType()==BDLocation.TypeGpsLocation || location.getLocType()==BDLocation.TypeNetWorkLocation){ navigateTo(location); } } } }
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/154863.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...