การคลัสเตอร์เครื่องหมาย

เลือกแพลตฟอร์ม: Android iOS JavaScript

หน้านี้อธิบายยูทิลิตีการจัดกลุ่มเครื่องหมายที่มีอยู่ในไลบรารียูทิลิตีสำหรับ Maps SDK สำหรับ iOS

การจัดกลุ่มเครื่องหมายช่วยให้คุณวางเครื่องหมายจำนวนมากบนแผนที่ได้ โดยไม่ทำให้แผนที่อ่านยาก ยูทิลิตีการจัดกลุ่มเครื่องหมายจะช่วยคุณจัดการตัวทำเครื่องหมายหลายรายการที่ระดับการซูมต่างๆ

เมื่อผู้ใช้ดูแผนที่ที่ระดับการซูมสูง เครื่องหมายต่างๆ จะแสดงบนแผนที่ เมื่อผู้ใช้ซูมออก เครื่องหมายจะรวมกันเป็นกลุ่มเพื่อให้ดูแผนที่ได้ง่ายขึ้น

ภาพหน้าจอต่อไปนี้แสดงรูปแบบเริ่มต้นของคลัสเตอร์เครื่องหมาย

แผนที่ที่มีเครื่องหมายกระจุกรวมกันในรูปแบบเริ่มต้น

ตัวอย่างคลัสเตอร์ตัวทำเครื่องหมายที่กำหนดเองมีดังนี้

แผนที่ซึ่งมีเครื่องหมายจัดเป็นกลุ่มที่กำหนดเอง

ข้อกำหนดเบื้องต้นและหมายเหตุ

ไลบรารี Maps SDK สำหรับ iOS Utility

ยูทิลิตีการจัดกลุ่มเครื่องหมายเป็นส่วนหนึ่งของ Maps SDK สำหรับไลบรารียูทิลิตีของ iOS หากยังไม่ได้ตั้งค่าไลบรารี ให้ทำตามคู่มือการตั้งค่าก่อนอ่านส่วนที่เหลือของหน้านี้

จำนวนเครื่องหมายสูงสุดที่แนะนำคือ 10,000 ตัวเพื่อประสิทธิภาพที่ดีที่สุด

สิทธิ์เข้าถึงตำแหน่ง

ตัวอย่างนี้ใช้ GPS ของอุปกรณ์เพื่อค้นหาผู้ใช้และแผนที่บนพิกัด หากต้องการเปิดใช้ คุณจะต้องเพิ่มคำอธิบายไปยังสิทธิ์ NSLocationWhenInUseUsageDescription ในไฟล์ Info.plist ของโปรเจ็กต์

หากต้องการเพิ่ม ให้ทำดังนี้

  1. คลิกไฟล์ Info.plist ในตัวนำทางโปรเจ็กต์ใน Xcode เพื่อเปิดเครื่องมือแก้ไขรายการพร็อพเพอร์ตี้
  2. คลิกไอคอน "+" ถัดจาก "รายการพร็อพเพอร์ตี้ข้อมูล" เพื่อเพิ่มพร็อพเพอร์ตี้ใหม่
  3. ในช่อง "คีย์" ให้พิมพ์ "NSLocationWhenInUseUsageDescription" Xcode จะแปลค่านี้เป็นชื่อเต็มว่า "Privacy - Location When In Use Usage Description" ดูรายการพร็อพเพอร์ตี้สิทธิ์เข้าถึงตำแหน่งที่เป็นไปได้ทั้งหมดได้ที่ การขอสิทธิ์ใช้บริการตำแหน่งในเอกสารประกอบสำหรับนักพัฒนาแอป Apple
  4. ตั้งค่าช่อง "Type" เป็น "String"
  5. ในช่อง "ค่า" ให้พิมพ์คำอธิบายเหตุผลที่แอปจำเป็นต้องใช้ตำแหน่งของผู้ใช้ ตัวอย่างเช่น "ค้นหาผู้ใช้เพื่อแสดงข้อมูลธุรกิจในบริเวณใกล้เคียง"

การใช้การจัดกลุ่มเครื่องหมาย

การคลัสเตอร์เครื่องหมายมี 3 ขั้นตอนดังนี้

  1. สร้างอินสแตนซ์ตัวจัดการคลัสเตอร์
  2. ส่งต่อเครื่องหมายที่ต้องการคลัสเตอร์ไปยังเครื่องมือจัดการคลัสเตอร์
  3. เรียกใช้ตัวจัดการคลัสเตอร์
ดูตัวอย่างทั้งหมดเกี่ยวกับวิธีใช้คลัสเตอร์เครื่องหมายได้จากตัวอย่างแอป Objective-C และ Swift ใน GitHub

กำลังสร้างตัวจัดการคลัสเตอร์

