2016-02-06 3 views
0

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

введите код здесь

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

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

+0

Код, который вы указали, только рисует круг на карте google. Это не создает геозонность. –

+0

спасибо за ваш ответ.пожалуйста, помогите мне, как сделать geo забор в application.thanks заранее. –

ответ

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

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

Смежные вопросы