3

我需要对 Geofence 指南提供的示例代码有所了解,如下所示:

https://developer.android.com/training/location/geofencing.html

我运行了代码,发现地理围栏已正确创建,但我真正想要的是一种在我开车前往这些地理围栏位置时获得警报的方法。现在,当我经过那些地理围栏点时,什么也没有发生(ReceiveTransitionsIntentService 没有被调用),没有任何通知。

我是否还必须收听定期位置更新并将 lat/lng 手动传递给上述代码以指示我当前的位置?当我向 LocationClient 添加地理围栏时,我认为这应该是自动的,但我想还有更多。

我还尝试将 LocationRequest 注册到 LocationClient 实例,但仍然没有警报:

mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setFastestInterval(LocationUtils.FAST_INTERVAL_CEILING_IN_MILLISECONDS);

mLocationClient.requestLocationUpdates(mLocationRequest, (LocationListener)mActivity);

如何将 Geofence api 与位置跟踪集成?

4

7 回答 7

4

我也有问题。确保在将服务添加到 AndroidManifest 时正确定义了路径。我还必须将导出的值更改为 true。

<application>
...
    <service
        android:name=".ReceiveTransitionsIntentService"
        android:exported="true" >
    </service>
...
</application>

您还需要在 Manifest 中添加元数据标签以指定您的 google gms 版本。

<application>
...
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="4242000" />
...
</application>

我在这里创建了一个更简单的 Google 示例版本,其中包含代码和更多解释。希望有帮助!

于 2014-08-08T19:01:09.133 回答
3

不幸的是,这似乎是预期的行为。地理围栏 API 设置为侦听设备上的自动位置更新。当您在某个位置安顿下来时,这通常会起作用,但正如您所说,如果您开车穿过围栏,或者您有一个半径只有几米的围栏,您可能会错过它。据说该 API 会自动检查您的行为(步行、驾驶等)并更频繁地更新,但事实并非如此。

您会注意到,如果您打开 Google 地图并跟踪您的位置,栅栏将被正确触发。

因此,您可以假设唯一真正的解决方案是设置您自己的服务,该服务以定义的时间间隔轮询位置(Google 建议 20 秒https://www.youtube.com/watch?v=Bte_GHuxUGc),然后强制定位在这些时间间隔广播。如果地理围栏位于此位置,则会自动触发它们。

于 2014-09-11T10:43:54.287 回答
1

一些快速说明:确保您选择了正确的过渡。您可能需要同时设置入口和出口过渡。

地理围栏的代码片段不显示通知。这是故意的;通知并不总是对转换的最佳响应。示例应用程序显示一个通知,如果您下载该示例,您可以看到它。

定位服务会尽力识别您的位置。

另请记住,您必须请求 ACCESS_FINE_LOCATION。

您不会在后台“进程”中找到有关地理围栏跟踪(或与位置相关的任何其他内容)的任何具体内容。所有这些都在 Google Play 服务中。

于 2013-05-28T23:32:31.357 回答
1

@andrewmolo I had that same issue when I tried. You should set-up proper build path and library for your project. It worked for me when I setup as following.

  1. Go to Properties of your project > Android > Select Google APIs.
  2. Properties > Java Build Path > Libraries.

Ensure you have

     1. android-support-v4.jar
     2. Google APIs
     3. google-play-services_lib.jar
     4. google-play-services.jar

Also in order and export you should have Google APIs at bottom position and all other selected.

于 2013-09-05T04:44:00.163 回答
1

我对示例代码也有一些麻烦。这对我有用。

            latitude = sharedPreferences.getFloat("storeAddress_lat", -1);
            longitude = sharedPreferences.getFloat("storeAddress_lng", -1);

            final ArrayList<Geofence> list = new ArrayList<Geofence>();

            Builder builder = new Geofence.Builder();
            builder.setRequestId("near_store_geofence");
            builder.setCircularRegion(latitude, longitude, 50);
            builder.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER);
            builder.setExpirationDuration(Geofence.NEVER_EXPIRE);
            list.add(builder.build());

            builder = new Geofence.Builder();
            builder.setRequestId("leave_store_geofence");
            builder.setCircularRegion(latitude, longitude, 50);
            builder.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_EXIT);
            builder.setExpirationDuration(Geofence.NEVER_EXPIRE);
            list.add(builder.build());


            final GooglePlayServicesClient.ConnectionCallbacks connectionCallbacks = new GooglePlayServicesClient.ConnectionCallbacks(){

                @Override
                public void onConnected(Bundle connectionHint) {
                    Log.i("GEOFENCE","onConnected");


                    // Create an explicit Intent
                    Intent intent = new Intent(getBaseContext(),  ReceiveTransitionsIntentService.class);
                    /*
                     * Return the PendingIntent
                     */
                    PendingIntent pendingIntent =  PendingIntent.getService(
                            getBaseContext(),
                            0,
                            intent,
                            PendingIntent.FLAG_UPDATE_CURRENT);

                    locationClient.addGeofences(list,pendingIntent, new OnAddGeofencesResultListener(){public void onAddGeofencesResult(int statusCode, String[] geofenceRequestIds) {
                        Log.i("GEOFENCE","onAddGeofencesResult");
                    }});

                    LocationRequest locationrequest = LocationRequest.create();
                    locationrequest.setPriority(locationrequest.PRIORITY_HIGH_ACCURACY);
                    locationrequest.setInterval(5000);

                    locationClient.requestLocationUpdates(locationrequest, new LocationListener(){

                        @Override
                        public void onLocationChanged(Location location) {
                            // TODO Auto-generated method stub

                        }});


                }

                @Override
                public void onDisconnected() {
                    Log.i("GEOFENCE","onDisconnected");                     
                }
            };
            GooglePlayServicesClient.OnConnectionFailedListener onConnectionFailedListener = new GooglePlayServicesClient.OnConnectionFailedListener(){

                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.i("GEOFENCE","onConnectionFailed");

                }
            };
            locationClient = new LocationClient(getBaseContext(), connectionCallbacks, onConnectionFailedListener);

            locationClient.connect();
于 2013-10-17T18:39:09.890 回答
1

地理围栏 API 永远不会启用 GPS(这很糟糕,我需要响应式地理围栏该死的并且不关心功耗)。文档中的任何地方也没有提到这一点。此外,如果没有 GPS,由于 wifi/cell 缺乏准确性,您不可能希望拥有更小的地理围栏。

您必须使用 null 处理程序单独轮询 GPS,以便为该 null 处理程序提供的有价值的位置也提供给地理围栏 API,从而可靠地触发您的地理围栏。

于 2014-03-19T18:00:03.897 回答
0

请注意,该示例包含一个错误,该错误将使您的围栏在 12 分钟后过期,而不是预期的 12 小时...修复更改

private static final long SECONDS_PER_HOUR = 60;

private static final long SECONDS_PER_HOUR = 60*60;

在 MainActivity

于 2013-06-27T08:48:00.097 回答