หากต้องการใช้ตัวจัดการคลัสเตอร์ ให้ทำดังนี้

  1. ตั้งค่า ViewController ที่มีการแสดงผลแผนที่เพื่อให้สอดคล้องกับโปรโตคอล GMSMapViewDelegate
  2. สร้างอินสแตนซ์ของ GMUClusterManager
  3. ส่งอินสแตนซ์ของ GMSMapView ที่ต้องการใช้คลัสเตอร์ตัวทำเครื่องหมาย และการใช้งานโปรโตคอลต่อไปนี้ไปยังอินสแตนซ์ GMUClusterManager
    • GMUClusterIconGenerator: ระบุตรรกะของแอปพลิเคชันที่ดึงไอคอนคลัสเตอร์เพื่อใช้ในการซูมระดับต่างๆ
    • GMUClusterAlgorithm: ระบุอัลกอริทึมที่กำหนดลักษณะการทำงานของการจัดกลุ่มตัวทำเครื่องหมาย เช่น ระยะห่างระหว่างตัวทำเครื่องหมายที่จะรวมในคลัสเตอร์เดียวกัน
    • GMUClusterRenderer: ระบุตรรกะของแอปพลิเคชันที่จัดการการแสดงผลจริงของไอคอนคลัสเตอร์บนแผนที่
  4. ตั้งค่าผู้รับมอบสิทธิ์แผนที่บนอินสแตนซ์ GMUClusterManager

ไลบรารียูทิลิตีนี้มีการใช้งานเริ่มต้นของโปรแกรมสร้างไอคอน (GMUDefaultClusterIconGenerator), อัลกอริทึม (GMUNonHierarchicalDistanceBasedAlgorithm) และตัวแสดงผล (GMUDefaultClusterRenderer) และคุณเลือกที่จะสร้างเครื่องมือสร้างไอคอน อัลกอริทึม และโปรแกรมแสดงผลที่กำหนดเองได้

โค้ดต่อไปนี้จะสร้างตัวจัดการคลัสเตอร์โดยใช้ค่าเริ่มต้นเหล่านี้ในการเรียกกลับ viewDidLoad ของ ViewController

Swift

import GoogleMaps
import GoogleMapsUtils

class MarkerClustering: UIViewController, GMSMapViewDelegate {
  private var mapView: GMSMapView!
  private var clusterManager: GMUClusterManager!

  override func viewDidLoad() {
    super.viewDidLoad()

    // Set up the cluster manager with the supplied icon generator and
    // renderer.
    let iconGenerator = GMUDefaultClusterIconGenerator()
    let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
    let renderer = GMUDefaultClusterRenderer(mapView: mapView,
                                clusterIconGenerator: iconGenerator)
    clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm,
                                                      renderer: renderer)

    // Register self to listen to GMSMapViewDelegate events.
    clusterManager.setMapDelegate(self)
    // ...
  }
  // ...
}
      

Objective-C

@import GoogleMaps;
@import GoogleMapsUtils;

@interface MarkerClustering () <GMSMapViewDelegate>

@end

@implementation MarkerClustering {
  GMSMapView *_mapView;
  GMUClusterManager *_clusterManager;
}

- (void)viewDidLoad {
  [super viewDidLoad];

  // Set up the cluster manager with a supplied icon generator and renderer.
  id<GMUClusterAlgorithm> algorithm =
      [[GMUNonHierarchicalDistanceBasedAlgorithm alloc] init];
  id<GMUClusterIconGenerator> iconGenerator =
      [[GMUDefaultClusterIconGenerator alloc] init];
  id<GMUClusterRenderer> renderer =
      [[GMUDefaultClusterRenderer alloc] initWithMapView:_mapView
                                    clusterIconGenerator:iconGenerator];
  _clusterManager =
      [[GMUClusterManager alloc] initWithMap:_mapView
                                   algorithm:algorithm
                                    renderer:renderer];

  // Register self to listen to GMSMapViewDelegate events.
  [_clusterManager setMapDelegate:self];
  // ...
}
// ...
@end
      

การเพิ่มเครื่องหมาย

การเพิ่มตัวทำเครื่องหมายลงในคลัสเตอร์เครื่องหมายมี 2 วิธี ได้แก่ แบบทีละรายการหรือเป็นอาร์เรย์

เครื่องหมายแยก

Swift

let position = CLLocationCoordinate2D(latitude: 47.60, longitude: -122.33)
let marker = GMSMarker(position: position)
clusterManager.add(marker)
      

Objective-C

CLLocationCoordinate2D position = CLLocationCoordinate2DMake(47.60, -122.33);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
[_clusterManager addItem:marker];
      

อาร์เรย์ของเครื่องหมาย

Swift

let position1 = CLLocationCoordinate2D(latitude: 47.60, longitude: -122.33)
let marker1 = GMSMarker(position: position1)

let position2 = CLLocationCoordinate2D(latitude: 47.60, longitude: -122.46)
let marker2 = GMSMarker(position: position2)

let position3 = CLLocationCoordinate2D(latitude: 47.30, longitude: -122.46)
let marker3 = GMSMarker(position: position3)

