欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android百度地图定位、显示用户当前位置

程序员文章站 2022-07-04 09:39:29
本文实例为大家分享了android百度地图定位、显示用户当前位置的工具类,供大家参考,具体内容如下 1、构建定位option的工具类 import com.ba...

本文实例为大家分享了android百度地图定位、显示用户当前位置的工具类,供大家参考,具体内容如下

1、构建定位option的工具类

import com.baidu.location.locationclientoption;

/**
 * 建造 locationclientoption 项
 *
 * @author peter 2018-12-21 10:58
 */
public class locationclientoptionbuilder {
 private locationclientoption option;

 public static locationclientoptionbuilder builder() {
  locationclientoptionbuilder builder = new locationclientoptionbuilder();
  builder.option = builder.initoption();
  return builder;
 }

 /**
  * 设置坐标系
  *
  * @return
  * @see coortype
  */
 public locationclientoptionbuilder setcoortype() {
  return setcoortype(coortype.bd09ll);
 }

 public locationclientoptionbuilder setcoortype(coortype coortype) {
  this.option.setcoortype(coortype.name());
  return this;
 }

 /**
  * 连续定位
  * 可选,设置发起定位请求的间隔,int类型,单位ms
  * 如果设置为0,则代表单次定位,即仅定位一次,默认为0
  * 如果设置非0,需设置1000ms以上才有效
  *
  * @param time
  * @return
  */
 public locationclientoptionbuilder setscanspan(int time) {
  this.option.setscanspan(time);
  return this;
 }

 public locationclientoption bulid() {
  return this.option;
 }

 private locationclientoption initoption() {
  locationclientoption option = new locationclientoption();
  //可选,设置定位模式,默认高精度
  //locationmode.hight_accuracy:高精度;
  //locationmode. battery_saving:低功耗;
  //locationmode. device_sensors:仅使用设备;
  option.setlocationmode(locationclientoption.locationmode.hight_accuracy);
  //可选,设置返回经纬度坐标类型,默认gcj02
  //gcj02:国测局坐标;
  //bd09ll:百度经纬度坐标;
  //bd09:百度墨卡托坐标;
  //海外地区定位,无需设置坐标类型,统一返回wgs84类型坐标
//  option.setcoortype("bd09ll");

  //可选,设置发起定位请求的间隔,int类型,单位ms
  //如果设置为0,则代表单次定位,即仅定位一次,默认为0
  //如果设置非0,需设置1000ms以上才有效
//  option.setscanspan(1000);

  //可选,设置是否使用gps,默认false
  //使用高精度和仅用设备两种定位模式的,参数必须设置为true
  option.setopengps(true);

  //可选,设置是否当gps有效时按照1s/1次频率输出gps结果,默认false
//  option.setlocationnotify(true);
  //可选,定位sdk内部是一个service,并放到了独立进程。
  //设置是否在stop的时候杀死这个进程,默认(建议)不杀死,即setignorekillprocess(true)
//  option.setignorekillprocess(true);

  //可选,设置是否收集crash信息,默认收集,即参数为false
//  option.setignorecacheexception(false);

  //可选,v7.2版本新增能力
  //如果设置了该接口,首次启动定位时,会先判断当前wi-fi是否超出有效期,若超出有效期,会先重新扫描wi-fi,然后定位
//  option.setwificachetimeout(5 * 60 * 1000);
  //可选,设置是否需要过滤gps仿真结果,默认需要,即参数为false
//  option.setenablesimulategps(false);
  return option;
 }

 /**
  * 坐标系
  */
 public enum coortype {
  gcj02,
  bd09,
  bd09ll
 }
}

2、构建定位的工具类

import android.content.context;
import android.support.annotation.nonnull;
import android.util.log;

import com.baidu.location.bdabstractlocationlistener;
import com.baidu.location.bdlocation;
import com.baidu.location.locationclient;
import com.baidu.location.locationclientoption;

/**
 * 百度地图定位工具类
 *
 * @author peter 2018-12-21 10:12
 */
public class bmaplocationhelper {

 private static final int location_success = 1;
 static final int location_fail = -1;
 private locationclient mlocationclient;
 private mylocationlistener mylistener = new mylocationlistener();
 private locationcallback callback;


