先进行百度开发平台的注册
1.先百度搜索百度地图开放平台
2.选择开发文档--> Android地图SDK
3.点击获取密钥
?
登录,通过百度App完成个人实名认证
点击创建应用?
输入应用名,类型选择Android-SDK
?
下面的信息也要填
?
Android studio 创建项目,完成创建在下面
?
2. 输入命令行和密码,即可获取SHA1等信息;
命令行:keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey (注意目录选择、开发版本、发布版本等问题)
密码:原始密码一般为android,开发者根据实际情况填写。
3使用 keytool(jdk自带工具)获取
1. 运行进入控制台
Windows:运行 -> 输入cmd -> 确定。
Mac:直接打开 终端。
2. 在控制台内,定位到.android文件夹,输入cd .android
Windows与Mac操作一致,如下截图为Mac效果。
3. 输入命令行和密码,获取SHA1等信息
命令行:keytool -list -v -keystore debug.keystore;
密码:原始密码一般为android,开发者根据实际情况填写。
注意:
调试版本使用 debug.keystore,命令为:keytool -list -v -keystore debug.keystore。 发布版本使用 apk 对应的 keystore,命令为:keytool -list -v -keystore apk 的 keystore。?
把SHA1等信息填完之后,提交
?
复制完,在AndroidManifest.xml中输入下面代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.map">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:name=".DemoApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Map">
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="Te84yrG7nmoBu92cHOaHPc7O1n2lGfTV"
/>
<service android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote"/>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
?在百度地图开发者平台下载Android SDK并解压
?
?
?
下载完解压
?
?
把这些东西复制到
?
创建一个Java类
DemoApplication.java
package com.example.map;
import android.app.Application;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.common.BaiduMapSDKException;
public class DemoApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
SDKInitializer.setAgreePrivacy(getApplicationContext(),true);
try {
// 在使用 SDK 各组间之前初始化 context 信息,传入 ApplicationContext
SDKInitializer.initialize(getApplicationContext());
} catch (BaiduMapSDKException e) {
e.printStackTrace();
}
// //在使用SDK各组件之前初始化context信息,传入ApplicationContext
// SDKInitializer.initialize(getApplicationContext());
//自4.3.0起,百度地图SDK所有接口均支持百度坐标和国测局坐标,用此方法设置您使用的坐标类型.
//包括BD09LL和GCJ02两种坐标,默认是BD09LL坐标。
SDKInitializer.setCoordType(CoordType.BD09LL);
}
}
?在布局文件中activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context=".MainActivity">
<com.baidu.mapapi.map.TextureMapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map1"
android:clickable="true"/>
</LinearLayout>
?在main.Java
package com.example.map;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.common.BaiduMapSDKException;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;
public class MainActivity extends AppCompatActivity {
private TextureMapView mMapView = null;
private BaiduMap mBaiduMap = null;
private LocationClient mLocationClient = null;
private boolean isFirstLogin = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();
LocationClient.setAgreePrivacy(true);
// java
// 是否同意隐私政策,默认为false
SDKInitializer.setAgreePrivacy(getApplicationContext(), true);
try {
// 在使用 SDK 各组间之前初始化 context 信息,传入 ApplicationContext
SDKInitializer.initialize(getApplicationContext());
} catch (BaiduMapSDKException e) {
}
//获取地图控件引用
mMapView = findViewById(R.id.map1);
mBaiduMap = mMapView.getMap();
//显示卫星图层
mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
//开启地图的定位图层
mBaiduMap.setMyLocationEnabled(true);
System.out.println("<<<<<<<<<<<<<,");
//定位初始化
try {
mLocationClient = new LocationClient(getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
//通过LocationClientOption设置LocationClient相关参数
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
//设置locationClientOption
mLocationClient.setLocOption(option);
System.out.println("=================");
//注册LocationListener监听器
MyLocationListener myLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(myLocationListener);
//开启地图定位图层
mLocationClient.start();
}
public class MyLocationListener extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
//mapView 销毁后不在处理新接收的位置
if (location == null || mMapView == null) {
return;
}
if (isFirstLogin) {
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate mapStatusUpdate =
MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(mapStatusUpdate);
isFirstLogin = false;
}
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(location.getDirection()).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
Log.i("经纬度",String.valueOf(locData.latitude));
Log.i("经纬度",String.valueOf(locData.longitude));
Log.i("方向",String.valueOf(locData.direction));
Log.i("精确度",String.valueOf(locData.accuracy));
mBaiduMap.setMyLocationData(locData);
}
}
private void requestPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED) {
System.out.println("-------------");
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时必须调用mMapView. onResume ()
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时必须调用mMapView. onPause ()
mMapView.onPause();
}
@Override
protected void onDestroy() {
mLocationClient.stop();
mBaiduMap.setMyLocationEnabled(false);
mMapView.onDestroy();
mMapView = null;
super.onDestroy();
}
}
这段代码可能报错
<com.baidu.mapapi.map.TextureMapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map1"
android:clickable="true"/>
在配置文件中把
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="Te84yrG7nmoBu92cHOaHPc7O1n2lGfTV"
/>
<service android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote"/>
把上面报错解决就这个项目没事了,运行把,少年
?
|