当前位置: 移动技术网 > 移动技术>移动开发>Android > android使用百度地图SDK获取定位信息示例

android使用百度地图SDK获取定位信息示例

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文使用android studio开发。

获取定位信息相对简单,我们只需要如下几步:

第一步,注册百度账号,在百度地图开放平台新建应用、生成api_key。这些就不细说了,请前往这里:

第二步,下载sdk,地址:

第三步,建立android studio工程(略过不说),配置环境:

将解压后的文件放入libs文件夹下,并在src/main下建立一个叫做jnilibs的文件夹,并把解压后内的文件夹靠进去,如下图:

这里写图片描述 这里写图片描述

第四步,将baidulbs_android.jar加入环境变量(右键,add as library),并在app的build.gradle中的android中添加:

 sourcesets {
    main {
      jnilibs.srcdirs = ['libs']
    }
  }

如图:

这里写图片描述

第五步,在androidmanifest.xml文件中声明权限:

<uses-permission android:name="android.permission.internet"/>
  <uses-permission android:name="android.permission.access_wifi_state" />
  <uses-permission android:name="android.permission.access_network_state" />
  <!--网络定位-->
  <uses-permission android:name="android.permission.access_coarse_location" />
  <!--gps定位-->
  <uses-permission android:name="android.permission.access_fine_location" />

并在application标签中添加如下内容:

 <meta-data
      android:name="com.baidu.lbsapi.api_key"
      android:value="你申请的api key" />
    <service
      android:name="com.baidu.location.f"
      android:enabled="true"
      android:process=":remote" />

第六步,测试代码,获取定位信息:

public class mainactivity extends appcompatactivity {

  public locationclient mlocationclient = null;
  public bdlocationlistener mylistener = new mylocationlistener();

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    startlocate();
  }

  /**
   * 定位
   */
  private void startlocate() {
    mlocationclient = new locationclient(getapplicationcontext());   //声明locationclient类
    mlocationclient.registerlocationlistener(mylistener);  //注册监听函数
    locationclientoption option = new locationclientoption();
    option.setlocationmode(locationclientoption.locationmode.battery_saving
    );//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
    option.setcoortype("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
    int span = 1000;
    option.setscanspan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
    option.setisneedaddress(true);//可选,设置是否需要地址信息,默认不需要
    option.setopengps(true);//可选,默认false,设置是否使用gps
    option.setlocationnotify(true);//可选,默认false,设置是否当gps有效时按照1s/1次频率输出gps结果
    option.setisneedlocationdescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在bdlocation.getlocationdescribe里得到,结果类似于“在北京天安门附近”
    option.setisneedlocationpoilist(true);//可选,默认false,设置是否需要poi结果,可以在bdlocation.getpoilist里得到
    option.setignorekillprocess(false);//可选,默认true,定位sdk内部是一个service,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
    option.setignorecacheexception(false);//可选,默认false,设置是否收集crash信息,默认收集
    option.setenablesimulategps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要
    mlocationclient.setlocoption(option);
    //开启定位
    mlocationclient.start();
  }

  private class mylocationlistener implements bdlocationlistener {

    @override
    public void onreceivelocation(bdlocation location) {
      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) {// gps定位结果
        sb.append("\nspeed : ");
        sb.append(location.getspeed());// 单位:公里每小时
        sb.append("\nsatellite : ");
        sb.append(location.getsatellitenumber());
        sb.append("\nheight : ");
        sb.append(location.getaltitude());// 单位:米
        sb.append("\ndirection : ");
        sb.append(location.getdirection());// 单位度
        sb.append("\naddr : ");
        sb.append(location.getaddrstr());
        sb.append("\ndescribe : ");
        sb.append("gps定位成功");

      } else if (location.getloctype() == bdlocation.typenetworklocation) {// 网络定位结果
        sb.append("\naddr : ");
        sb.append(location.getaddrstr());
        //运营商信息
        sb.append("\noperationers : ");
        sb.append(location.getoperators());
        sb.append("\ndescribe : ");
        sb.append("网络定位成功");
      } else if (location.getloctype() == bdlocation.typeofflinelocation) {// 离线定位结果
        sb.append("\ndescribe : ");
        sb.append("离线定位成功,离线定位结果也是有效的");
      } else if (location.getloctype() == bdlocation.typeservererror) {
        sb.append("\ndescribe : ");
        sb.append("服务端网络定位失败,可以反馈imei号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
      } else if (location.getloctype() == bdlocation.typenetworkexception) {
        sb.append("\ndescribe : ");
        sb.append("网络不同导致定位失败,请检查网络是否通畅");
      } else if (location.getloctype() == bdlocation.typecriteriaexception) {
        sb.append("\ndescribe : ");
        sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
      }
      sb.append("\nlocationdescribe : ");
      sb.append(location.getlocationdescribe());// 位置语义化信息
      list<poi> list = location.getpoilist();// poi数据
      if (list != null) {
        sb.append("\npoilist size = : ");
        sb.append(list.size());
        for (poi p : list) {
          sb.append("\npoi= : ");
          sb.append(p.getid() + " " + p.getname() + " " + p.getrank());
        }
      }
      log.e("描述:", sb.tostring());
    }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网