单页面单次/连续定位切换

您所在的位置:网站首页 高德地图页面设置 单页面单次/连续定位切换

单页面单次/连续定位切换

2024-07-09 23:38| 来源: 网络整理| 查看: 265

使用场景

该示例主要展示在同一个移动端页面内通过连续定位持续确定设备位置的同时,用户通过按钮(或其他)主动获取一次当前位置的场景。

用到产品

Android 定位 SDK

核心类/接口

接口

说明

版本

AMapLocationClientOption

setOnceLocation(Boolean b);

设置单次定位接口

V2.0.0版本起

setInterval(long time);

设置连续定位时间间隔

V2.0.0版本起

AMapLocationClient

startLocation();

启动定位

V2.0.0版本起

setLocationOption(mLocationOption);

给定位客户端设置参数

V2.0.0版本起

AMapLocationListener

onLocationChanged(AMapLocation amapLocation);

监听器回调方法

V2.0.0版本起

核心难点

分别创建单次、连续定位客户端以及监听器,这样可以有效的区分定位结果是来自持续定位还是来自单次定位。

1、分别创建定位客户端:

创建单次定位客户端:

JavaAMapLocationClient locationClientSingle = new AMapLocationClient(this.getApplicationContext());

创建连续定位客户端:

JavaAMapLocationClient locationClientContinue = new AMapLocationClient(this.getApplicationContext());

2、分别创建定位结果监听器:

单次定位的监听:

Java/** * 单次客户端的定位监听 */ AMapLocationListener locationSingleListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation location) { } }; locationClientSingle.setLocationListener(locationSingleListener);

连续定位的监听:

Java/** * 连续客户端的定位监听 */ AMapLocationListener locationContinueListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation location) { } }; locationClientContinue.setLocationListener(locationContinueListener);

3、分别设置定位行为:

设置单次定位的行为:

Java //获取一次定位结果: //该方法默认为false。 locationClientSingleOption.setOnceLocation(true); //关闭缓存机制 locationClientSingleOption.setLocationCacheEnable(false); //给定位客户端对象设置定位参数 locationClientSingle.setLocationOption(locationClientSingleOption); //启动定位 locationClientSingle.startLocation();

设置连续定位的行为:

Java //设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。 locationClientContinueOption.setInterval(1000); //给定位客户端对象设置定位参数 locationClientContinue.setLocationOption(locationClientContinueOption); //启动定位 locationClientContinue.startLocation();

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3