משתמשים במחלקה המשנית GMSAdvancedMarker
כדי ליצור תכונות סמנים בסיסיות או ספציפיות, כפי שמתואר בהמשך. כתת-מחלקה של GMSMarker
, GMSAdvancedMarker
מספקת סמנים עם יותר ביטוי.
Swift
let camera = GMSCameraPosition( target: coordinate, zoom: 14) let mapID = GMSMapID(identifier: "YOUR_MAP_ID") let mapView = GMSMapView(frame: view.bounds, mapID: mapID, camera: camera) let marker = GMSAdvancedMarker(position: coordinate) marker.map = mapView
Objective-C
GMSCameraPosition *camera = [GMSCameraPosition cameraWithTarget:kCoordinate zoom:16]; GMSMapID *mapID = [GMSMapID mapIDWithIdentifier:"YOUR_MAP_ID"]; self.mapView = [GMSMapView mapWithFrame:self.view.bounds mapID:mapID camera:camera]; GMSAdvancedMarker *marker = [GMSAdvancedMarker markerWithPosition:kCoordinate]; Marker.map = self.mapView;
הסרת סמן מתקדם
בדומה ל-GMSMarker
, אפשר להסיר סמן מתקדם מהמפה על ידי הגדרת מאפיין המפה של GMSAdvancedMarker
ל-nil
. לחלופין, אפשר להסיר את כל שכבות-העל (כולל סמנים מתקדמים) מהמפה על ידי קריאה לשיטה GMSMapView
clear
.
Swift
let camera = GMSCameraPosition.camera( withLatitude: -33.8683, longitude: 151.2086, zoom: 6 ) let mapView = GMSMapView.map(withFrame: .zero, camera: camera) // ... mapView.clear()
Objective-C
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.8683 longitude:151.2086 zoom:6]; mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; // ... [mapView clear];
אם רוצים לבצע שינויים בסמן אחרי שמוסיפים אותו למפה, חשוב להחזיק את האובייקט GMSAdvancedMarker
. אפשר לשנות את הסמן בשלב מאוחר יותר על ידי ביצוע שינויים באובייקט הזה.
Swift
let position = CLLocationCoordinate2D(latitude: 10, longitude: 10) let marker = GMSAdvancedMarker(position: position) marker.map = mapView // ... marker.map = nil
Objective-C
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(10, 10); GMSAdvancedMarker *marker = [GMSAdvancedMarker markerWithPosition:position]; marker.map = mapView; // ... marker.map = nil;
יכולות מפה
המאפיין mapCapabilities
ב-GMSMapView
כולל בדיקה פרוגרמטית של תכונות ספציפיות למפה. האפשרות הזו שימושית כשרוצים לדעת אם יש יכולות מסוימות במפה לפני קריאה לממשקי API ספציפיים. גם הפונקציה didChangeMapCapabilities
של GMSMapViewDelegate
מופעלת כשהיכולות משתנות.
השאילתה הזו קובעת אם תצוגת המפה תומכת בסמנים מתקדמים.
Swift
// ... let advancedMarker: GMSAdvancedMarker = { GMSAdvancedMarker(position: CLLocationCoordinate2D(latitude: 47.6089945, longitude: -122.3410462)) }() let marker: GMSMarker = { GMSMarker(position: CLLocationCoordinate2D(latitude: 47.6089945, longitude: -122.3410462)) }() func addMarker() { if mapView.mapCapabilities.contains(.advancedMarkers) { advancedMarker.map = mapView } else { marker.map = mapView } } extension MapCapabilities: GMSMapViewDelegate { func mapView(_ mapView: GMSMapView, didChangeMapCapabilities mapCapabilities: GMSMapCapabilityFlags) { let advancedMarkerAvailable = mapCapabilities.contains(.advancedMarkers) advancedMarker.map = advancedMarkerAvailable ? mapView : nil marker.map = advancedMarkerAvailable ? nil : mapView } }
Objective-C
// ... _advancedMarker = [GMSAdvancedMarker markerWithPosition: kSeattleCoordinates]; _fallbackMarker = [GMSMarker markerWithPosition: kSeattleCoordinates]; - (void)addMarker { if (_mapView.mapCapabilities & GMSMapCapabilityFlagsAdvancedMarkers) { _advancedMarker.map = _mapView; } else { _fallbackMarker.map = _mapView; } } #pragma mark - GMSMapViewDelegate - (void)mapView:(GMSMapView *)mapView didChangeMapCapabilities:(GMSMapCapabilityFlags)mapCapabilities { BOOL advancedMarkersAvailable = mapCapabilities & GMSMapCapabilityFlagsAdvancedMarkers; _advancedMarker.map = advancedMarkersAvailable ? _mapView : nil; _fallbackMarker.map = advancedMarkersAvailable ? nil : _mapView; }