геозона: - хотите отобразить тост на разных геозонах - PullRequest
0 голосов
/ 18 февраля 2019

Я хочу создать условие для всей геозоны i.e, когда я вхожу в разные геозоны, он показывает, что я нахожусь в этой конкретной геозоне

ConstantData

открытый класс ConstantData {

private ConstantData(){

}
public static final String PACKAGE_NAME = "com.google.android.gms.location.Geofence";

public static final String SHARED_PREFERENCES_NAME = PACKAGE_NAME + ".SHARED_PREFERENCES_NAME";

public static final String GEOFENCES_ADDED_KEY = PACKAGE_NAME + ".GEOFENCES_ADDED_KEY";

/**
 * Used to set an expiration time for a geofence. After this amount of time Location Services
 * stops tracking the geofence.
 */
public static final long GEOFENCE_EXPIRATION_IN_HOURS = 12;
public static final String GEOFENCE_ID_STAN_UNI = "home";

/**
 * For this sample, geofences expire after twelve hours.
 */
public static final long GEOFENCE_EXPIRATION_IN_MILLISECONDS =
        GEOFENCE_EXPIRATION_IN_HOURS * 60 * 60 * 1000;
//public static final float GEOFENCE_RADIUS_IN_METERS = 1609; // 1 mile, 1.6 km
public static final float GEOFENCE_RADIUS_IN_METERS = 20; // 1 mile, 1.6 km

/**
 * Map for storing information about airports in the San Francisco bay area.
 */

public double getLatitude() {
    return latitude;
}

public void setLatitude(double latitude) {
    this.latitude = latitude;
}

public double getLongitude() {
    return longitude;
}

public void setLongitude(double longitude) {
    this.longitude = longitude;
}

public double getll(){
    return ll;
}



public String id;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String name;
public double latitude;
public double longitude;

public double ll;публичный радиус плавания;

public ConstantData(String id, String name, double latitude, double longitude, float radius) {
    this.id = id;
    this.name = name;
    this.latitude = latitude;
    this.longitude = longitude;
    this.radius = radius;
}

public static final HashMap<String, ConstantData> BAY_AREA_LANDMARKS = new HashMap<String, ConstantData>();
static {
    BAY_AREA_LANDMARKS.put("home",new ConstantData("1","abcd",12.896926, 77.622730,40));

    BAY_AREA_LANDMARKS.put("block3",new ConstantData("7","abd",12.931893, 77.606396,20));

    BAY_AREA_LANDMARKS.put("audienter",new ConstantData("1","abcd",12.936133, 77.606215,10));
    BAY_AREA_LANDMARKS.put("puc",new ConstantData("2","abd",12.935561, 77.606208,10));
    BAY_AREA_LANDMARKS.put("park",new ConstantData("3","abd",12.934890, 77.606192,10));
    BAY_AREA_LANDMARKS.put("central",new ConstantData("4","abd",12.934529, 77.606197,20));
    BAY_AREA_LANDMARKS.put("block1",new ConstantData("5","abd",12.933932, 77.606426,20));
    BAY_AREA_LANDMARKS.put("block2",new ConstantData("6","abd",12.933357, 77.606238,30));

    BAY_AREA_LANDMARKS.put("block4",new ConstantData("8","abd",12.932131, 77.606000,10));
    BAY_AREA_LANDMARKS.put("block2nd",new ConstantData("9","abd",12.932797, 77.606291,20));



}

GeofenceTransitionsIntentService

открытый класс GeofenceTransitionsIntentService extends IntentService {protected static final String TAG = "geofence-transitions-service";

public GeofenceTransitionsIntentService() {
    // Use the TAG to name the worker thread.
    super(TAG);
}

@Override
public void onCreate()
{
    super.onCreate();
}

@Override
protected void onHandleIntent(Intent intent) {
    GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
    if (geofencingEvent.hasError()) {
        String errorMessage = GeofenceErrorMessages.getErrorString(this,
                geofencingEvent.getErrorCode());
        Log.e(TAG, errorMessage);

        int transaction = geofencingEvent.getGeofenceTransition();
        List<Geofence> geofences = geofencingEvent.getTriggeringGeofences();
        Geofence geofence = geofences.get(0);
        if (transaction == Geofence.GEOFENCE_TRANSITION_ENTER && geofence.getRequestId().equals(ConstantData.BAY_AREA_LANDMARKS.get("home"))) {
            Log.d(TAG, "You are inside Stanford University");
            Toast.makeText(getApplicationContext(),"inside",Toast.LENGTH_LONG).show();
        } else {
            Log.d(TAG, "You are outside Stanford University");
        }
        return;
    }

    // Get the transition type.
    int geofenceTransition = geofencingEvent.getGeofenceTransition();

    // Test that the reported transition was of interest.
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
            geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

        // Get the geofences that were triggered. A single event can trigger multiple geofences.
        List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

        // Get the transition details as a String.
        String geofenceTransitionDetails = getGeofenceTransitionDetails(
                this,
                geofenceTransition,
                triggeringGeofences
        );

        // Send notification and log the transition details.
        //   sendNotification(geofenceTransitionDetails);
        Log.i(TAG, geofenceTransitionDetails);
    } else {
        // Log the error.
        Log.e(TAG, getString(R.string.geofence_transition_invalid_type, geofenceTransition));
    }
}

private String getGeofenceTransitionDetails(
        Context context,
        int geofenceTransition,
        List<Geofence> triggeringGeofences) {

    String geofenceTransitionString = getTransitionString(geofenceTransition);

    // Get the Ids of each geofence that was triggered.
    ArrayList<String> triggeringGeofencesIdsList = new ArrayList<>();
    for (Geofence geofence : triggeringGeofences) {
        triggeringGeofencesIdsList.add(geofence.getRequestId());
    }
    String triggeringGeofencesIdsString = TextUtils.join(", ", triggeringGeofencesIdsList);

    return geofenceTransitionString + ": " + triggeringGeofencesIdsString;
}

private String getTransitionString(int transitionType) {
    switch (transitionType) {
        case Geofence.GEOFENCE_TRANSITION_ENTER:
            return getString(R.string.geofence_transition_entered);
        case Geofence.GEOFENCE_TRANSITION_EXIT:
            return  getString(R.string.geofence_transition_exited);
        default:
            return getString(R.string.unknown_geofence_transition);
    }
}

}

...