首先下载百度地图开发包,把libs中的包拷到项目中libs中
在app下的build.gradle中添加sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
AndroidManifest.xml
application里添加
<meta-data android:name="com.baidu.lbsapi.API_KEY"
android:value="秘钥"/>
秘钥的获取
在安装了Android开发环境后,主目录下会自动创建一个隐藏的.android目录,其下有一个debug.keystore文件即为调试秘钥。
为了获取证书指纹,可以运行如下命令:
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android这样我们就能得到MD5,SHA1和SHA256三种指纹。(本方法获取秘钥没有使用过,如若获取不到参考开发指南)
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>在application外添加权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />mainActivity.java
package com.example.lgkj.bugmapapp;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity{
private MapView mMapView = null;
BaiduMap mBaiduMap;
public LocationClient mLocationclient;
private TextView postionTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//获取地图初始化
// requestWindowFeature(Window.FEATURE_NO_TITLE);
// SDKInitializer.initialize(getApplicationContext());
//
// setContentView(R.layout.activity_main);
// mMapView = (MapView) findViewById(R.id.map_view);
// 开启位置
mLocationclient = new LocationClient(getApplicationContext());
// 监听位置的变化
mLocationclient.registerLocationListener(new MyLocationListener());
setContentView(R.layout.activity_main);
postionTextView = (TextView)findViewById(R.id.position_text_view);
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 (!permissionList.isEmpty()){
String[] permissions = permissionList.toArray(new String[permissionList.size()]);
ActivityCompat.requestPermissions(MainActivity.this,permissions,1);
}else{
requestLocation();
}
}
// 设置更新时间间隔
private void initLocation(){
LocationClientOption option = new LocationClientOption();
// option.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);
option.setScanSpan(2000);
option.setIsNeedAddress(true);
mLocationclient.setLocOption(option);
}
// 开启定位并且初始化
private void requestLocation(){
initLocation();
mLocationclient.start();
}
// 请求权限之后返回数值
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull 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 extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
StringBuffer currentPostion = new StringBuffer();
currentPostion.append("纬度:").append(bdLocation.getLatitude())
.append(" ");
currentPostion.append("经线:").append(bdLocation.getLongitude())
.append(" ");
currentPostion.append("国家:").append(bdLocation.getCountry())
.append(" ");
currentPostion.append("省:").append(bdLocation.getProvince())
.append(" ");
currentPostion.append("市:").append(bdLocation.getCity())
.append(" ");
currentPostion.append("区:").append(bdLocation.getDistrict())
.append(" ");
currentPostion.append("街道:").append(bdLocation.getStreet())
.append(" ");
currentPostion.append("定位方式:");
if (bdLocation.getLocType()==BDLocation.TypeGpsLocation){
currentPostion.append("GPS");
}else if (bdLocation.getLocType()==BDLocation.TypeNetWorkLocation){
currentPostion.append("网络");
}
postionTextView.setText(currentPostion);
}
}
@Override
protected void onResume() {
super.onResume();
// mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
// mMapView.onPause();
}
// 程序关闭之后的销毁工作
@Override
protected void onDestroy() {
super.onDestroy();
mLocationclient.stop();
// mMapView.onDestroy();
// MapView.setMapCustomEnable(false);
// mMapView = null;
}
}