最近利用下班时间,找了看什么书比较适合初学android的朋友,很多人推荐了这本书,于是就买了一本,感觉看书,思考,动手,再思考和总结这样过程还是很有必要的,于是就打算把自己学习的东西简单的总结一下;方便自己以后查找,也有利于学习的巩固。在这里首先要感谢一下书籍的作者——郭霖前辈。
看这一篇之前,强烈建议先开一下前期准备工作;如果前期准备工作完成以后,那么我们就可以基于百度地图以及百度定位的SDK进行基础功能开发了;当然这里面还是有一些准备工作需要做,不过因为资料都已经有了,所以只是简单的复制粘贴就可以了。
1,准备工作
第一步:把AK添加到配置文件里面,和使用其他在线平台服务一样
第二步:在main目录下,新建jniLibs文件夹,把SDK示例的so文件放在该文件夹下
第三步:把示例代码中的核心jar包放在libs文件夹下,并右键添加为jar文件即可,或者在gradle文件中的依赖jar包中添加上也可以;
2,示例代码
MainActivity.java代码:
package com.hfut.operationbaidumapapi;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.Poi;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.MapView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
public LocationClient myLocationClient = null;
public MyLocationListener myLocationListener;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在使用SDK各组件之前初始化context信息,传入ApplicationContext
//注意该方法要再setContentView方法之前实现
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_main);
registerMyLocationListener();
}
public void showLocation(View view) {
Intent intent = new Intent(this, ShowLocationActivity.class);
startActivity(intent);
}
public void showKindMap(View view) {
Intent intent = new Intent(this, ShowMapActivity.class);
startActivity(intent);
}
private void getLocation() {
LocationClientOption option = new LocationClientOption();
option.setScanSpan(1000);
option.setOpenGps(true);
option.setLocationNotify(true);
option.setLocationMode(LocationClientOption.LocationMode.Device_Sensors);
option.setIgnoreKillProcess(false);
option.SetIgnoreCacheException(false);
option.setWifiCacheTimeOut(5 * 60 * 1000);
option.setEnableSimulateGps(false);
option.setIsNeedAddress(true);
option.setIsNeedLocationDescribe(true);
option.setIsNeedLocationPoiList(true);
myLocationClient.setLocOption(option);
// 启动定位
myLocationClient.start();
}
public class MyLocationListener extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
MyLocationInfo.myLatitude = location.getLatitude(); //获取纬度信息
MyLocationInfo.mylongitude = location.getLongitude(); //获取经度信息
MyLocationInfo.address = location.getAddrStr(); //获取详细地址信息
MyLocationInfo.country = location.getCountry(); //获取国家
MyLocationInfo.province = location.getProvince(); //获取省份
MyLocationInfo.city = location.getCity(); //获取城市
MyLocationInfo.district = location.getDistrict(); //获取区县
MyLocationInfo.street = location.getStreet(); //获取街道信息
MyLocationInfo.type = location.getNetworkLocationType();
MyLocationInfo.locationDescribe = location.getLocationDescribe(); //获取位置描述信息
MyLocationInfo.poiList = location.getPoiList();
//MyLocationInfo.showIs(MyApplication.getContext());
}
}
private void registerMyLocationListener() {
myLocationClient = new LocationClient(getApplicationContext());
myLocationListener = new MyLocationListener();
myLocationClient.registerLocationListener(myLocationListener);
getLocation();
}
}
MyApplication.java代码:
package com.hfut.operationbaidumapapi;
import android.app.Application;
import android.content.Context;
/**
* author:why
* created on: 2018/4/14 17:26
* description:
*/
public class MyApplication extends Application {
public static Context context;
@Override
public void onCreate() {
context=getApplicationContext();
super.onCreate();
}
public static Context getContext(){
return context;
}
}
MyLocationInfo.java代码:
package com.hfut.operationbaidumapapi;
import com.baidu.location.Poi;
import java.util.List;
/**
* author:why
* created on: 2018/4/14 16:00
* description:
*/
public class MyLocationInfo {
public static double myLatitude = 0.0;
public static double mylongitude = 0.0;
public static float radius=0.0f;
public static String address = "";
public static String country = "";
public static String province = "";
public static String city = "";
public static String district = "";
public static String street = "";
public static String type = "";
public static String locationDescribe = "";
public static List<Poi> poiList ;
// public static void showIs(Context context){
// Toast.makeText(context,"我在定位",Toast.LENGTH_SHORT).show();
// }
}
PermissionHelper.java代码:
package com.hfut.operationbaidumapapi;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import java.util.List;
import java.util.ArrayList;
/**
* author:why
* created on: 2018/4/11 14:35
* description:
*/
public class PermissionHelper {
public static String[] requestList;
public static String[] getPermissionRequest(Context context) {
List<String> permissionList = new ArrayList<String>();
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) !=
PackageManager.PERMISSION_GRANTED) {
permissionList.add(Manifest.permission.READ_PHONE_STATE);
}
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED) {
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if(!permissionList.isEmpty()){
requestList=permissionList.toArray(new String[permissionList.size()]);
}
return requestList;
}
}
ShowLocationActivity.java代码:
package com.hfut.operationbaidumapapi;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.Poi;
import java.util.List;
import static com.hfut.operationbaidumapapi.MyLocationInfo.*;
public class ShowLocationActivity extends AppCompatActivity {
// public static final int LONANDLATINFO=0;
// public static final int ADDRESS=1;
// public static final int ADDRESSDESC=2;
// public static final int NEARBRPOI=3;
//存储位置信息
private StringBuilder locationInfo=new StringBuilder();
//public LocationClient myLocationClient=null;
//public MyLocationListener myLocationListener;
private TextView myLocation;
private static final String TAG = "MainActivity";
String[] permissionRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_location);
myLocation = findViewById(R.id.myLocation);
//权限申请
permissionRequest=PermissionHelper.getPermissionRequest(this);
if(permissionRequest!=null){
ActivityCompat.requestPermissions(this,permissionRequest,1);
}
}
//获取经纬度
public void getLonandLatInfo(View view) {
// //一定要先调用该对象的stop()方法,不然只能获取一种位置信息,其他的全是null
// if (myLocationClient != null) {
// myLocationClient.stop();
// }
// registerMyLocationListener(LONANDLATINFO);
// getLocation(myLocationListener.flag);
locationInfo.append("经度:" + myLatitude + "\n");
locationInfo.append("维度:" + mylongitude + "\n");
locationInfo.append("精度:" + radius + "\n");
locationInfo.append("定位方式:" + MyLocationInfo.type);
showInfo();
}
//获取具体位置信息
public void getPositionInfo(View view) {
// if(myLocationClient!=null){
// myLocationClient.stop();
// }
// registerMyLocationListener(ADDRESS);
// getLocation(myLocationListener.flag);
locationInfo.append("详细地址:"+address+"\n");
locationInfo.append("国家:"+country+"\n");
locationInfo.append("省份:"+province+"\n");
locationInfo.append("城市:"+city+"\n");
locationInfo.append("县区:"+district+"\n");
locationInfo.append("街道:"+street+"\n");
locationInfo.append("定位方式:" + type);
showInfo();
}
//获取位置描述
public void getPositionDesc(View view) {
// if(myLocationClient!=null){
// myLocationClient.stop();
// }
// registerMyLocationListener(ADDRESSDESC);
// getLocation(myLocationListener.flag);
locationInfo.append("位置描述:"+locationDescribe);
showInfo();
}
//获取位置描述
public void getPositionPOI(View view) {
// if(myLocationClient!=null){
// myLocationClient.stop();
// }
// registerMyLocationListener(NEARBRPOI);
// getLocation(myLocationListener.flag);
locationInfo.append("您当前位置在:\n"+locationDescribe+"\n");
locationInfo.append("您附近的标志位置有:\n");
for(Poi poi:poiList){
locationInfo.append(poi.getName()+"\n");
}
showInfo();
}
//位置提醒功能
public void positionNotify(View view) {
// if(myLocationClient!=null){
// myLocationClient.stop();
// }
// registerMyLocationListener(NEARBRPOI);
// getLocation(myLocationListener.flag);
}
// private void getLocation(int locationType) {
// LocationClientOption option = new LocationClientOption();
// option.setScanSpan(3000);
// option.setOpenGps(true);
// option.setLocationNotify(true);
// option.setIgnoreKillProcess(false);
// option.SetIgnoreCacheException(false);
// option.setWifiCacheTimeOut(5*60*1000);
// option.setEnableSimulateGps(false);
// if(locationType==1){
// option.setIsNeedAddress(true);
// }
// if (locationType==2){
// option.setIsNeedAddress(true);
// option.setIsNeedLocationDescribe(true);
// }
// if (locationType==3){
// option.setIsNeedAddress(true);
// option.setIsNeedLocationDescribe(true);
// option.setIsNeedLocationPoiList(true);
// }
// myLocationClient.setLocOption(option);
// // 启动定位
// myLocationClient.start();
// }
// public class MyLocationListener extends BDAbstractLocationListener {
// int flag;
// public MyLocationListener(int flag){
// this.flag=flag;
// }
// @Override
// public void onReceiveLocation(BDLocation location){
// myLocation.setText("");
// if(flag==0) {
// double myLatitude = location.getLatitude();
// double mylongitude = location.getLongitude();
// float radius = location.getRadius(); //获取定位精度,默认值为0.0f
// String type = location.getNetworkLocationType();
// locationInfo.append("经度:" + myLatitude + "\n");
// locationInfo.append("维度:" + mylongitude + "\n");
// locationInfo.append("精度:" + radius + "\n");
// locationInfo.append("定位方式:" + MyLocationInfo.type);
// }
// else if(flag==1){
// String addr = location.getAddrStr(); //获取详细地址信息
// String country = location.getCountry(); //获取国家
// String province = location.getProvince(); //获取省份
// String city = location.getCity(); //获取城市
// String district = location.getDistrict(); //获取区县
// String street = location.getStreet(); //获取街道信息
// String type = location.getNetworkLocationType();
// locationInfo.append("详细地址:"+address+"\n");
// locationInfo.append("国家:"+country+"\n");
// locationInfo.append("省份:"+province+"\n");
// locationInfo.append("城市:"+city+"\n");
// locationInfo.append("县区:"+district+"\n");
// locationInfo.append("街道:"+street+"\n");
// locationInfo.append("定位方式:" + type);
// }
// else if(flag==2){
// String locationDescribe = location.getLocationDescribe(); //获取位置描述信息
// locationInfo.append("位置描述:"+locationDescribe);
// }
// else if(flag==3){
// String locationDescribe = location.getLocationDescribe(); //获取位置描述信息
// List<Poi> poiList = location.getPoiList();
// locationInfo.append("您当前位置在:\n"+locationDescribe+"\n");
// locationInfo.append("您附近的标志位置有:\n");
// for(Poi poi:poiList){
// locationInfo.append(poi.getName()+"\n");
// }
// }
// myLocation.setText(locationInfo.toString());
// //清除数据
// locationInfo.delete(0,locationInfo.toString().length());
// int errorCode = location.getLocType();
// //获取定位类型、定位错误返回码,具体信息可参照类参考中BDLocation类中的说明
// Log.i(TAG, "onReceiveLocation: "+errorCode);
// }
// }
//
// private void registerMyLocationListener(int positionType){
// myLocationClient = new LocationClient(getApplicationContext());
// myLocationListener = new MyLocationListener(positionType);
// myLocationClient.registerLocationListener(myLocationListener);
// }
//
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 1:
if(grantResults.length>0){
for(int result: grantResults){
Toast.makeText(this,"我是why,谢谢你同意权限让我测试",
Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(this,"你为什么要拒绝?",
Toast.LENGTH_SHORT).show();
}
}
}
//
// public class MyNotificationLisenter extends BDNotifyListener {
// public void onNotify(BDLocation mlocation, float distance){
// //已到达设置监听位置附近
// }
// }
private void showInfo(){
myLocation.setText(locationInfo.toString());
//清除数据
locationInfo.delete(0,locationInfo.toString().length());
}
}
ShowMapActivity.java代码:
package com.hfut.operationbaidumapapi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.baidu.location.BDLocation;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import java.util.Map;
public class ShowMapActivity extends AppCompatActivity {
private BaiduMap baiduMap = null;
private MapView mapView = null;
private static final String TAG = "ShowMapActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在使用SDK各组件之前初始化context信息,传入ApplicationContext
//注意该方法要再setContentView方法之前实现
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_show_map);
mapView = findViewById(R.id.show_MapView);
baiduMap = mapView.getMap();
}
//显示普通地图
public void showNormalMap(View view) {
baiduMap = mapView.getMap();
baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
}
//显示实时交通地图
public void showTrafficMap(View view) {
baiduMap = mapView.getMap();
baiduMap.setTrafficEnabled(true);
}
//显示卫星地图
public void showSattelliteMap(View view) {
baiduMap = mapView.getMap();
baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
}
//显示城市热力图
public void showCityHotMap(View view) {
baiduMap = mapView.getMap();
baiduMap.setBaiduHeatMapEnabled(true);
}
//显示我当前位置地图
public void showMyLocationMap(View view) {
baiduMap = mapView.getMap();
baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
moveToMyLoc(baiduMap);
}
//把我显示到地图上
public void showMyLocMapWithTag(View view) {
baiduMap = mapView.getMap();
baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
baiduMap.setMyLocationEnabled(true);
moveToMyLoc(baiduMap);
MyLocationData.Builder builder=new MyLocationData.Builder();
builder.latitude(MyLocationInfo.myLatitude);
builder.longitude(MyLocationInfo.mylongitude);
MyLocationData locationData=builder.build();
baiduMap.setMyLocationData(locationData);
}
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mapView.onDestroy();
baiduMap.setMyLocationEnabled(false);
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mapView.onPause();
}
private void moveToMyLoc(BaiduMap baiduMap) {
//移动地图到指定经纬度上
LatLng latLng = new LatLng(MyLocationInfo.myLatitude, MyLocationInfo.mylongitude);
MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(latLng);
baiduMap.animateMapStatus(update);
}
}
activity_main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hfut.operationbaidumapapi.MainActivity">
<Button
android:layout_marginTop="10dp"
android:text="获取当前位置信息"
android:onClick="showLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_marginTop="10dp"
android:text="获取不同类型地图"
android:onClick="showKindMap"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
activity_show_location.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hfut.operationbaidumapapi.ShowLocationActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="140dp">
<TextView
android:textSize="20dp"
android:id="@+id/myLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="显示当前位置信息"
/>
</ScrollView>
<Button
android:layout_marginTop="20dp"
android:textSize="20dp"
android:onClick="getLonandLatInfo"
android:text="获取当前位置经纬度"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_marginTop="10dp"
android:textSize="20dp"
android:onClick="getPositionInfo"
android:text="获取当前位置具体名称"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_marginTop="10dp"
android:textSize="20dp"
android:onClick="getPositionDesc"
android:text="获取当前位置描述信息"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_marginTop="10dp"
android:textSize="20dp"
android:onClick="getPositionPOI"
android:text="获取当前位置周围POI"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--<TextView-->
<!--android:textSize="20dp"-->
<!--android:textColor="@color/colorPrimary"-->
<!--android:gravity="center_horizontal"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="下面是辅助功能测试"/>-->
<!--<Button-->
<!--android:layout_marginTop="10dp"-->
<!--android:textSize="20dp"-->
<!--android:onClick="positionNotify"-->
<!--android:text="位置提醒功能"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content" />-->
</LinearLayout>
activity_show_map.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hfut.operationbaidumapapi.ShowMapActivity">
<com.baidu.mapapi.map.MapView
android:id="@+id/show_MapView"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:clickable="true" />
<ScrollView
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showNormalMap"
android:text="显示普通地图"
android:textSize="20dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showTrafficMap"
android:text="显示实时路况图"
android:textSize="20dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showSattelliteMap"
android:text="显示卫星地图"
android:textSize="20dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showCityHotMap"
android:text="显示城市热力图"
android:textSize="20dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showMyLocationMap"
android:text="显示我当前位置"
android:textSize="20dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="showMyLocMapWithTag"
android:text="显示我到地图上"
android:textSize="20dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
主配置文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hfut.operationbaidumapapi">
<uses-permission android:name="android.permission.INTERNET" />
<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.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:name=".MyApplication"
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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 定位服务 -->
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" />
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="mbRvWHeiuFeh4GRO1hljXI4wmVcsWO4u" />
<activity android:name=".ShowLocationActivity" />
<activity android:name=".ShowMapActivity" />
</application>
</manifest>
build.gradle文件(app):
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.hfut.operationbaidumapapi"
minSdkVersion 22
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDir 'libs'
//说明so的路径为该libs路径,关联所有地图SDK的so文件
jni.srcDirs = [];
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation files('libs/BaiduLBS_Android.jar')
}
3,运行结果
第一步:运行程序
第二步:点击“获取当前位置信息”,进去随意点击
第三步:点击“获取不同类型地图”,进去随意点击查看
- (1)打开该应用的定位功能,不然获取不到数据
- (2)我使用的是6.0以下的版本,我做了动态权限申请,兼容6.0及以上版本
- (3)我这边定位不是特别精确
- (4)百度地图包括定位API还有很多内容可以挖掘,我这是最基本的功能实现,后面会进一步学习
- (5)我注释的代码其实在之前还是做了很多设计的;所有没有舍得删除
注:欢迎扫码关注