क्वाडट्री

इस पेज पर, क्वाडट्री की सुविधा के बारे में जानकारी दी गई है. यह सुविधा iOS के लिए Maps SDK टूल में उपलब्ध यूटिलिटी लाइब्रेरी में उपलब्ध है.

क्वाडट्री एक डेटा स्ट्रक्चर है. इसकी मदद से, किसी खास जगह के आस-पास के पॉइंट ढूंढे जा सकते हैं. इसके लिए, लोकप्रिय जगह के आस-पास के इलाके में खोज की जाती है.

क्वाडट्री का इस्तेमाल करके, 2D रेंज में पॉइंट को आसानी से खोजा जा सकता है. इनमें उन पॉइंट को अक्षांश/देशांतर निर्देशांक या कार्टेशियन (x, y) निर्देशांक के तौर पर दिखाया जाता है. क्वाडट्री, निर्देशांक की बकेट को नोड में स्टोर करती है और उन्हें क्षेत्र (बाउंडिंग बॉक्स) के हिसाब से इंडेक्स करती है. किसी निर्देशांक पेयर को ढूंढने के लिए, आपको क्वाडट्री के नोड को पार करना होगा.

ज़रूरी शर्तें और ज़रूरी जानकारी

क्वाडट्री यूटिलिटी, iOS के लिए Maps SDK यूटिलिटी लाइब्रेरी का हिस्सा है. अगर आपने अभी तक लाइब्रेरी सेट अप नहीं की है, तो इस पेज के बाकी हिस्से को पढ़ने से पहले सेटअप गाइड का पालन करें.

क्वाडट्री जोड़कर, दिए गए इलाके में पॉइंट खोजें

यह कोड एक क्वाडट्री बनाता है, फिर दिए गए एरिया में सभी पॉइंट खोजता है:

Swift

import GoogleMapsUtils

class QuadTreeItem : NSObject, GQTPointQuadTreeItem {
  private let gqtPoint : GQTPoint

  init(point : GQTPoint) {
    self.gqtPoint = point
  }

  func point() -> GQTPoint {
    return gqtPoint
  }

  /// Function demonstrating how to create and use a quadtree
  private func test() {

    // Create a quadtree with bounds of [-2, -2] to [2, 2].
    let bounds = GQTBounds(minX: -2, minY: -2, maxX: 2, maxY: 2)
    guard let tree = GQTPointQuadTree(bounds: bounds) else {
      return
    }

    // Add 4 points to the tree.
    tree.add(QuadTreeItem(point: GQTPoint(x: -1, y: -1)))
    tree.add(QuadTreeItem(point: GQTPoint(x: -1, y: -1)))
    tree.add(QuadTreeItem(point: GQTPoint(x: -1, y: 1)))
    tree.add(QuadTreeItem(point: GQTPoint(x: 1, y: 1)))
    tree.add(QuadTreeItem(point: GQTPoint(x: 1, y: -1)))

    // Search for items within the rectangle with lower corner of (-1.5, -1.5)
    // and upper corner of (1.5, 1.5).
    let searchBounds = GQTBounds(minX: -1.5, minY: -1.5, maxX: 1.5, maxY: 1.5)
    for item in tree.search(with: searchBounds) as! [QuadTreeItem] {
      print("(\(item.point().x), \(item.point().y))");
    }
  }
}
      

Objective-C

@import GoogleMapsUtils;

@interface QuadTreeItem : NSObject<GQTPointQuadTreeItem>
- (instancetype)initWithPoint:(GQTPoint)point;
@end

@implementation QuadTreeItem {
  GQTPoint _point;
}

- (instancetype)initWithPoint:(GQTPoint)point {
  if ((self = [super init])) {
    _point = point;
  }
  return self;
}

- (GQTPoint)point {
  return _point;
}

/// Function demonstrating how to create and use a quadtree
- (void)test {
  // Create a quadtree with bounds of [-2, -2] to [2, 2].
  GQTBounds bounds = {-2, -2, 2, 2};
  GQTPointQuadTree *tree = [[GQTPointQuadTree alloc] initWithBounds:bounds];

  // Add 4 points to the tree.
  [tree add:[[QuadTreeItem alloc] initWithPoint:(GQTPoint){-1, -1}]];
  [tree add:[[QuadTreeItem alloc] initWithPoint:(GQTPoint){-1, 1}]];
  [tree add:[[QuadTreeItem alloc] initWithPoint:(GQTPoint){1, 1}]];
  [tree add:[[QuadTreeItem alloc] initWithPoint:(GQTPoint){1, -1}]];

  // Search for items within the rectangle with lower corner of (-1.5, -1.5)
  // and upper corner of (1.5, 1.5).
  NSArray *foundItems = [tree searchWithBounds:(GQTBounds){-1.5, -1.5, 1.5, 1.5}];

  for (QuadTreeItem *item in foundItems) {
    NSLog(@"(%lf, %lf)", item.point.x, item.point.y);
  }
}

@end