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

使用百度地图定位当前位置并获取附近poi -- Android学习之路

2016-12-19 19:16 555 查看
sky-mxc 总结 ;欢迎转载交流 https://sky-mxc.github.io

1. 下载sdk
2. 生成 SHA1码(debug,和release版本)
3. 申请key
4. 集成sdk
5. 获取位置(包含附近poi)


下载定位sdk

链接 http://lbsyun.baidu.com/index.php?title=android-locsdk/geosdk-android-download

生成 SHA1 码

在填写 SHA1码的时候能填写两个,发布版和开发版,如果是的一个人开发的话两个都填一个电脑生成的就好了,如果是多人的话,最好是两个码从两个电脑生成;因为如果码不对的话,定位是无法使用的;

debug 版

定位到 .android 目录下:

打开cmd 输入命令 keytool -list -v -keystore debug.keystore

输入口令 :android (百度地图官网提供)

就得到了SHA1 码:



发布版

在获取发布版的SHA1前 要对项目进行签名

输入命令 keytool -list -v -keystore jrjh.jks (ps:jrjh.jks 是你签名是的keystore)

口令输入你的密码

得到 发布版的SHA1



申请key



集成sdk

详细文档地址:http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/buildprojec

获取位置

定义位置帮助类

import android.content.Intent;
import android.util.Log;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

/**
* Location utils.
* 位置 帮助类
* @author 孟祥超 on 2016.12.15
*/
public class LocationHelper {
public static final String TAG = "LocationHelper";
public LocationClient client= new LocationClient(AppUtils.getAppContext());
//位置监听
private BDLocationListener locationListener = new BDLocationListener() {
@Override
public void onReceiveLocation(BDLocation bdLocation) {
//  2016/12/15 发送一个广播啊
Log.i(TAG, "onReceiveLocation: address="+bdLocation.getAddrStr());
Intent intent =new Intent(Constants.Intent.LOCATION_CHANGE);
intent.putExtra("location",bdLocation);
AppUtils.getAppContext().sendBroadcast(intent);
}
};

public static LocationHelper locationHelper;

/**
* 获取位置帮助类
* @return location helper
* @author 孟祥超 on 2016.12.15 14:16:01
*/
public synchronized static LocationHelper getInstance(){
if (locationHelper == null){
locationHelper = new LocationHelper();
}
return  locationHelper;
}

private LocationHelper(){
initLocation();
}

//初始化 定位
private void initLocation(){
LocationClientOption option = new LocationClientOption();
option.setIsNeedLocationPoiList(true);  //获取附近 poi
option.setIsNeedAddress(true);  //需要详细位置
option.setCoorType("bd09ll");   //百度的编码
option.setScanSpan(1000*3); //3秒 一次
option.setIsNeedLocationDescribe(true); //位置语义化
client.setLocOption(option);    //设置参数
client.registerLocationListener(locationListener);  //注册获取位置监听
}

/**
* 释放 位置对象
* @author 孟祥超 on 2016.12.15 14:23:04
*/
public void release(){
if (locationHelper == null) return;
client.unRegisterLocationListener(locationListener);
locationHelper = null;
}

}


接受广播并更新UI

//获取到 位置信息
private BroadcastReceiver locationChangeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
BDLocation location =intent.getParcelableExtra("location");
if (location == null || TextUtils.isEmpty(location.getAddrStr())){
letterCreateAddress.setText("没有定位的外星球.....");
return;
}
Log.i(TAG, "onReceive: address ="+location.getAddrStr());
letterCreateAddress.setText(location.getAddrStr());
List<Poi> pois = location.getPoiList();
if (pois != null && pois.size() != 0){
nearbyAddress = new ArrayList<>();
}else{
return;
}
for (Poi poi :pois){
Log.i(TAG, "onReceive: poi ="+poi.getName());
nearbyAddress.add(poi.getName());
}
}
};


在项目中已经测试可用;这里做个总结
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