 private bmaplocationhelper(locationcallback callback) {

  this.callback = callback;
 }

 public static bmaplocationhelper create(@nonnull context context, @nonnull locationclientoption option, @nonnull locationcallback callback) {
  bmaplocationhelper bmaplocationhelper = new bmaplocationhelper(callback);
  locationclient client = new locationclient(context);
  client.setlocoption(option);
  //声明locationclient类
  client.registerlocationlistener(bmaplocationhelper.mylistener);
  bmaplocationhelper.mlocationclient = client;
  return bmaplocationhelper;
 }

 /**
  * 开始定位
  */
 public void locstart() {
  mlocationclient.start();
 }

 /**
  * 停止定位
  */
 public void locstop() {
  mlocationclient.stop();
 }

 public void locrestart() {
  mlocationclient.restart();
 }

 public locationclient getmlocationclient() {
  return mlocationclient;
 }

 /**
  * 地图定位结果监听类
  */
 public class mylocationlistener extends bdabstractlocationlistener {
  private static final string tag = "mylocationlistener";

  @override
  public void onreceivelocation(bdlocation location) {
   if (location == null) return;
   int loctype = location.getloctype();
   int status = location_success;
   if (loctype != 61 && loctype != 161 && loctype != 66) status = location_fail;
   string errmsg = getlocationresultmsg(loctype);
   callback.onreceivelocation(status, location, errmsg);
  }

  @override
  public void onlocdiagnosticmessage(int i, int i1, string s) {
   log.i(tag, "onlocdiagnosticmessage: " + i + "diatype:" + i1);
   callback.onlocdiagnosticmessage(i, i1, getlocdiagnosticmessage(i, i1));
   super.onlocdiagnosticmessage(i, i1, s);
  }
 }


 /**
  * 回调类
  */
 public abstract static class locationcallback {

  /**
   * 定位的结果
   *
   * @param statuscode 状态码,1:定位成功,-1定位失败
   * @param bdlocation 定位成功时返回的定位结果对象
   * @param errmsg  定位失败时的错误信息,成功时则为null
   */
  public abstract void onreceivelocation(int statuscode, bdlocation bdlocation, string errmsg);

  /**
   * 错误的状态码
   * <a>http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/addition-func/error-code</a>
   * <p>
   * 回调定位诊断信息,开发者可以根据相关信息解决定位遇到的一些问题
   *
   * @param loctype   当前定位类型
   * @param diagnostictype 诊断类型(1~9)
   * @param diagnosticmessage 具体的诊断信息释义
   */
  public void onlocdiagnosticmessage(int loctype, int diagnostictype, string diagnosticmessage) {
  }
 }

 /**
  * 错误的状态码
  * <a>http://lbsyun.baidu.com/index.php?title=android-locsdk/guide/addition-func/error-code</a>
  *
  * @param loctype 当前定位类型
  * @return string 定位成功或失败的信息
  */
 private string getlocationresultmsg(int loctype) {
  switch (loctype) {
   case 61:
    return "gps定位结果,gps定位成功";
   case 62:
    return "无法获取有效定位依据,定位失败,请检查运营商网络或者wifi网络是否正常开启,尝试重新请求定位";
   case 63:
    return "网络异常,没有成功向服务器发起请求,请确认当前测试手机网络是否通畅,尝试重新请求定位";
   case 66:
    return "离线定位结果。通过requestofflinelocaiton调用时对应的返回结果";
   case 67:
    return "离线定位失败";
   case 161:
    return "网络定位结果,网络定位成功";
   case 162:
    return "请求串密文解析失败,一般是由于客户端so文件加载失败造成,请严格参照开发指南或demo开发,放入对应so文件";
   case 167:
    return "服务端定位失败,请您检查是否禁用获取位置信息权限,尝试重新请求定位";
   case 505:
    return "ak不存在或者非法,请按照说明文档重新申请ak";
   default:
    return "";
  }
 }

