Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

How to load dynamically Geofences? #10

Closed
geolyth opened this issue Apr 17, 2015 · 2 comments
Closed

How to load dynamically Geofences? #10

geolyth opened this issue Apr 17, 2015 · 2 comments

Comments

@geolyth
Copy link

geolyth commented Apr 17, 2015

Hi,
I have a problem...in this sample you use geofence from Constant...unfortunately i'm not able to load geofences dynamically :-(

Maybe I wrong something...

In onCreate method:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    // Empty list for storing geofences.
    mGeofenceList = new ArrayList<Geofence>();

    // Initially set the PendingIntent used in addGeofences() and removeGeofences() to null.
    mGeofencePendingIntent = null;

    geofencePointsRequest(this, null, null);
}

Volley request to retrieve data

public void geofencePointsRequest(final Context context, Map<String,String> params, final ProgressDialog pDialog){
webService = new WebService();

CustomArrayRequest jsonObjReq = new CustomArrayRequest(
        Request.Method.GET,
        WebServiceUrl.POINTS,
        params,
        new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                ArrayList<Point> points = webService.getPoints(response);
                populateGeofenceList(points);
            }
        },

        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                String errorHandler = webService.getError(error);
                int status = (error.networkResponse.statusCode != 0) ? error.networkResponse.statusCode : 0;
                Toast.makeText(context, "Code : " + status + " - Error : " + errorHandler, Toast.LENGTH_LONG).show();
            }
        }) {

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return webService.getHeaders();
    }
};

VolleySingleton.getInstance(context).addToRequestQueue("pointsRequest", jsonObjReq);
}

Build GoogleApiClient

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    mGoogleApiClient.connect();
}

@Override
protected void onStart() {
    super.onStart();
    if (mGoogleApiClient != null) mGoogleApiClient.connect();
}

On connected I load geofences

@Override
public void onConnected(Bundle connectionHint) {
    Log.i(TAG, "Connected to GoogleApiClient");
    addGeofencesOnLoad();
}

private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
    builder.addGeofences(mGeofenceList);
    return builder.build();
}

public void addGeofencesOnLoad() {
    if (!mGoogleApiClient.isConnected()) {
        Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
        return;
    }

    try {
        LocationServices.GeofencingApi.addGeofences(
                mGoogleApiClient,
                getGeofencingRequest(),
                getGeofencePendingIntent()
        ).setResultCallback(this); // Result processed in onResult().
    } catch (SecurityException securityException) {
        logSecurityException(securityException);
    }
}

I create a list of geofences (from data parsed)

public void populateGeofenceList(ArrayList<Point> points) {
    for (int i=0; i<points.size(); i++) {
        mGeofenceList.add(new Geofence.Builder()
                .setRequestId(pins.get(i).getShortId())
                .setCircularRegion(
                        points.get(i).getLocation().getCoordinates().latitude,
                        points.get(i).getLocation().getCoordinates().longitude,
                        points.get(i).getLocation().getRadius()
                )
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
                .build());
    }

    buildGoogleApiClient();
}

Thanks in advance :-)

@geolyth
Copy link
Author

geolyth commented Apr 22, 2015

The problem was geofences id...everything works fine :)

@geolyth geolyth closed this as completed Apr 22, 2015
@ChandoraAnkit
Copy link

Can you be specific? I'm having the same problem

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants