जियोस्पेशल क्वेरी का इस्तेमाल करना

जगह की जानकारी देने वाली सेवाएं बनाने के लिए, 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 */)
  });