1.这里最主要的就是SHA1比较麻烦
包名就是java代码第一行那个
找到图中的文件双击
复制自己的SHA1就OK了
如果上图你没有出现那个目录的话,取消勾选这个按钮,点击OK,设置完成以后重新打开软件,就OK了
如果上面都配置好了的话,就开始进入最激动人心的实现地图功能。
AndroidMainfest.xml
//这个是百度上自己软件key的值,每个人都不一样
MainActivity.java
package com.example.baidugps;
import android.Manifest;
import android.app.Activity;
import android.app.alertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import java.util.List;
public class MainActivity extends Activity {
private MapView mapView;
private BaiduMap baiduMap;
private LocationManager locationManager;
private String provider;
private boolean isFirstLocate = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.bmapView);
baiduMap = mapView.getMap();
new Thread(new GameThread()).start();
openGPSSettings();
//设置位置提供器
setLovationManager();
//将显示位置的功能开启
baiduMap.setMyLocationEnabled(true);
}
Handler mHandler = new Handler() {
public void handleMessage(Message msg){
switch (msg.what){
case 1:
Log.d("输出a",""+isFirstLocate);
mapView.invalidate(); //刷新界面
isFirstLocate = false;
break;
}
}
};
class GameThread implements Runnable {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
new Thread(new Runnable() {
@Override
public void run() {
Log.d("输出1",""+isFirstLocate);
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this, new String []{android.Manifest.permission.ACCESS_COARSE_LOCATION},1);
}
if (isFirstLocate){
mapView.invalidate(); //刷新界面
Message msg = new Message();
msg.what = 1;
mHandler.sendMessage(msg);
}
}
}).start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
private boolean checkGPSIsOpen() {
boolean isOpen;
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
isOpen = locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER);
return isOpen;
}
private void openGPSSettings() {
if (!checkGPSIsOpen()) {
//没有打开则弹出对话框
alertDialog.Builder dialog = new alertDialog.Builder(this);
dialog.setTitle("提示");
dialog.setMessage("打开定位功能可以提高精度哟");
dialog.setCancelable(false);//不能用返回关闭
// 拒绝, 退出应用
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Activity activity=new Activity();
System.exit(0);
}
});
dialog.setPositiveButton("开启定位",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//跳转GPS设置界面
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
dialog.show();
}
}
private void setLovationManager() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//获取所有可用的位置提供器
List providerList = locationManager.getProviders(true);
if (providerList.contains(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
} else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
} else {
//当前没有可用的位置提供器时,弹出Toast提示
Toast.makeText(this, "没有可用的位置提供器", Toast.LENGTH_SHORT).show();
return;
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
navigateTo(location);
}
locationManager.requestLocationUpdates(provider,5000,5,locationListener);
}
private void navigateTo(Location location) {
//如果是第一次创建,就获取位置信息并且将地图移到当前位置
//为防止地图被反复移动,所以就只在第一次创建时执行
if(isFirstLocate){
//LatLng对象主要用来存放经纬度
//zoomTo是用来设置百度地图的缩放级别,范围为3~19,数值越大越精确
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;
}
//封装设备当前位置并且显示在地图上
//由于设备在地图上显示的位置会根据我们当前位置而改变,所以写到if外面
MyLocationData.Builder locationBuilder=new MyLocationData.Builder();
locationBuilder.latitude(location.getLatitude());
locationBuilder.longitude(location.getLongitude());
MyLocationData locationData=locationBuilder.build();
baiduMap.setMyLocationData(locationData);
}
LocationListener locationListener =new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if(locationManager!=null)
navigateTo(location);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
@Override
protected void onDestroy() {
super.onDestroy();
//最后要销毁mapView
//关闭程序时将监听器移除
//关闭可以显示位置的功能
mapView.onDestroy();
if(locationManager!=null){
locationManager.removeUpdates(locationListener);
}
baiduMap.setMyLocationEnabled(false);
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
}
activity_main.xml
如果这里你的MapView爆红的话,就说明你的百度skd的jar包没有引入成功
到这肯定有帅逼会问了,哎,sdk在哪下载啊
这就是在申请key的那个网站上,选择AndroidSDK,进入这里,点你想要的服务下载SDK就行
进来以后把所有的libs下的文件复制到自己代码的libs下
右击BaiduLBS_ndroid.jar
选择Add_As_Library…
选择ok
你的build.gradle文件就会出现这个
然后在这里面引入
build.gradle
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.baidugps"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation files('libs\BaiduLBS_android.jar')
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
DemoApplication.java
package com.example.baidugps;
import android.app.Application;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
public class DemoApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//在使用SDK各组件之前初始化context信息,传入ApplicationContext
SDKInitializer.initialize(this);
//自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.
//包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。
SDKInitializer.setCoordType(CoordType.BD09LL);
}
}



