【发布时间】:2021-09-14 15:29:59
【问题描述】:
我想知道是否可以得到一些帮助,我正在创建一个使用点对象来创建路线的应用程序,我在每条路线上都有某些点作为“航路点”,并且有了它,所以这些点中的每一个都可以绑定到地理围栏。
到目前为止,我可以触发地理围栏事件等,但我想(如果可能)获取有关实际“点”的信息,例如它的描述等以显示在另一个活动中,地理围栏的意思是代表,我不确定如何实现这一点,我似乎只能获得地理围栏的 ID,任何帮助将不胜感激!
这是我的 BroadcastReceiver 类
public class GeofenceBroadcastReceiver extends BroadcastReceiver {
private static final Object TAG = "Error";
NotifactionHelper notifactionHelper;
@Override
public void onReceive(Context context, Intent intent) {
notifactionHelper = new NotifactionHelper(context);
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
Log.d("geoferror", "onReceive: Geofence even has error..");
}
List<Geofence> triggeredGeofenceList = geofencingEvent.getTriggeringGeofences();
for (Geofence geofence : triggeredGeofenceList) {
Log.d("GEOF", "onReceive: "+geofence.getRequestId());
}
Location triggerLocation = geofencingEvent.getTriggeringLocation();
double lat = triggerLocation.getLatitude();
double lon = triggerLocation.getLongitude();
int transitionType = geofencingEvent.getGeofenceTransition();
switch (transitionType) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
Toast.makeText(context, "Entered Geofence", Toast.LENGTH_SHORT).show();
Log.d("GEOF", "onReceive: "+geofencingEvent.getGeofenceTransition());
notifactionHelper.sendNotification("A Waypoint is Nearby!", "Open the Application to find out more", RouteActivity.class);
break;
case Geofence.GEOFENCE_TRANSITION_DWELL:
Toast.makeText(context, "Dwelling inside of Geofence", Toast.LENGTH_SHORT).show();
break;
case Geofence.GEOFENCE_TRANSITION_EXIT:
Toast.makeText(context, "Exited Geofence area", Toast.LENGTH_SHORT).show();
break;
}
Bundle b = intent.getExtras();
Intent i = new Intent("TESTER");
i.putExtra("lat", lat);
i.putExtra("lon", lon);
i.putExtras(b);
context.sendBroadcast(i);
}
}
也是我的地理围栏方法,它从列表中的每个点获取纬度和经度来创建地理围栏
private void addGeoFence(LatLng latLng, float radius) {
List<Geofence> geofence = geoFenceHelper.getGeoFence(GEOFENCE_ID, latLng, radius,
Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT);
GeofencingRequest geofencingRequest = geoFenceHelper.getGeoFencingRequest(geofence);
PendingIntent pendingIntent = geoFenceHelper.getPendingIntent();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
geofencingClient.addGeofences(geofencingRequest, pendingIntent)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void unused) {
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull @NotNull Exception e) {
}
});
}
还有我的 geofenceHelper 类
public class GeoFenceHelper extends ContextWrapper {
private static final java.util.UUID UUID = null;
PendingIntent pendingIntent;
/** Constructor
*
* @param base
*/
public GeoFenceHelper(Context base) {
super(base);
}
public GeofencingRequest getGeoFencingRequest(List<Geofence> geofence) {
return new GeofencingRequest.Builder()
.addGeofences(geofence)
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
.build();
}
/**
*
* @param ID
* @param latLng
* @param radius
* @param transitionTypes
* @return
*/
public List <Geofence> getGeoFence (String ID, LatLng latLng, float radius, int transitionTypes) {
List<Geofence> returnGeoFenceList = new ArrayList<>();
String id = UUID.randomUUID().toString();
returnGeoFenceList.add(
new Geofence.Builder()
.setCircularRegion(latLng.latitude, latLng.longitude, radius)
.setRequestId(id)
.setTransitionTypes(transitionTypes)
.setLoiteringDelay(2000)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build());
return returnGeoFenceList;
}
/**
*
* @return
*/
public PendingIntent getPendingIntent() {
if(pendingIntent != null) {
return pendingIntent;
}
Intent intent = new Intent(this, GeofenceBroadcastReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this,2607, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
}
感谢您抽出宝贵时间阅读本文!
【问题讨论】:
-
such as its description etc请说到重点。究竟是什么?准确! -
抱歉,每个点都是一个对象,包含它的 LatLng、点的字符串描述和枚举 PointType,当地理围栏被触发时,我希望我的活动向我展示更多关于点的信息我已经达到了,比如它的描述和 LatLng 在另一个活动中。
-
您甚至没有解释“描述”的含义。是你解释了“等”。
-
再次抱歉.. 'description' 是 Point 对象的实例变量,可用于添加包含有关特定 Point 的更多信息的描述。
标签: android broadcastreceiver android-geofence