简体   繁体   English

使用融合位置提供程序获取启用/禁用位置服务的信息

[英]Get information of Location Services enabled/disabled using Fused Location Provider

I am working on an app which uses Google Maps and tracks user's location. 我正在开发一款使用Google地图并跟踪用户位置的应用。 I want to change the visibility of the "You are here!" 我想改变“你在这里!”的知名度。 marker when the user closes Location Services by hand or services goes to condition of inaccessible. 当用户手动关闭位置服务或服务进入无法访问的状态时的标记。 This method can return the information of whether Location Services enabled or not: 此方法可以返回是否启用位置服务的信息:

private boolean isLocationServicesEnabled() {
    LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER) || lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        return true;
    }
    return false;
}

However, I do not want to detect the status of Location Services for once. 但是,我不想一次检测位置服务的状态。 Whenever the user opens or closes Location Services, I should detect it and change the visibility of marker. 每当用户打开或关闭位置服务时,我都应检测到它并更改标记的可见性。 In short, this sloppy pseudocode explains what I want to listen: 简而言之,这个草率的伪代码解释了我想听的内容:

while application runs
   if location services turn off
      change marker visibility to false
   else
      change marker visibility to true 

I did not find a way to achieve this task without android.location.LocationListener, want to achieve it just using Fused Location Provider. 没有android.location.LocationListener,我没有找到实现此任务的方法,想要使用Fused Location Provider实现它。 Do you have any idea? 你有什么主意吗? Here is the core structure I used if you want to see: 如果你想看到,我使用的核心结构如下:

http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/ (Check the title of "Complete Code") http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/ (查看“完整代码”的标题)

Doing this the proper way involves a huge amount of code. 以正确的方式执行此操作涉及大量代码。 You have to make use of the location SettingsApi class. 你必须要使用的位置SettingsApi类。

The main entry point for interacting with the location settings-enabler APIs. 与位置设置启用程序API交互的主要入口点。

This API makes it easy for an app to ensure that the device's system settings are properly configured for the app's location needs. 此API使应用程序可以轻松确保为应用程序的位置需求正确配置设备的系统设置。

Fortunately there is a full blown sample provided by Google on github 幸运的是,谷歌在github上提供了一个完整的样本

How about implementing a gps listener? 如何实现gps监听器?

mLocationManager.addGpsStatusListener(new GpsStatus.Listener(){

    @Override
    public void onGpsStatusChanged(int event) {
        if(event==GpsStatus.GPS_EVENT_STARTED){
            Log.d(TAG,"Gps Started");
        }else if(event==GpsStatus.GPS_EVENT_STOPPED){
            Log.d(TAG,"Gps Stopped");
        }
    }
});

modify the above code to change visibility of your marker. 修改上述代码以更改标记的可见性。 Hope this works. 希望这有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM