项目需要做一个管道巡检的模块,需要用到实时定位。实验了好几种办法,最后还是回归原生应用了。
项目的app是用vue做的,但是定位那叫一个难受,必须用原生应用的辅助定位才能做到准确。最后还是放弃了,回归原生。
定位大概用了以下几种:
1.cordova 的定位插件:
? ? 百度,高德都用了,但是效果都不理想。
2.h5定位:
? ? 信号强的点没问题,但是我们的应用场景都是信号偏弱的地方,也放弃了。
3.百度原生:
? ? 最后选用了这种,即使放在后台运行,已然很准确。而且可以辅助h5定位,暂时没时间研究,就直接上原生的了。
? ? 简单介绍下怎么用。
? ? ① 百度地图sdk 的初始化,这里放在了一个application中。
? ? ? ? ?走过弯路,一开始放在启动页面里。正常页面开着没问题,一旦页面进入后台运行,定位就停了。
@override
public void oncreate() {
super.oncreate();
sdkinitializer.initialize(getapplicationcontext());
sdkinitializer.setcoordtype(coordtype.bd09ll);
mcontext = getapplicationcontext();
}
②? 地图组件初始化,在需要定位的页面中即可。
? ? ?需要注册一个监听器locationlistener,监听定位完成的事件。
private void initlocates() {
// 定位初始化
mclient = new locationclient(this);
locationclientoption moption = new locationclientoption();
moption.setscanspan(5000);
moption.setcoortype("bd09ll");
moption.setisneedaddress(true);
moption.setopengps(true);
mclient.setlocoption(moption);
mclient.registerlocationlistener(mylocationlistener);
//设置后台定位
//android8.0及以上使用notificationutils
if (build.version.sdk_int >= 26) {
mnotificationutils = new notificationutils(this);
notification.builder builder2 = mnotificationutils.getandroidchannelnotification
("掌上巡检", "正在后台定位");
notification = builder2.build();
} else {
//获取一个notification构造器
notification.builder builder = new notification.builder(mainactivity.this);
intent nfintent = new intent(mainactivity.this, mainactivity.class);
builder.setcontentintent(pendingintent.
getactivity(mainactivity.this, 0, nfintent, 0)) // 设置pendingintent
.setcontenttitle("掌上巡检") // 设置下拉列表里的标题
.setsmallicon(r.drawable.ic_launcher_foreground) // 设置状态栏内的小图标
.setcontenttext("正在后台定位") // 设置上下文内容
.setwhen(system.currenttimemillis()); // 设置该通知发生的时间
notification = builder.build(); // 获取构建好的notification
}
notification.defaults = notification.default_sound; //设置为默认的声音
//开启后台定位
mclient.enablelocinforeground(1, notification);
mclient.start();
}
③ 监听器
这里对4.9e-324坐标做了处理,手机没信号的时候回定位到几内亚湾,本初子午线和赤道的焦点。
class mylocationlistener extends bdabstractlocationlistener {
@override
public void onreceivelocation(bdlocation bdlocation) {
if (bdlocation == null || mmapview == null ||
bdlocation.getlongitude() == 4.9e-324 || bdlocation.getlatitude() == 4.9e-324) {
return;
}
mylocationdata locdata = new mylocationdata.builder().accuracy(bdlocation.getradius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(bdlocation.getdirection()).latitude(bdlocation.getlatitude())
.longitude(bdlocation.getlongitude()).build();
// 设置定位数据
mbaidumap.setmylocationdata(locdata);
//地图sdk处理
if (isfirstloc) {
isfirstloc = false;
latlng ll = new latlng(bdlocation.getlatitude(),
bdlocation.getlongitude());
mapstatus.builder builder = new mapstatus.builder();
builder.target(ll).zoom(18.0f);
mbaidumap.animatemapstatus(mapstatusupdatefactory.newmapstatus(builder.build()));
}
latlng point = new latlng(bdlocation.getlatitude(), bdlocation.getlongitude());
overlayoptions dotoption = new dotoptions().center(point).color(0xaaa9a9a9);
mbaidumap.addoverlay(dotoption);
currentlat = point.latitude;
currentlng = point.longitude;
if (isuploadingposition) {
uploadposition(bdlocation);
}
}
}
大致的代码基本是这些,具体的可以参照官方的demo。
再说下后台运行,需要手机端开放自启动和关闭电源优化。省电策略那里设置到无限制。以小米手机为例:
?