- 环境的搭建(参考上一次的文章)
百度地图Android定位SDK的使用
2.在Android studio里设计程序来获取经纬度
界面布局activity_main.xml文件的源代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_loc_info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
<Button
android:id="@+id/btn_start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="520dp"
android:text="Start" />
</LinearLayout>
build.gradle(app)文件源代码:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.app"
minSdkVersion 15
targetSdkVersion 28
multiDexEnabled true
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
sourceSets{
main{
jniLibs.srcDir 'libs'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation files('libs/BaiduLBS_Android.jar')
implementation 'com.android.support:multidex:1.0.2'
}
配置文件AndroidManifest.xml文件的源代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.global_1">
<application
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">
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="AK" > 自己获取的AK码
</meta-data>
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:permission="android.permission.BAIDU_LOCATION_SERVICE"
android:process=":remote">
<intent-filter>
<action android:name="com.baidu.location.service_v2.4" />
</intent-filter>
</service>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<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_EXTERNAL_STORAGE"/>
<!--允许sd卡写权限,需写入地图数据,禁用后无法显示地图-->
<uses-permission android:name="android.permission.CAMERA" />
<!--使用步行AR导航,配置Camera权限-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<permission android:name="android.permission.BAIDU_LOCATION_SERVICE" />
<uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
</manifest>
主线程文件MainActivity.java文件的源代码:
package com.example.global_1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
public class MainActivity extends Activity {
private TextView locationInfoTextView = null;
private Button startButton = null;
private LocationClient locationClient = null;
private static final int UPDATE_TIME = 5000;
private static int LOCATION_COUTNS = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationInfoTextView = (TextView) this.findViewById(R.id.tv_loc_info);
startButton = (Button) this.findViewById(R.id.btn_start);
locationClient = new LocationClient(this);
//设置定位条件
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
//是否打开GPS
option.setCoorType("bd09ll");
//可选,设置返回经纬度坐标类型,默认GCJ02
//GCJ02:国测局坐标;
//BD09ll:百度经纬度坐标;
//BD09:百度墨卡托坐标;
//海外地区定位,无需设置坐标类型,统一返回WGS84类型坐标
option.setPriority(LocationClientOption.NetWorkFirst);
//设置定位优先级
option.setProdName("LocationDemo");
//设置产品线名称
option.setScanSpan(UPDATE_TIME);
//设置定时定位的时间间隔。单位毫秒
locationClient.setLocOption(option);
//注册位置监听器
locationClient.registerLocationListener(new BDLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null) {
return;
}
StringBuffer sb = new StringBuffer(256);
sb.append("Time : ");
sb.append(location.getTime());//获取定位时间
sb.append("\nError code : ");
sb.append(location.getLocType());//获取类型
sb.append("\nLatitude : ");
sb.append(location.getLatitude());//获取纬度信息
sb.append("\nLontitude : ");
sb.append(location.getLongitude());//获取经度信息
sb.append("\nRadius : ");
sb.append(location.getRadius());//获取定位精准度
if (location.getLocType() == BDLocation.TypeGpsLocation) {
sb.append("\nSpeed : ");
sb.append(location.getSpeed());
sb.append("\nSatellite : ");
sb.append(location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
sb.append("\nAddress : ");
sb.append(location.getAddrStr());
}
LOCATION_COUTNS++;
sb.append("\n检查位置更新次数:");
sb.append(String.valueOf(LOCATION_COUTNS));
locationInfoTextView.setText(sb.toString());
}
});
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (locationClient == null) {
return;
}
if (locationClient.isStarted()) {
startButton.setText("Start");
locationClient.stop();
}
else {
startButton.setText("Stop");
locationClient.start();
locationClient.requestLocation();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (locationClient != null && locationClient.isStarted()) {
locationClient.stop();
locationClient = null;
}
}
}
运行结果:
点击START后进入监听状态,进行位置的更新
点击STOP后停止位置的更新
百度地图开放平台链接
版权声明:本文为weixin_45755438原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。