 /**
  * @param loctype  当前定位类型
  * @param diagnostictype 诊断类型(1~9)
  * @return string
  */
 private string getlocdiagnosticmessage(int loctype, int diagnostictype) {

  switch (loctype) {
   case 62:
    switch (diagnostictype) {
     case 4:
      return "定位失败,无法获取任何有效定位依据";
     case 5:
      return "定位失败,无法获取有效定位依据,请检查运营商网络或者wi-fi网络是否正常开启,尝试重新请求定位";
     case 6:
      return "定位失败,无法获取有效定位依据,请尝试插入一张sim卡或打开wi-fi重试";
     case 7:
      return "定位失败,飞行模式下无法获取有效定位依据,请关闭飞行模式重试";
     case 9:
      return "定位失败,无法获取任何有效定位依据";
    }
   case 67:
    if (diagnostictype == 3) return "定位失败,请您检查您的网络状态";

   case 161:
    switch (diagnostictype) {
     case 1:
      return "定位失败,建议您打开gps";
     case 2:
      return "定位失败,建议您打开wi-fi";
    }
   case 167:
    if (diagnostictype == 8) return "定位失败,请确认您定位的开关打开状态,是否赋予app定位权限";

   default:
    return "未知错误";
  }
 }
}

3、显示用户当前位置到地图的工具类

import android.content.context;
import android.support.annotation.nonnull;
import android.util.log;

import com.baidu.location.bdlocation;
import com.baidu.location.locationclientoption;
import com.baidu.mapapi.map.baidumap;
import com.baidu.mapapi.map.mapstatus;
import com.baidu.mapapi.map.mapstatusupdatefactory;
import com.baidu.mapapi.map.mylocationdata;
import com.baidu.mapapi.model.latlng;
import com.yikesong.sender.util.toastutils;

/**
 * 显示用户当前位置到地图上
 *
 * @author peter 2018-12-21 16:27
 */
public class userlocation extends bmaplocationhelper.locationcallback {
 private context context;
 private boolean isfirstloc = true;
 private baidumap map;
 private int mcurrentdirection = 0;
 private bmaplocationhelper helper;
 private static final string tag = "userlocation";

 public userlocation(@nonnull context context, @nonnull baidumap map) {
  this.context = context;
  this.map = map;
  init();
 }

 private void init() {
  map.setmaptype(baidumap.map_type_normal);
  //开启定位图层
  map.setmylocationenabled(true);
 }

 /**
  * 在地图上显示用户的当前位置
  */
 public void showuserlocationonmap() {
  if (helper == null) {
   locationclientoption option = locationclientoptionbuilder
     .builder()
     .setcoortype()
     .bulid();
   helper = bmaplocationhelper.create(this.context, option, this);
  }
  helper.locstart();
 }

 @override
 public void onreceivelocation(int statuscode, bdlocation bdlocation, string errmsg) {
  if (statuscode == bmaplocationhelper.location_fail) {
   toastutils.toastinfo(errmsg, context);
   log.i(tag, "onreceivelocation: " + errmsg);
   return;
  }
  mylocationdata locdata = new mylocationdata.builder()
    .accuracy(bdlocation.getradius())
    // 此处设置开发者获取到的方向信息,顺时针0-360
    .direction(mcurrentdirection).latitude(bdlocation.getlatitude())
    .longitude(bdlocation.getlongitude()).build();
  map.setmylocationdata(locdata);
  if (isfirstloc) {
   isfirstloc = false;
   latlng centerpoint = new latlng(bdlocation.getlatitude(),
     bdlocation.getlongitude());
   mapstatus mapstatus = new mapstatus.builder()
     .target(centerpoint) //设置中心点
     .zoom(18f)//设置缩放级别
     .build();
   map.animatemapstatus(mapstatusupdatefactory.newmapstatus(mapstatus));
  }
  if (helper != null) helper.locstop();
 }

 @override
 public void onlocdiagnosticmessage(int loctype, int diagnostictype, string diagnosticmessage) {
  super.onlocdiagnosticmessage(loctype, diagnostictype, diagnosticmessage);
 }

 public bmaplocationhelper gethelper() {
  return helper;
 }

 public baidumap getmap() {
  return map;
 }

 public int getmcurrentdirection() {
  return mcurrentdirection;
 }
}

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