百度Android 定位SDK示例
来源:互联网 发布:数据库系统开发工具 编辑:程序博客网 时间:2024/05/11 20:59
LBS(Location Based Service)是互联网时代的热点,在本地搜索、定向广告、社交网络等众多领域得到了广泛应用。其核心基础,就是定位能力,为各类应用提供位置信息。百度移动定位API是一个提供基础定位API的第三方平台,为各类应用提供定位能力,提升用户体验。
百度地图定位SDK免费对外开放,无需申请key。
百度地图定位SDK免费对外开放,无需申请key。
百度地图定位SDK目前只支持Android和Symbian两大平台,其他平台产品正在计划开发中。
定位SDK可以从 百度地图api官网 下载,具体文档可以参考官方文档,这里不详述。
示例代码
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();@Overridepublic void onCreate() {mLocationClient = new LocationClient(this);mLocationClient.registerLocationListener(myListener);}/** * 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中 */public class MyLocationListenner implements BDLocationListener {@Overridepublic 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());}@Overridepublic 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;@Overridepublic 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);// 设置定时定位的时间间隔。单位msoption.setPriority(LocationClientOption.NetWorkFirst); // 设置网络优先//option.setPoiNumber(10); // 设置最多可返回的POI个数,默认值为3//option.setPoiExtraInfo(false); // 设置是否需要POI的电话地址等详细信息://option.setPoiDistance(500);//设置查询范围,默认值为500,即以当前定位位置为中心的半径大小option.disableCache(true); // 设置是否启用缓存定位.true表示禁用缓存定位,false表示启用缓存定位。mLocationClient.setLocOption(option);}@Overridepublic 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;}}@Overrideprotected 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>
运行结果
- 百度Android 定位SDK示例
- 百度Android 定位SDK v4.0示例
- android 百度SDK定位
- Android 百度地图SDK定位
- android定位二(百度定位SDK)
- [android]百度定位sdk报错
- 百度地图SDK for Android【定位】
- 百度地图Android定位SDK的API
- Android使用GSP和百度SDK定位
- Android学习之百度定位SDK
- Android--定位 基于百度地图SDK
- android 百度地图定位SDK使用方法
- 百度地图 Android定位SDK--【显示地图 】
- 百度定位SDK Android v7.0 使用方法
- 配置、使用百度定位android sdk
- Xamarin Android 集成百度定位SDK
- Android 百度定位及地图SDK
- 高德Android 定位SDK示例
- 安卓壁纸机制
- android System.out.println无输出
- 2013面试题
- 比赛小结:codeforces#170
- 安卓Activity启动模式
- 百度Android 定位SDK示例
- No6、(数组)
- KM算法 详解+模板
- hive的UDF (2)
- 路由器忘记密码该如何解决
- UVa 270 - Lining Up
- Android内核编程
- 安卓Intent Flag介绍
- aa