android 百度SDK定位

本文介绍如何在Android应用中集成百度定位SDK,包括申请授权KEY、配置权限和服务、调用接口等步骤,并提供了完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android使用百度定位SDK 定位主要有以下几步:

1.下载定位SDK并申请授权KEY ,地址http://developer.baidu.com/map/geosdk-android-developv4.1.htm

2.导入jar包及SO文件

3.配置androidManifest权限及服务

加入权限 :

 <!-- 这个权限用于进行网络定位 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
    </uses-permission>
    <!-- 这个权限用于访问GPS定位 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
    </uses-permission>
    <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
    </uses-permission>
    <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
    </uses-permission>
    <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 -->
    <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" />
    <!-- SD卡读取权限,用户写入离线定位数据 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
    </uses-permission>
    <!-- 允许应用读取低级别的系统日志文件 -->
    <uses-permission android:name="android.permission.READ_LOGS" >
    </uses-permission>

在application标签下加入授权码:(每个授权码对应一个应用,需要自己申请)

 <meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="4ff8I8uXAuHPtpgyVw0GfUvY" />

部署定位服务

 <service
            android:name="com.baidu.location.f"
            android:enabled="true"
            android:process=":remote" >
        </service>

4.调用提供的接口

package com.util.location;

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

import android.content.Context;
import android.widget.Toast;

public class MyLocationManager {
	private static MyLocationManager mInstance;
	private Context mContext;
	private LocationClient mLocationClient;
	private BDLocationListener mListener = null;
	private LocationEntry mCurrLocEntry;
	private boolean hasResult = false;

	public static final int LOCATION_OK = 161;
	private int mRefreshTime = 5000;
	private LocationClientOption mOption;

	public static synchronized MyLocationManager getInstance(Context ctx) {
		if (mInstance == null) {
			mInstance = new MyLocationManager(ctx);
		}
		return mInstance;
	}

	private MyLocationManager(Context ctx) {
		mContext = ctx;
		init();
	}

	private void init() {
		// 初始化定位客户端
		mLocationClient = new LocationClient(mContext);
		// 初始化定位参数
		mOption = new LocationClientOption();
		initLocationOption();
		mListener = new BDLocationListener() {

			@Override
			public void onReceivePoi(BDLocation location) {
			}

			@Override
			public void onReceiveLocation(BDLocation location) {
				if (location == null) {
					hasResult = false;
					if (mResultListener != null) {
						mResultListener.onLocationResult(hasResult, null);
					}
					return;
				}
				LocationEntry entry = new LocationEntry();
				entry.locType = location.getLocType();
				loadLocMsg(entry);
				entry.city = location.getCity();
				entry.longitude = location.getLongitude();
				entry.latitude = location.getLatitude();
				mCurrLocEntry = entry;
				hasResult = true;
				if (mResultListener != null) {
					mResultListener.onLocationResult(hasResult, mCurrLocEntry);
				}
			}
		};
		// 注册定位广播
		mLocationClient.registerLocationListener(mListener);
	}

	private void loadLocMsg(LocationEntry entry) {
		String locMsg = "";
		switch (entry.locType) {
		case 61:
			locMsg = "GPS定位结果";
			break;
		case 62:
			locMsg = "扫描整合定位依据失败。此时定位结果无效";
			break;
		case 63:
			locMsg = "网络异常,没有成功向服务器发起请求。此时定位结果无效";
			break;
		case 65:
			locMsg = "定位缓存的结果";
			break;
		case 66:
			locMsg = "离线定位结果。通过requestOfflineLocaiton调用时对应的返回结果";
			break;
		case 67:
			locMsg = "离线定位失败。通过requestOfflineLocaiton调用时对应的返回结果";
			break;
		case 68:
			locMsg = "网络连接失败时,查找本地离线定位时对应的返回结果";
			break;
		case LOCATION_OK:
			locMsg = "定位正常";
			break;
		case 162:
		case 163:
		case 164:
		case 165:
		case 166:
		case 167:
			locMsg = "服务端定位失败";
			break;
		case 502:
			locMsg = "key参数错误";
			break;
		case 505:
			locMsg = "key不存在或者非法";
			break;
		case 601:
			locMsg = "key服务被开发者自己禁用";
			break;
		case 602:
			locMsg = "key mcode不匹配";
			break;
		default:
			if (entry.locType > 501 && entry.locType < 700) {
				locMsg = "key验证失败";
			}
			break;
		}
		entry.locMsg = locMsg;
	}

	public boolean isStart() {
		return mLocationClient.isStarted();
	}

	public boolean hasResult() {
		return hasResult;
	}

	/**
	 * 初始化定位参数
	 */
	public void initLocationOption() {
		mOption.setOpenGps(false);
		mOption.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式
		mOption.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
		mOption.setScanSpan(mRefreshTime);// 设置发起定位请求的间隔时间为5000ms
		mOption.setIsNeedAddress(true);// 返回的定位结果包含地址信息
		mOption.setAddrType("all");
		mOption.setNeedDeviceDirect(false);// 返回的定位结果包含手机机头的方向
	}

	/**
	 * 设置产品名,功能未知
	 * 
	 * @param name
	 */
	public void setProdName(String name) {
		mOption.setProdName(name);
	}

	/**
	 * 设置刷新时间
	 * 
	 * @param time
	 */
	public void setRefreshTime(int time) {
		mRefreshTime = time;
	}

	public int getRefreshTime() {
		return mRefreshTime;
	}

	/**
	 * 开始定位
	 */
	public void start() {
		if (mLocationClient != null) {
			mLocationClient.setLocOption(mOption);
			mLocationClient.start();
		} else {
			Toast.makeText(mContext, "locClient is null ", Toast.LENGTH_SHORT)
					.show();
		}
	}

	/**
	 * 停止定位
	 */
	public void stop() {
		if (mLocationClient != null && mLocationClient.isStarted()) {
			mLocationClient.stop();
		}
	}

	public void destory() {
		stop();
		mLocationClient = null;
		mListener = null;
		mCurrLocEntry = null;
		mInstance = null;
	}

	public LocationEntry getLocationEntry() {
		return mCurrLocEntry;
	}

	private OnLocationResultListener mResultListener;

	public void setOnLocationResultListener(OnLocationResultListener l) {
		mResultListener = l;
	}

	public class LocationEntry {
		int locType;
		String locMsg;
		String city;
		double longitude;
		double latitude;
	}

	public interface OnLocationResultListener {
		void onLocationResult(boolean hasResult, LocationEntry entry);
	}

}


详细参见  http://developer.baidu.com/map/geosdk-android-developv4.1.htm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值