餐廳的位置將作爲經度和緯度的組合存儲在您的數據庫中。
當客戶打開應用程序時,必須獲取客戶的當前位置並查詢數據庫,以檢查用戶/客戶的當前位置與餐館位置之間的距離是否小於5英里。 Google已經提供了一種實現此功能的方法。
假設你所代表的用戶的位置爲:
Location userLocation = new Location("");
userLocation.setLatitude(latitude1);
userLocation.setLongitude(longitude1);
這是你如何代表餐廳的位置,從數據庫中逐個查詢。
Location restaurantLocation = new Location("");
restaurantLocation.setLatitude(latitude2);
restaurantLocation.setLongitude(longitude2);
這樣就可以計算以米爲單位的距離。
float distanceInMeters = userLocation.distanceTo(restaurantLocation);
你可以做適當的轉換爲英里如下:
float distanceInMiles = distanceInMeters/1.60934
然後檢查它是否是超過5英里。如果條件返回true,則用餐廳的座標在地圖上標記該位置。
if (distanceInMiles > 5)
{
// code to set the marker with the restaurant's coordinates on the map.
}
可以(每5分鐘後說)定期執行此測試,發現在用戶附近方圓5英里的餐館。