2015-03-25 84 views
-1

如果我在半徑之外時已經解決了,是否有可能發出通知?超出半徑範圍時發出通知谷歌地圖android

我的模式是這樣的: 我是學生,我的手機已安裝跟蹤器。如果我走進校外的範圍,我的申請會通知我的母親,我已經使用短信/電子郵件跳過課程。

你能指導我嗎?

這裏是我的代碼:

protected void onCreate(Bundle savedInstanceState) { 
     mLocationRequest = LocationRequest.create() 
       .setInterval(3) 
       .setFastestInterval(60) 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_student_activity); 
} 

public void circle() 
{ 
    Circle circle = mMap.addCircle(new CircleOptions() 
      .center(new LatLng(-xxxxxx,xxxxx)) 
      .radius(2) 
      .strokeColor(Color.RED) 
      .strokeWidth(1) 
      .fillColor(Color.BLUE)); 
} 

public void setUpMapIfNeeded() 
{ 
    SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
    mMap = mapFrag.getMap(); 
    mMap.setMyLocationEnabled(true); 
    circle(); 
} 

public void onConnected(Bundle bundle) 
{ 
    Log.i(TAG,"Location Services Connected."); 
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    if(location==null) 
    { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this); 
    } 
    else 
    { 
     handleNewLocation(location); 
    } 
} 

private void handleNewLocation(Location location) 
{ 
    Log.d(TAG,location.toString()); 
    double currentLatitude = location.getLatitude(); 
    double currentLongitude = location.getLongitude(); 
    LatLng latLng = new LatLng(currentLatitude,currentLongitude); 
    MarkerOptions options = new MarkerOptions() 
      .position(latLng) 
      .title("I am here !"); 
    mMap.addMarker(options); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,21)); 
} 

public void onLocationChanged(Location location) { 
    handleNewLocation(location); 
} 
+0

看無論是地理範圍API或接近警告API – 2015-03-25 09:35:40

+0

任何鏈接或關鍵字? – 2015-03-25 09:40:52

回答

0

//把這個代碼在處理新的位置

//put this code in handler new location 
if(distanceBetween(current_latlon, circle_latlon)>radius_of_circle)//radius in meters 
{ 
    //here you can create a notification of whatever you want 
} 

//to calculate distance between two latlong 
private float distanceBetween(LatLng latLng1, LatLng latLng2) 
{ 
    Location loc1 = new Location(LocationManager.GPS_PROVIDER); 
     Location loc2 = new Location(LocationManager.GPS_PROVIDER); 
     loc1.setLatitude(latLng1.latitude); 
     loc1.setLongitude(latLng1.longitude); 
     loc2.setLatitude(latLng2.latitude); 
     loc2.setLongitude(latLng2.longitude); 
     return loc1.distanceTo(loc2);  //in meters 
    } 
+0

它來自哪裏:current_latlon和circle_latlon和radius_of_circle? 我還是新手。 Tehee – 2015-03-26 04:11:52

+0

1.在你的public void circle()函數中有一個像新的latlon一樣的latlon(-xxxxx,xxxx)是你的circle_latlon,2.current_latlon是public void onLocationChanged(Location location)函數中的位置變量3。圓的半徑是你在public void circle()方法中提到的任何東西。 – Rahul 2015-03-27 04:56:51

+0

我已創建: Latlng latlngcircle = new LatLng(xxxxxx,xxxxx); 如果把在聲明中這樣的: 如果(distanceBetween(的latLng,latlngcircle)> 5 { sendsms(); } ,當我在我的手機調試它成爲一次又一次的重新啓動程序 什麼。解決方案? – 2015-03-27 07:50:33