android studio上的百度定位的初始化

时间:2021-01-22 19:41:51

记录一下自己引入百度地图API的结果,调用了百度地图的定位,下面开始讲解:


1.下载官方百度地图SDK,详细的我就不说了,自己查就能查到了,然后根据自己的选择进行下载,别忘了申请密钥


2.新建一个项目,导入jar包和so包,不知道的朋友可以点击下面的链接去查看如何导入:

http://blog.csdn.net/anny_lin/article/details/46763255  so包引入

jar包的引入只需要将jar包考入到libs里面,然后右键点击“add as library”就可以了


3.进行基础的配置,官方文档上有给出,当然你也可下载官方的Demo进行代码的查看与修改,我是基于官方demo进行修改的,修改后的代码如下,基本上面什么变化:

package demo.baidumap;

import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

public class MainActivity extends AppCompatActivity {
private LocationClient mLocationClient;
private TextView LocationResult,ModeInfor;
 //开启定位按钮
private Button startLocation;
private RadioGroup selectMode,selectCoordinates;
private EditText frequence;
private LocationClientOption.LocationMode tempMode = LocationClientOption.LocationMode.Hight_Accuracy;
private String tempcoor="gcj02";
private CheckBox checkGeoLocation;
private Handler handler;
private ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
mLocationClient = ((LocationApplication) getApplication()).mLocationClient;
LocationResult = (TextView) findViewById(R.id.textView1);


((LocationApplication) getApplication()).mLocationResult = LocationResult;

//开启定位按钮
startLocation = (Button) findViewById(R.id.addfence);
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what==2) {
mLocationClient.stop();
progressDialog.dismiss();
}

}

};
//开始定位
startLocation.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
progressDialog=ProgressDialog.show(MainActivity.this,"aa","aa");
new Thread(new Runnable() {
@Override

public void run() {
InitLocation();
mLocationClient.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(2);
}
}).start();



}
});
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
mLocationClient.stop();
super.onStop();
}



private void InitLocation(){
LocationClientOption option = new LocationClientOption();
option.setLocationMode(tempMode);//���ö�λģʽ
option.setCoorType(tempcoor);//���صĶ�λ����ǰٶȾ�γ�ȣ�Ĭ��ֵgcj02
int span=1000;
try {
span = Integer.valueOf(frequence.getText().toString());
} catch (Exception e) {

}
option.setScanSpan(span);//���÷���λ����ļ��ʱ��Ϊ5000ms
option.setIsNeedAddress(true);
mLocationClient.setLocOption(option);
}
}

其次是application文件

package demo.baidumap;

import android.app.Application;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.GeofenceClient;
import com.baidu.location.LocationClient;

/**

*/
public class LocationApplication extends Application {



/**
* ��Application
*/

public LocationClient mLocationClient;
public GeofenceClient mGeofenceClient;
public MyLocationListener mMyLocationListener;

public TextView mLocationResult,logMsg;
public TextView trigger,exit;
public Vibrator mVibrator;

@Override
public void onCreate() {
super.onCreate();

mLocationClient = new LocationClient(this.getApplicationContext());
mMyLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mMyLocationListener);

}


/**
*/
public class MyLocationListener implements BDLocationListener {

@Override
public void onReceiveLocation(BDLocation location) {
//Receive Location
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());
sb.append("\ndirection : ");
sb.append("\naddr : ");
sb.append(location.getAddrStr());
sb.append(location.getDirection());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append("\naddr : ");
sb.append(location.getAddrStr());
//��Ӫ����Ϣ
sb.append("\noperationers : ");
sb.append(location.getOperators());
}
logMsg(sb.toString());
Log.i("BaiduLocationApiDem", sb.toString());
}


}


/**
* @param str
*/
public void logMsg(String str) {
try {
if (mLocationResult != null)
mLocationResult.setText(str);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* @author jpren
*
*/

}



最后就是androidfest文件了


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="demo.baidumap" >

<application
android:name=".LocationApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<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">
<intent-filter>
<action android:name="com.baidu.location.service_v2.2" >
</action>
</intent-filter>

</service>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="你的密钥" />

</application>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
</uses-permission>
<uses-permission android:name="android.permission.READ_LOGS" >
</uses-permission>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />



</manifest>

然后就可以进行基础的定位服务了,需要注意的是,你需要在真机上进行测试,虚拟机可能显示不出结果。