您可以在 Cloud Firestore 中執行地理空間查詢,建構可感知位置的服務。舉例來說,您可以找出使用者與附近景點之間的距離,並從最近到最遠排序。
版本需求
地理空間搜尋功能需要 Firestore Enterprise 版資料庫。
事前準備
如要執行地理空間搜尋,您必須先為要搜尋的欄位建立地理空間索引。
執行地理空間搜尋
如要執行地理空間搜尋,請在 search(...) 階段的 query 參數中使用 geoDistance 運算式。
僅支援小於或等於 (<=) 運算子。距離以公尺為單位。
舉例來說,下列查詢會找出所列地理點 1,000 公尺內的所有餐廳。
Web
firestore.pipeline().collection('restaurants') .search({ query: field('location') .geoDistance(new GeoPoint(38.989177, -107.065076)) .lessThan(1000 /* m */) });
iOS
firestore.pipeline().collection("restaurants") .search( query: Field("location") .geoDistance(GeoPoint(latitude: 38.989177, longitude: -107.065076)) .lessThan(1000) )
Android
firestore.pipeline() .collection("restaurants") .search(new SearchOptions() .withQuery(field("location") .geoDistance(new GeoPoint(38.989177, -107.065076)) .lessThan(1000 /* meters */)));
Node.js
firestore.pipeline().collection('restaurants') .search({ query: field('location') .geoDistance(new GeoPoint(38.989177, -107.065076)) .lessThan(1000 /* m */) });