目录

CLASS info
public class FusedLocationProviderClient
融合定位,程序定位交互接入类。

Public Method Summary

Return typeMethod name
Task<Void>flushLocations()
Task<Location>getLastLocation()
Task<LocationAvailability>getLocationAvailability()
Task<Void>removeLocationUpdates(LocationCallback callback)
Task<Void>removeLocationUpdates(PendingIntent callbackIntent)
Task<Void>requestLocationUpdates(LocationRequest request, LocationCallback callback, Looper looper)
Task<Void>requestLocationUpdates(LocationRequest request, PendingIntent callbackIntent)
Task<Void>setMockMode(boolean isMockMode)
Task<Void>setMockLocation(Location mockLocation)
Task<HWLocation>getLastLocationWithAddress(LocationRequest request)
Task<Void>            requestLocationUpdatesEx (LocationRequest request, LocationCallback callback, Looper looper)

Public Methods

Method
public Task<Void> flushLocations()
刷新当前正在处理的位置。

Return

typedesc
Task<Void>

任务。通过addOnSuccesslistener()和addOnFailureListener()添加监听,然后通过onSuccess()和onFailure()方法获取任务执行结果。

说明:
当前版本并未提供flushLocations()功能,SDK中为空实现。

Method
public Task<Location> getLastLocation()

返回最后一次请求的可用位置。该接口并不会主动请求位置,将使用上一次请求的缓存位置。

以下场景可能导致返回null:

  • 从未使用过定位功能。

  • 位置开关关闭。

  • 恢复出厂设置。

如果对位置的实时性有较高要求,推荐主动调用requestLocationUpdates替代调用getLastLocation。如果只想收到一次位置信息,可以将LocationRequest中numUpdates参数设置为1。

Return

typedesc
Task<Location>

任务。通过addOnSuccesslistener()和addOnFailureListener()添加监听,然后通过onSuccess()和onFailure()方法获取任务执行结果。

Method
public Task<LocationAvailability> getLocationAvailability()
返回位置数据的可用性。

Return

typedesc
Task<LocationAvailability>任务。通过添加addOnSuccesslistener()和addOnFailureListener()监听,获取当前方法执行的结果,通过getStateCode()确定是否成功(0:成功。1:失败;404:找不到资源;500:内部错误)。
Method
public Task<Void> removeLocationUpdates(LocationCallback callback)
移除指定回调信息的位置更新。

Parameters

Parameter nameParameter desc
callback要移除的回调信息。

Return

typedesc
Task<Void>任务。通过addOnSuccesslistener()和addOnFailureListener()添加监听,然后通过onSuccess()和onFailure()方法获取任务执行结果。
Method
public Task<Void> removeLocationUpdates(PendingIntent callbackIntent)
移除指定PendingIntent的位置更新。

Parameters

Parameter nameParameter desc
callbackIntent要移除的PendingIntent对象。

Return

typedesc
Task<Void>任务。通过addOnSuccesslistener()和addOnFailureListener()添加监听,然后通过onSuccess()和onFailure()方法获取任务执行结果。
Method
public Task<Void> requestLocationUpdates(LocationRequest request,LocationCallback callback,Looper looper)
请求位置更新,并在指定的Looper线程上回调。该方法要求请求方进程存在才能持续回调。如进程被杀后仍然希望能够持续收到回调,请参考requestLocationUpdates(LocationRequest request,PendingIntent callbackIntent) ,如果接口调用成功,获取位置信息失败,请参考FAQ[3]。

Parameters

Parameter nameParameter desc
request位置请求对象。
callback位置更新的回调。
looper为空时默认为当前线程的looper对象。

Return

typedesc
Task<Void>

任务。通过addOnSuccesslistener()和addOnFailureListener()添加监听,然后通过onSuccess()和onFailure()方法获取任务执行结果。

Method
public Task<Void> requestLocationUpdates(LocationRequest request,PendingIntent callbackIntent)
请求方使用PendingIntent的方式,持续请求位置更新,HMS把位置更新的结果LocationResult和LocationAvailability封装到Intent中,调用传入的PendingIntent的send()发送给请求方。如果请求方进程被杀死,使用此方法,依然能够持续回调。如果接口调用成功,获取位置信息失败,请参考FAQ[3]。

Parameters

Parameter nameParameter desc
request位置请求对象。
callbackIntent每个位置请求更新关联的PendingIntent,构造PendingIntent时需设置Class、Action,不能构造空Intent。

Return

typedesc
Task<Void>

任务。通过addOnSuccesslistener()和addOnFailureListener()添加监听,然后通过onSuccess()和onFailure()方法获取任务执行结果。

Method
public Task<Void> setMockMode(boolean isMockMode)
设置位置提供者是否使用位置模拟模式。为true时,将不再使用GPS或网络位置,最终直接返回通过setMockLocation(Location)设置的位置信息。

Parameters

Parameter nameParameter desc
isMockMode如果为true,则位置提供者将设置为模拟模式。如果为false,它将返回到正常状态。

Return

typedesc
Task<Void>

任务。通过addOnSuccesslistener()和addOnFailureListener()添加监听,然后通过onSuccess()和onFailure()方法获取任务执行结果。

Method
public Task<Void> setMockLocation(Location mockLocation)
设置具体的模拟位置。必须在调用此方法之前调用setMockMode(boolean)并设置为true。

Parameters

Parameter nameParameter desc
mockLocation模拟位置。此类为Android原生类,具体介绍请参见Android开发文档。

Return

typedesc
Task<Void>

任务。通过addOnSuccesslistener()和addOnFailureListener()添加监听,然后通过onSuccess()和onFailure()获取任务执行结果。

Method

public Task<HWLocation> getLastLocationWithAddress(LocationRequest request)

返回最后一次请求的可用位置,包括详细地址信息。如果某个位置不可用,则返回null。

Parameters

Parameter nameParameter desc
request位置请求对象。

Return

typedesc

Task<HWLocation>

任务。通过addOnSuccesslistener()和addOnFailureListener()添加监听,然后通过onSuccess()和onFailure()方法获取任务执行结果。

Method
public Task<Void> requestLocationUpdatesEx(LocationRequest request,LocationCallback callback,Looper looper)
扩展的位置信息服务接口,当前支持高精度定位,并兼容普通定位接口。如果设备不支持高精度定位或者应用没有请求高精度定位权限,会返回普通位置信息,效果同requestLocationUpdates接口。在返回的location对象中可以通过getExtras().getInt("SourceType", 0)获取位置信息的SourceType,如果将SourceType转换为二进制后倒数第四位为1则表示该位置为高精度位置。如果接口调用成功,获取位置信息失败,请参考FAQ[3]。

Parameters

Parameter nameParameter desc
request

位置请求对象。priority参数需要设置为200才能获取高精度位置信息。

callback位置更新的回调。
looper为空时默认为当前线程的looper对象。

Return

typedesc
Task<Void>任务。通过addOnSuccesslistener()和addOnFailureListener()添加监听,然后通过onSuccess()和onFailure()方法获取任务执行结果。