如何在android中使用Geo Fence?

时间:2016-02-06 10:36:50

标签: android geofencing android-geofence

我正在尝试在Android应用程序中实现Geo Fence。我按照本教程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应用程序中获得任何关于委托和退出的通知。请帮助我如何开发地理围栏以及如何在应用程序中获取通知。 提前尝试。

1 个答案:

答案 0 :(得分:0)

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();
}

试试这个,这可能对你有所帮助。