Как работать с Geo Fence на андроиде?

Я пытаюсь реализовать Geo Fence в приложении для Android. я следовал этому руководству http://developer.android.com/training/location/geofencing.html . и

введите код сюда

   mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
    googleMap = mapFragment.getMap();
    // set OnMarkerDrag Listener
    googleMap.setOnMarkerDragListener(this);
    // radius distance for geofencing boundary
    distance = 1;
    // move camera at specific location.
    // current location latitude and longitude can be provided here
    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Mylatitude, Mylongitude), 1));

    // createGeofence location latitude and longitude and shape
    createGeofence(latitudeGeofence, longitudeGeofence, distance, "CIRCLE", "MyOffice");
}
private void createGeofence(double latitude, double longitude, int radius, String geofenceType, String title){
    Marker marker = googleMap.addMarker(new MarkerOptions().draggable(true).position(new LatLng(latitude, longitude)).title(title).draggable(true).icon(BitmapDescriptorFactory.fromResource(R.drawable.lock)));
   //marker.setAnchor(10,10);

    googleMap.addCircle(new CircleOptions().center(new LatLng(latitude, longitude)).radius(radius).strokeColor(Color.parseColor("#ffff00")).fillColor(Color.parseColor("#B2A9F6")));
}

public void onMarkerDrag(Marker marker){}

public void onMarkerDragEnd(Marker marker){
    LatLng dragPosition = marker.getPosition();
    double dragLat = dragPosition.latitude;
    double dragLong = dragPosition.longitude;

    googleMap.clear();
    createGeofence(dragLat, dragLong, distance, "CIRCLE", "GEOFENCE");
}
public void onMarkerDragStart(Marker marker){}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_fencing, menu);
    return true;
}

но он не работает, он не получил никаких уведомлений о входе и выходе в приложении для Android. пожалуйста, помогите мне, как разработать гео-фехтование и как получать уведомления в приложении. Спасибо заранее.


person Pushpa Latha    schedule 06.02.2016    source источник
comment
Код, который вы указали, рисует только круг на карте Google. Это не создание геозоны.   -  person Pablo Baxter    schedule 07.02.2016
comment
Спасибо за ваш ответ. Пожалуйста, помогите мне, как сделать геозону в приложении. Заранее спасибо.   -  person Pushpa Latha    schedule 08.02.2016


Ответы (1)


public class SimpleGeofence {

    private final String mId;
    private final double mLatitude;
    private final double mLongitude;
    private final float mRadius;
    private long mExpirationDuration;
    private int mTransitionType;

    public SimpleGeofence(String geofenceId, double latitude, double longitude, float radius,long expiration, int transition) {

        this.mId = geofenceId;
        this.mLatitude = latitude;
        this.mLongitude = longitude;
        this.mRadius = radius;
        this.mExpirationDuration = expiration;
        this.mTransitionType = transition;
    }

    public String getId() {
        return mId;
    }
    public double getLatitude() {
        return mLatitude;
    }
    public double getLongitude() {
        return mLongitude;
    }
    public float getRadius() {
        return mRadius;
    }
    public long getExpirationDuration() {
        return mExpirationDuration;
    }
    public int getTransitionType() {
        return mTransitionType;
    }


    public Geofence toGeofence() {
        return new Geofence.Builder()
            .setRequestId(mId)
            .setTransitionTypes(mTransitionType)
            .setCircularRegion(mLatitude, mLongitude, mRadius)
            .setExpirationDuration(mExpirationDuration)
            .build();
    }
}

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

Попробуйте этот, он может быть вам полезен.

person Mahesh Keshvala    schedule 22.10.2016