• 用百度API实现热(WIFI)、GPS、基站定位


    直接在代码。。

    。嘎嘎

    /**
     * 百度基站定位错误返回码
     */
    // 61 : GPS所在地结果
    // 62 : 扫描整合的基础上有针对性的失败。在这一点上的定位结果无效。
    // 63 : 网络异常,没有成功向server发起请求。此时定位结果无效。

    // 65 : 定位缓存的结果。 // 66 : 离线定位结果。通过requestOfflineLocaiton调用时相应的返回结果 // 67 : 离线定位失败。通过requestOfflineLocaiton调用时相应的返回结果 // 68 : 网络连接失败时。查找本地离线定位时相应的返回结果 // 161: 表示网络定位结果 // 162~167: 服务端定位失败 // 502:KEY參数错误 // 505:KEY不存在或者非法 // 601:KEY服务被开发人员自己禁用 // 602: KEY Mcode不匹配,意思就是您的ak配置过程中安全码设置有问题。请确保: sha1正确,“;”分号是英文状态。且包名是您当前执行应用的包名 // 501-700:KEY验证失败 public class BaiduActivity extends Activity implements OnClickListener { private static final String TAG = "BaiduActivity"; private TextView mText; private TextView mTextPoi; private LocationClient mLocationClient = null; private BDLocationListener myListener = new MyLocationListener(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_weather); mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类 // mLocationClient.setAccessKey("8mrnaFzKu3DoduLnWuB5Lt2w"); //V4.1 // mLocationClient.setAK("8mrnaFzKu3DoduLnWuB5Lt2w"); //V4.0 mLocationClient.registerLocationListener(myListener); // 注冊监听函数 setLocationOption(); mLocationClient.start();// 開始定位 initWidgets(); } private void initWidgets() { mText = (TextView) findViewById(R.id.tv_text); mTextPoi = (TextView) findViewById(R.id.tv_text_poi); Button btn = (Button) findViewById(R.id.btn_request); btn.setOnClickListener(this); btn = (Button) findViewById(R.id.btn_request_poi); btn.setOnClickListener(this); } @Override protected void onDestroy() { super.onDestroy(); mLocationClient.stop();// 停止定位 } /** * 设置相关參数 */ private void setLocationOption() { LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); option.setIsNeedAddress(true);// 返回的定位结果包括地址信息 option.setAddrType("all");// 返回的定位结果包括地址信息 option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02 option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms option.disableCache(true);// 禁止启用缓存定位 option.setPoiNumber(5); // 最多返回POI个数 option.setPoiDistance(1000); // poi查询距离 option.setPoiExtraInfo(true); // 是否须要POI的电话和地址等具体信息 option.setPriority(LocationClientOption.NetWorkFirst); // 优先网络定位 // option.setLocationMode(LocationMode.Battery_Saving);//设置定位模式 mLocationClient.setLocOption(option); } public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { if (location == null) return; StringBuffer sb = new StringBuffer(256); sb.append("当前时间 : "); sb.append(location.getTime()); sb.append(" 错误码 : "); sb.append(location.getLocType()); sb.append(" 纬度 : "); sb.append(location.getLatitude()); sb.append(" 经度 : "); sb.append(location.getLongitude()); sb.append(" 半径 : "); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation) { sb.append(" 速度 : "); sb.append(location.getSpeed()); sb.append(" 卫星数 : "); sb.append(location.getSatelliteNumber()); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) { sb.append(" 地址 : "); sb.append(location.getAddrStr()); } mText.setText(sb.toString()); Log.d(TAG, "onReceiveLocation " + sb.toString()); } public void onReceivePoi(BDLocation poiLocation) { // 将在下个版本号中去除poi功能 if (poiLocation == null) { return; } StringBuffer sb = new StringBuffer(256); sb.append("Poi time : "); sb.append(poiLocation.getTime()); sb.append(" error code : "); sb.append(poiLocation.getLocType()); sb.append(" latitude : "); sb.append(poiLocation.getLatitude()); sb.append(" lontitude : "); sb.append(poiLocation.getLongitude()); sb.append(" radius : "); sb.append(poiLocation.getRadius()); if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation) { sb.append(" addr : "); sb.append(poiLocation.getAddrStr()); } if (poiLocation.hasPoi()) { sb.append(" Poi:"); sb.append(poiLocation.getPoi()); } else { sb.append("noPoi information"); } mTextPoi.setText(sb.toString()); Log.d(TAG, "onReceivePoi " + sb.toString()); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_request: if (mLocationClient != null && mLocationClient.isStarted()) mLocationClient.requestLocation(); else Log.d(TAG, "locClient is null or not started"); break; case R.id.btn_request_poi: // 请求POI数据 if (mLocationClient != null && mLocationClient.isStarted()) mLocationClient.requestPoi(); break; default: break; } } }

    代码下载BaiduLocation

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    python configparser模块
    python shutil模块
    软件架构百度百科
    设计模式
    python Sys module
    python time module
    对别人的敬仰就是对自己的信心。数据和计算给了人类史无前例的巨大能力,也带来了前所未有的未知,挑战着人类的自信。
    阿波罗计划里最了不起的就是自信心,这份自信,比任何事情都重要。到今天为止,我觉得美国这个国家做的最有领导力的事情就是阿波罗登月计划。今天我们享受的很多科技,从通信技术到生物医疗,再到材料以及其他很多东西,都要感谢阿波罗登月计划打下的非常好的基础。
    打一个不太恰当的比方,我们与技术的关系就像是农夫与蛇。什么是对技术的热爱?你真的相信技术会改变很多东西吗?你有没有足够的自信和热爱,去捂暖这条蛇,哪怕它苏醒以后可能会咬你一口?
    人们熟知的一句名言是:“天才是1%的灵感加99%的汗水。”可如果没有那1%的灵感,世界上所有的汗水也就仅仅是一桶汗水而已。
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/4711718.html
Copyright © 2020-2023  润新知