首页 > 解决方案 > 谷歌地图的地理围栏和准确性,以使用付费版本的谷歌地图 API 定位特定地点

问题描述

我正在尝试开发一个 android 应用程序,我想从 google maps API 获取以下详细信息。

  1. 是否可以使用付费版本的谷歌地图 API 为特定地点设置地理围栏(矩形或圆形)?如果是,那么我可以从谷歌地图 API 获得的准确度是多少(特定地点的米或英尺实习生)。

  2. 我可以使用谷歌地图 API 读取用户在特定地点停留的时间吗?

  3. 当用户在特定地点停留一段时间(如第 2 点所述)时,android 操作系统是否可以在该手机中通知我的应用程序?

对于以上三个功能,我是否必须选择付费版的谷歌地图 API?或者也可以使用免费版本的谷歌地图 API 来完成?

标签: androidgoogle-mapsgoogle-maps-api-3google-maps-markers

解决方案


对于所有三个问题,答案都是肯定的。第一个,你可以在Geofence的Builder中确定你想要得到的精度,像这样

new Geofence.Builder()
            .setRequestId(key)
            .setCircularRegion(lat, lang, 150)
            .setExpirationDuration(Geofence.NEVER_EXPIRE)
            .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
            .setLoiteringDelay(1000)
            .build();

我将精度设置为 150m(您应该知道的一件事是,您设置的精度越高,使用的功率就越大)

对于第二个和第三个,您可以将 TransitionTypes 设置为 Geofence.GEOFENCE_TRANSITION_DWELL 以了解用户是否在一个地方停留一段时间。同时,当这个条件匹配时,你可以使用 PendingIntent 发送一个广播。完整代码如下

Geofence geofence = getGeofence(lat, lng, key);
    geofencingClient.addGeofences(
            getGeofencingRequest(geofence),
            getGeofencePendingIntent(title, location, id))
            .addOnCompleteListener(task -> {
                if (task.isSuccessful()) {

                }else{

                }
            });

getGeofencingRequest 的代码

private GeofencingRequest getGeofencingRequest(Geofence geofence) {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofence(geofence);
    return builder.build();
}

getGeofencePendingIntent 的代码

private PendingIntent getGeofencePendingIntent(String title, String location, long id) {
    Intent i = new Intent("add your unique ID for the broadcast");
    Bundle bundle = new Bundle();
    bundle.putLong(Reminder.ID, id);
    bundle.putString(Reminder.LOCATION_NAME, location);
    bundle.putString(Reminder.TITLE, title);
    i.putExtras(bundle);
    return PendingIntent.getBroadcast(
            getContext(),
            (int) id,
            i,
            0
    );
}

推荐阅读