2016-12-01 45 views
0

注意的給定距離內:我不需要連續監視用戶的位置,只需執行檢查一次關上的按鈕,點擊查找用戶是否是給COORDS

我有一組緯度和經度,並且需要檢查用戶是否在該點的給定距離內,我該怎麼做?我檢查了Geofencing,但它像一個服務,而我需要在主線程中執行一次檢查

+0

你既可以座標之間計算距離和工作,因此我不使用的地圖其實,只是一個靜態檢查 –

回答

0

這是用來計算兩點之間的距離的方法:

public double CalculationByDistance(LatLng StartP, LatLng EndP) { 
     int Radius = 6371;// radius of earth in Km 
     double lat1 = StartP.latitude; 
     double lat2 = EndP.latitude; 
     double lon1 = StartP.longitude; 
     double lon2 = EndP.longitude; 
     double dLat = Math.toRadians(lat2 - lat1); 
     double dLon = Math.toRadians(lon2 - lon1); 
     double a = Math.sin(dLat/2) * Math.sin(dLat/2) 
       + Math.cos(Math.toRadians(lat1)) 
       * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon/2) 
       * Math.sin(dLon/2); 
     double c = 2 * Math.asin(Math.sqrt(a)); 
     double valueResult = Radius * c; 
     double km = valueResult/1; 
     DecimalFormat newFormat = new DecimalFormat("####"); 
     int kmInDec = Integer.valueOf(newFormat.format(km)); 
     double meter = valueResult % 1000; 
     int meterInDec = Integer.valueOf(newFormat.format(meter)); 
     Log.i("Radius Value", "" + valueResult + " KM " + kmInDec 
       + " Meter " + meterInDec); 

     return Radius * c; 
    } 

這是此問題的副本:Find distance between two points on map using Google Map API V2

請嘗試查看您的問題在未來詢問之前是否已得到解答。

+0

。 。也許這就是爲什麼我沒有遇到這個問題 –

+0

沒問題,這仍然會給你你需要的功能。只需調用此方法並傳遞您的Lat/Lng值,然後根據您需要的任何距離檢查從此方法返回的值,並相應地繼續。 – JamesSwinton

+0

是的,會做,謝謝! –

1

只需檢查它。

因與下面的代碼經緯度設定邊界的位置:

public LatLngBounds getLocationBounds(LatLng sourceLocation, double distance) 
{ 
    LatLng southwest = SphericalUtil.computeOffset(sourceLocation, distance, 225); 
    LatLng northeast = SphericalUtil.computeOffset(sourceLocation, distance, 45); 
    return new LatLngBounds(southwest, northeast); 
}