java - 获取进入的地理围栏的ID

标签 java list google-maps android-geofence

我通过添加来自 sqlite 表的坐标制作了两个地理围栏。它确实会在 map 上创建地理围栏,并且也会发生转换。

我的问题是所有的转换都会触发相同的广播,这意味着无论我进入哪两个地理围栏,它都会显示相同的通知。

如何获取特定于位置的消息?

我已经使用这行代码来获取触发的栅栏的 ID,这工作正常,但它也提供了不需要的东西,例如经度、纬度和半径。如何从中提取 ID?

List<Geofence> ab = LocationClient.getTriggeringGeofences(intent);

这是代码的相关部分。

 @Override
        public void onConnected(Bundle arg0) {

            mClient.requestLocationUpdates(mRequest, this);
            SQLiteDatabase db = database.getWritableDatabase();
            String[] columns = {PromoDatabase.LID,PromoDatabase.lRestuarantID,PromoDatabase.lBranchID,PromoDatabase.Latitude,PromoDatabase.Longitude};
            Cursor cursor = db.query(PromoDatabase.LOCATION_TABLE, columns, null, null, null, null, null);
            String RestuarantName = null;


                while(cursor.moveToNext())  //create the two geofences with with help of sqlite table
                {

                    String LocationID  = cursor.getString(0);
                    String RestuarantID  = cursor.getString(1);
                    double latitude = cursor.getDouble(3); 
                    double longitude = cursor.getDouble(4); 

                    RestuarantName = getData(RestuarantID); //get the restuarant name by giving the RestuarantID


                    float radius = 800;
                    // Build a Geofence
                    Geofence fence = new Geofence.Builder()
                    .setRequestId(LocationID)
                    .setCircularRegion(latitude, longitude, radius)
                    .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
                    .setExpirationDuration(Geofence.NEVER_EXPIRE)
                    .build();
                    mList.add(fence);

                    googleMap.addMarker( new MarkerOptions()
                      .position( new LatLng(latitude, longitude) )
                      .title(RestuarantName+": Fence " + LocationID)
                      .snippet("Radius: " + radius) ).showInfoWindow();


                        circleOptions = new CircleOptions()
                          .center( new LatLng(latitude, longitude) )
                          .radius( radius )
                          .fillColor(0x40ff0000)
                          .strokeColor(Color.TRANSPARENT)
                          .strokeWidth(2);
                        googleMap.addCircle(circleOptions);

                }


            // Method 2: Using Broadcast
            Intent intent = new Intent();
            intent.setAction(GeofenceEventReceiver.GEOFENCE_EVENTS); // Specify the action, a.k.a. receivers
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.putExtra("Location", "KFC");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            // Send out the Geofence request

            mClient.addGeofences(mList, pendingIntent, this);

        }


        // Broadcast receiver used to receive broadcast sent from the GeofenceIntentService
            public class GeofenceEventReceiver extends BroadcastReceiver {
                public static final String GEOFENCE_EVENTS = "com.Drogo.proto.GeofenceEvents";

                    @Override
                    public void onReceive(Context context, Intent intent) {
                        String locationInfo = "Arraived at " + intent.getStringExtra("Location");


                        List<Geofence> BreachedGeofence = LocationClient.getTriggeringGeofences(intent); 

                        Toast.makeText(context, locationInfo + BreachedGeofence, Toast.LENGTH_LONG).show();

                        notifyMe(context,intent, locationInfo);   //this will basically give me a notification through the notification manager.

                    }

最佳答案

使用

List <Geofence> triggerList = getTriggeringGeofences(intent);

String[] triggerIds = new String[geofenceList.size()];

for (int i = 0; i < triggerIds.length; i++) {
    triggerIds[i] = triggerList.get(i).getRequestId();
}

摘自 Android 文档 here .

你想要一些不同的东西吗?

关于java - 获取进入的地理围栏的ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23641950/

相关文章:

java - 回滚 RuntimeException : org. springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback;错误的 SQL 语法 [ - Spring Batch 问题

java - 在 Java 中管理与数据库的多个连接

javascript - 更改当前 li 类(被点击的那个)

python - 生成 生成 numpy 数组

android - 如何在 map 中心的谷歌地图中获取位置(经纬度/名称)

android - 使用 JSON 的谷歌地图中的多个标记

java - 使用 Java RegEx 查找以大写字母开头并包含小写字母的单词

list - 返回最频繁的 OCaml 函数

php - 尝试用 MySQL、PHP 和谷歌地图做商店定位器

java - JAVA如何添加一个按钮来卡住和恢复弹跳球