您的位置:首页 > 移动开发 > Android开发

百度Android 定位SDK示例

2015-11-04 11:00 375 查看
LBS(Location Based Service)是互联网时代的热点,在本地搜索、定向广告、社交网络等众多领域得到了广泛应用。其核心基础,就是定位能力,为各类应用提供位置信息。百度移动定位API是一个提供基础定位API的第三方平台,为各类应用提供定位能力,提升用户体验。</span>
MyApp.java

package com.yulore.baidupoi;

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

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

public class MyApp extends Application {

public static final String TAG = "MyApp";
public LocationClient mLocationClient;
public TextView tv_result;
public MyLocationListenner myListener = new MyLocationListenner();
@Override
public void onCreate() {

mLocationClient = new LocationClient(this);

mLocationClient.registerLocationListener(myListener);
}

/**
* 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中
*/
public class MyLocationListenner implements 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("\n省:");
sb.append(location.getProvince());
sb.append("\n市:");
sb.append(location.getCity());
sb.append("\n区/县:");
sb.append(location.getDistrict());
sb.append("\naddr : ");
sb.append(location.getAddrStr());
}
sb.append("\nsdk version : ");
sb.append(mLocationClient.getVersion());
sb.append("\nisCellChangeFlag : ");
sb.append(location.isCellChangeFlag());

setAddress(sb.toString());

Log.e(TAG, sb.toString());
}

@Override
public void onReceivePoi(BDLocation poiLocation) {
if (poiLocation == null){
return ;
}

StringBuffer sb = new StringBuffer(256);
sb.append("Poi time : ");
sb.append(poiLocation.getTime());
sb.append("\nerror code : ");
sb.append(poiLocation.getLocType());
sb.append("\nlatitude : ");
sb.append(poiLocation.getLatitude());
sb.append("\nlontitude : ");
sb.append(poiLocation.getLongitude());
sb.append("\nradius : ");
sb.append(poiLocation.getRadius());
if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append("\naddr : ");
sb.append(poiLocation.getAddrStr());
}
if(poiLocation.hasPoi()){
sb.append("\nPoi:");
sb.append(poiLocation.getPoi());
}else{
sb.append("noPoi information");
}

setAddress(sb.toString());

Log.e(TAG, sb.toString());
}
}

private void setAddress(String text) {
if(text!=null){
if(tv_result!=null){
tv_result.setText(text);
}
}
}

}
MainActivity.java

package com.yulore.baidupoi;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

private Button bt_poi;
private Button bt_start;
private TextView tv_result;
private boolean isStart;
private LocationClient mLocationClient;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

findViewById();

mLocationClient = ((MyApp) getApplication()).mLocationClient;
((MyApp) getApplication()).tv_result = tv_result;

setListener();
}

private void findViewById() {
bt_poi = (Button) findViewById(R.id.bt_poi);
bt_start = (Button) findViewById(R.id.bt_start);
tv_result = (TextView) findViewById(R.id.tv_result);
}

private void setListener() {
bt_start.setOnClickListener(this);
bt_poi.setOnClickListener(this);
}

private void setLocationOption() {
LocationClientOption option = new LocationClientOption();
option.setOpenGps(false); 	// 设置是否打开gps,使用gps前提是用户硬件打开gps。默认是不打开gps的
option.setCoorType("bd09ll"); 	// 设置返回值的坐标类型
option.setServiceName("com.baidu.location.service_v2.9");
option.setAddrType("all");	// 设置是否要返回地址信息,默认为无地址信息
option.setScanSpan(3000);	// 设置定时定位的时间间隔。单位ms
option.setPriority(LocationClientOption.NetWorkFirst); 	// 设置网络优先
//		option.setPoiNumber(10); 	// 设置最多可返回的POI个数,默认值为3
//		option.setPoiExtraInfo(false); 	// 设置是否需要POI的电话地址等详细信息:
//		option.setPoiDistance(500);		//设置查询范围,默认值为500,即以当前定位位置为中心的半径大小
option.disableCache(true); 		// 设置是否启用缓存定位.true表示禁用缓存定位,false表示启用缓存定位。

mLocationClient.setLocOption(option);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_start:

if(!isStart){
setLocationOption();
mLocationClient.start();
bt_start.setText("停止");
isStart = true;
}else{
mLocationClient.stop();
bt_start.setText("开始");
isStart = false;
}

break;
case R.id.bt_poi:

if(mLocationClient!=null && mLocationClient.isStarted()){
setLocationOption();
mLocationClient.requestLocation();
}
break;

default:
break;
}
}

@Override
protected void onStop() {
mLocationClient.stop();
super.onStop();
}
}
布局文件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_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<Button
android:id="@+id/bt_start"
android:layout_width="100dip"
android:layout_height="50dip"
android:text="@string/start_loc" >
</Button>

<Button
android:id="@+id/bt_poi"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/loc" >
</Button>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: