यह पेज मार्कर क्लस्टर करने की सुविधा की जानकारी देता है, जो iOS के लिए Maps SDK टूल की यूटिलिटी लाइब्रेरी में उपलब्ध है.
अपने मार्कर को क्लस्टर करके, आप मैप को पढ़ने में मुश्किल बनाए बिना, मैप पर बड़ी संख्या में मार्कर रख सकते हैं. मार्कर क्लस्टर बनाने की सुविधा की मदद से आप अलग-अलग ज़ूम लेवल पर कई मार्कर प्रबंधित कर सकते हैं.
जब कोई उपयोगकर्ता मैप को ज़्यादा ज़ूम लेवल पर देखता है, तो मैप पर अलग-अलग मार्कर दिखते हैं. जब उपयोगकर्ता ज़ूम आउट करता है, तो मार्कर को क्लस्टर में एक साथ रखा जाता है, ताकि मैप को आसानी से देखा जा सके.
नीचे दिया गया स्क्रीनशॉट, मार्कर क्लस्टर की डिफ़ॉल्ट स्टाइल दिखाता है:

नीचे कस्टम मार्कर क्लस्टर का उदाहरण दिया गया है:

ज़रूरतें और नोट
iOS Utility लाइब्रेरी के लिए Maps SDK टूल
मार्कर क्लस्टर बनाने की सुविधा, iOS यूटिलिटी लाइब्रेरी के लिए Maps SDK टूल का हिस्सा है. अगर आपने अभी तक लाइब्रेरी सेट अप नहीं की है, तो इस पेज के बाकी हिस्से को पढ़ने से पहले, सेट अप गाइड में दिए गए निर्देशों का पालन करें.
बेहतर परफ़ॉर्मेंस के लिए, मार्कर की सुझाई गई ज़्यादा से ज़्यादा संख्या 10,000 है.
जगह की जानकारी ऐक्सेस करने की अनुमति
यह उदाहरण उपयोगकर्ता के मैप और उसके निर्देशांकों को मैप करने के लिए, डिवाइस के जीपीएस का इस्तेमाल करता है. इसे
चालू करने के लिए, आपको प्रोजेक्ट की NSLocationWhenInUseUsageDescription
फ़ाइल
में NSLocationWhenInUseUsageDescription
की जानकारी जोड़नी होगी.
इसे जोड़ने के लिए, ये काम करें:
- प्रॉपर्टी की सूची में बदलाव करने वाला टूल खोलने के लिए, Xcode के प्रोजेक्ट नेविगेटर में
Info.plist
फ़ाइल पर क्लिक करें. - नई प्रॉपर्टी जोड़ने के लिए, 'जानकारी प्रॉपर्टी सूची' के आगे दिए गए '+' आइकॉन पर क्लिक करें.
- 'key' फ़ील्ड में, और#39;NSLocationwhenInUseUsageDescription' लिखें. Xcode का मतलब है कि उसका पूरा नाम और #39;निजता - इस्तेमाल किए जाने के समय की जगह की जानकारी'. जगह की जानकारी की संभावित प्रॉपर्टी की पूरी सूची देखने के लिए, Apple डेवलपर के दस्तावेज़ में जगह की जानकारी को अनुमति देने का अनुरोध करना देखें.
- 'Type' फ़ील्ड को #39;String' पर सेट छोड़ दें.
- 'वैल्यू' फ़ील्ड में, इस बारे में जानकारी दें कि आपका ऐप्लिकेशन, उपयोगकर्ता की जगह की जानकारी का इस्तेमाल क्यों करना चाहता है. जैसे, "आस-पास के कारोबार की लिस्टिंग देने के लिए उपयोगकर्ता का पता लगाता है."
मार्कर क्लस्टर लागू करना
मार्कर क्लस्टर लागू करने के तीन चरण हैं:
- क्लस्टर मैनेजर इंस्टेंस बनाएं.
- उन मार्कर को पास करें जिन्हें आप क्लस्टर निर्माता में शामिल करना चाहते हैं.
- क्लस्टर मैनेजर को न्योता दें.
क्लस्टर मैनेजर बनाना
क्लस्टर मैनेजर का इस्तेमाल करने के लिए, ये करें:
GMSMapViewDelegate
प्रोटोकॉल के मुताबिकViewController
को वहां सेट करें जहां आपका मैप रेंडर किया गया है.GMUClusterManager
का इंस्टेंस बनाएं.GMSMapView
के उस इंस्टेंस को पास करें जिसमें आप इन प्रोटोकॉल कोGMUClusterManager
इंस्टेंस में लागू करना और लागू करना चाहते हैं:GMUClusterIconGenerator
: ऐप्लिकेशन लॉजिक की सुविधा देता है, जो अलग-अलग ज़ूम लेवल पर इस्तेमाल करने के लिए क्लस्टर आइकॉन फ़ेच करता है.GMUClusterAlgorithm
: इससे, एक एल्गोरिदम के बारे में पता चलता है. इससे, यह पता चलता है कि मार्कर कैसे क्लस्टर बनाए जाते हैं. जैसे कि एक ही क्लस्टर में शामिल करने के लिए, मार्कर के बीच की दूरी क्या होती है.GMUClusterRenderer
: ऐप्लिकेशन लॉजिक देता है, जो मैप पर क्लस्टर आइकॉन की असल रेंडरिंग को मैनेज करता है.
- मैप इंस्टेंस को
GMUClusterManager
इंस्टेंस पर सेट करें.
उपयोगिता लाइब्रेरी में आइकॉन जनरेटर (GMUDefaultClusterIconGenerator
), एल्गोरिदम (GMUNonHierarchicalDistanceBasedAlgorithm
) और रेंडरर (GMUDefaultClusterRenderer
) को डिफ़ॉल्ट तौर पर लागू किया जाता है. आपके पास अपनी पसंद के मुताबिक क्लस्टरिंग आइकॉन जनरेटर, एल्गोरिदम, और रेंडरर बनाने का विकल्प भी है.
नीचे दिया गया कोड, ViewController
के viewDidLoad
कॉलबैक में इन डिफ़ॉल्ट का इस्तेमाल करके, क्लस्टर मैनेजर बनाता है:
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
मार्कर जोड़ना
मार्कर क्लस्टर में मार्कर जोड़ने के दो तरीके हैं: अलग-अलग या श्रेणी के रूप में.
व्यक्तिगत मार्कर
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];
मार्कर और क्लस्टर पर इवेंट प्रबंधित करना
आम तौर पर, iOS के लिए Maps SDK टूल का इस्तेमाल करते समय, मैप पर इवेंट सुनने के लिए, आपको GMSMapViewDelegate
प्रोटोकॉल लागू करना होगा. आप
इवेंट मैप करें सुन सकते हैं, लेकिन आप टाइप-सुरक्षित क्लस्टर मैनेजर इवेंट नहीं सुन सकते. जब उपयोगकर्ता किसी मार्कर,
किसी क्लस्टर वाले आइटम या क्लस्टर पर टैप करता है, तो एपीआई
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
को अपने हिसाब से लागू किया जा सकता है. आप अपनी पसंद के मुताबिक लागू करने की प्रोसेस को
यूटिलिटी लाइब्रेरी में शामिल इन प्रोटोकॉल के सैंपल
पर लागू करके कर सकते हैं या प्रोटोकॉल को पूरा करके, कस्टम प्रोटोकॉल को पूरी तरह से
लागू कर सकते हैं.