【问题标题】:Get current location from the geo fence sample project从地理围栏示例项目中获取当前位置
【发布时间】:2014-11-13 16:01:40
【问题描述】:

我正在试用 Google 的地理围栏示例项目。我正在正确获取进入/退出事件。但是我怎样才能从中获取用户的当前位置。即使在进入/退出点之后,我也想跟踪用户位置。请帮忙。 sample project is from the developer portal.

【问题讨论】:

    标签: geolocation location latitude-longitude geofencing android-geofence


    【解决方案1】:

    在您的广播接收器或服务中,您会获得一个 GeofencingEvent ,使用它来获取触发位置

    public class GeofenceTransitionsIntentService extends IntentService {
    
        //[...]
    
        @Override
        protected void onHandleIntent(Intent intent) {
            GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
            if (geofencingEvent.hasError()) {
                // ...
            }
    
            Location l = geofencingEvent.getTriggeringLocation()
    
        }
        //[...]
    }
    

    【讨论】:

      【解决方案2】:

      您需要以正常方式从 Fused Location Provider 获取用户位置。完整教程在这里:http://developer.android.com/training/location/retrieve-current.html

      但基本上,当您在 Receiver 或 Service 中接收到地理围栏事件时,会在那里获取一个 LocationClient 的实例,连接到它,然后在回调中,onConnected(),获取最后一个已知位置:

      Location position = locationClient.getLastLocation();
      

      在 99.9% 的情况下,或者稍微好一点的情况下,您会发现此位置位于您的地理围栏内。

      要继续跟踪用户位置更新,请关注接收位置更新:http://developer.android.com/training/location/receive-location-updates.html

      【讨论】:

      • 嗨,为了消除我的困惑,这是否意味着如果我使用地理围栏,我不必使用网络或 gps 跟踪用户位置?地理围栏本身就可以吗?
      • locationClient.getLastLocation() 返回当前位置并且没有一个触发事件(它们很接近但不一样),GeofencingEvent.getTriggeringLocation() 已经存在而不使用已弃用的 LocationClient
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 2013-12-02
      相关资源
      最近更新 更多