let position4 = CLLocationCoordinate2D(latitude: 47.20, longitude: -122.23)
let marker4 = GMSMarker(position: position4)

let markerArray = [marker1, marker2, marker3, marker4]
clusterManager.add(markerArray)
      

Objective-C

CLLocationCoordinate2D position1 = CLLocationCoordinate2DMake(47.60, -122.33);
GMSMarker *marker1 = [GMSMarker markerWithPosition:position1];

CLLocationCoordinate2D position2 = CLLocationCoordinate2DMake(47.60, -122.46);
GMSMarker *marker2 = [GMSMarker markerWithPosition:position2];

CLLocationCoordinate2D position3 = CLLocationCoordinate2DMake(47.30, -122.46);
GMSMarker *marker3 = [GMSMarker markerWithPosition:position3];

CLLocationCoordinate2D position4 = CLLocationCoordinate2DMake(47.20, -122.23);
GMSMarker *marker4 = [GMSMarker markerWithPosition:position4];

NSArray<GMSMarker *> *markerArray = @[marker1, marker2, marker3, marker4];
[_clusterManager addItems:markerArray];
      

การเรียกใช้การคลัสเตอร์เครื่องหมาย

เมื่อสร้างคลัสเตอร์เครื่องหมายและส่งผ่านเครื่องหมายที่ต้องการจัดคลัสเตอร์แล้ว สิ่งที่คุณต้องทำคือเรียกใช้เมธอด cluster ในอินสแตนซ์คลัสเตอร์เครื่องหมาย

Swift

clusterManager.cluster()
      

Objective-C

[_clusterManager cluster];
      

จัดการเหตุการณ์บนตัวทำเครื่องหมายและคลัสเตอร์

โดยทั่วไปเมื่อใช้ Maps SDK สำหรับ iOS คุณต้องใช้โปรโตคอล GMSMapViewDelegate เพื่อฟังเหตุการณ์ในแผนที่ คุณฟังเหตุการณ์ที่แมปได้ แต่ฟังเหตุการณ์ตัวจัดการคลัสเตอร์ที่ปลอดภัยประเภทไม่ได้ เมื่อผู้ใช้แตะตัวทำเครื่องหมาย รายการคลัสเตอร์แต่ละรายการ หรือคลัสเตอร์ API จะทริกเกอร์ mapView:didTapMarker: และแนบข้อมูลคลัสเตอร์เพิ่มเติมกับพร็อพเพอร์ตี้ marker.userData จากนั้นคุณจะตรวจสอบได้ว่า userData สอดคล้องกับโปรโตคอล GMUCluster หรือไม่ เพื่อดูว่ามีการแตะไอคอนคลัสเตอร์หรือเครื่องหมายหรือไม่

Swift

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
  // center the map on tapped marker
  mapView.animate(toLocation: marker.position)
  // check if a cluster icon was tapped
  if marker.userData is GMUCluster {
    // zoom in on tapped cluster
    mapView.animate(toZoom: mapView.camera.zoom + 1)
    NSLog("Did tap cluster")
    return true
  }

  NSLog("Did tap a normal marker")
  return false
}
      

Objective-C

- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
  // center the map on tapped marker
    [_mapView animateToLocation:marker.position];
    // check if a cluster icon was tapped
    if ([marker.userData conformsToProtocol:@protocol(GMUCluster)]) {
      // zoom in on tapped cluster
      [_mapView animateToZoom:_mapView.camera.zoom + 1];
      NSLog(@"Did tap cluster");
      return YES;
    }

    NSLog(@"Did tap marker in cluster");
    return NO;
}
      

ตอนนี้ตัวจัดการคลัสเตอร์สกัดกั้นเหตุการณ์ที่คุณติดตั้งใช้งานใน clusterManager แล้ว และส่งต่อเหตุการณ์ที่เหลือไปยังผู้รับมอบสิทธิ์แผนที่ หากมี โปรดทราบว่าเหตุการณ์สำหรับตัวทำเครื่องหมายมาตรฐาน (กล่าวคือ ตัวทำเครื่องหมายที่ไม่ได้สร้างขึ้นโดยตัวแสดงผลคลัสเตอร์) จะส่งต่อไปยังผู้รับมอบสิทธิ์แผนที่เสมอ

ปรับแต่งการคลัสเตอร์เครื่องหมาย

คุณระบุการติดตั้งใช้งานที่กําหนดเองสําหรับ GMUClusterRenderer, GMUClusterIconGenerator หรือ GMUClusterAlgorithm ได้ คุณอาจยึดการติดตั้งใช้งานที่กําหนดเองจากตัวอย่างการใช้โปรโตคอลเหล่านี้ที่รวมอยู่ในไลบรารียูทิลิตี หรือเขียนโค้ดการติดตั้งใช้งานที่กําหนดเองทั้งหมดโดยปฏิบัติตามโปรโตคอลก็ได้