我正在尝试在我的应用程序中实现基于多边形的地理围栏,并试图避免添加任何新库。我也没有在应用程序中使用任何地图。到目前为止,我已经找到Stefan Bangel's tutorial 来确定一个点是否在多边形内部。在checkInside()之内,我正在检查它是否在多边形内部。但是,我不知道如何根据我所拥有的地理围栏代码来制定地理围栏动作,主要是针对圆形区域。我可以操纵GeofenceTransition值吗?我可以用geofencingEvent.getTriggeringLocation()做些什么吗?我将在下面发布我的代码参考。如果您需要更多参考,请告诉我。
在获得纬度,经度值后的HomeActivity内:
double dbl_centr_lat = Double.parseDouble(computeCentroid(alst_location).getLat());
double dbl_centr_lng = Double.parseDouble(computeCentroid(alst_location).getLng());
createGeofences(dbl_centr_lat,dbl_centr_lng);
Double[] dbl_arr = new Double[alst_location.size()];
for(int i = 0;i< alst_location.size();i++)
{
double dbl_var_lat = Double.parseDouble(alst_location.get(i).getLat());
double dbl_var_lng = Double.parseDouble(alst_location.get(i).getLng());
dbl_arr[i] = distanceBetweenCoordinates(dbl_centr_lat, dbl_centr_lng, dbl_var_lat, dbl_var_lng);
}
List<Double> list = Arrays.asList(dbl_arr);
double dbl_max_radius = Collections.max(list);
Log.d("maximumdisHome", Arrays.toString(dbl_arr));
Log.d("maximumdisHome", Collections.max(list)+"");
PointObject[] points = new PointObject[alst_location.size()];
for (int i = 0; i< alst_location.size();i++)
{
PointObject pointObject = new PointObject(Double.parseDouble(alst_location.get(i).getLat()),
Double.parseDouble(alst_location.get(i).getLng()));
points[i] = pointObject;
}
PolygonObject polygonObject = new PolygonObject(points);
Log.d("checkinside", checkInside(polygonObject,dbl_centr_lat,dbl_centr_lng)+"");
JSONArray Channels =data.getJSONArray("Channels");
for (int i = 0; i < Channels.length(); i++) {
JSONObject obj = Channels.getJSONObject(i);
alst_channels.add(obj.getString("ChannelName"));
}
try{
LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
getGeofencingRequest(),
getGeofencePendingIntent()
).setResultCallback(new ResultCallback<com.google.android.gms.common.api.Status>() {
@Override
public void onResult(com.google.android.gms.common.api.Status status) {
if (status.isSuccess()) {
Log.i("geofence", "Saving Geofence");
} else {
Log.e("geofence", "Registering geofence failed: " + status.getStatusMessage() +
" : " + status.getStatusCode());
}
}
});
} catch (SecurityException securityException) {
// Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
Log.e("geofence", "Error");
}
现在在GeofenceTransitionIntentService.java中:
public class GeofenceTransitionsIntentService extends IntentService {
private static final String TAG = "GeofenceTransitions";
SharedPreferences shrp_sharedpreferences;
public GeofenceTransitionsIntentService() {
super("GeofenceTransitionsIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "onHandleIntent");
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
Log.e(TAG, "Goefencing Error " + geofencingEvent.getErrorCode());
return;
}
// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();
Log.i(TAG, "geofenceTransition = " + geofenceTransition + " Enter : " + Geofence.GEOFENCE_TRANSITION_ENTER + "Exit : " + Geofence.GEOFENCE_TRANSITION_EXIT);
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER || geofenceTransition == Geofence.GEOFENCE_TRANSITION_DWELL){
showNotification("Entered", "Entered the Location");
shrp_sharedpreferences = getSharedPreferences("shrp_loc", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = shrp_sharedpreferences.edit();
editor.putString("location", "entered");
editor.commit();
}
else if(geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
Log.i(TAG, "Showing Notification...");
showNotification("Exited", "Exited the Location");
shrp_sharedpreferences = getSharedPreferences("shrp_loc", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = shrp_sharedpreferences.edit();
editor.putString("location", "exited");
editor.commit();
} else {
// Log the error.
showNotification("Error", "Error");
Log.e(TAG, "Error ");
}
}
public void showNotification(String text, String bigText) {
// 1. Create a NotificationManager
NotificationManager notificationManager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
// 2. Create a PendingIntent for AllGeofencesActivity
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 3. Create and send a notification
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.mic)
.setContentTitle("xxxx")
.setContentText(text)
.setContentIntent(pendingNotificationIntent)
.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true)
.build();
notificationManager.notify(0, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("anitest",intent.getExtras().get("test")+"");
return super.onStartCommand(intent, flags, startId);
}
}
[1]: http://stefanbangels.blogspot.in/2013/10/geo-fencing-sample-code.html