Gunakan subclass GMSAdvancedMarker
untuk membuat fitur penanda dasar atau
tertentu, seperti yang ditunjukkan di bawah. Sebagai subclass dari GMSMarker
, GMSAdvancedMarker
memberikan penanda dengan lebih banyak ekspresi.
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;
Menghapus penanda lanjutan
Serupa dengan GMSMarker
, Anda dapat menghapus penanda lanjutan dari peta dengan menetapkan properti peta GMSAdvancedMarker
ke nil
. Atau, Anda dapat menghapus semua overlay (termasuk penanda
lanjutan) di peta dengan memanggil metode clear
GMSMapView
.
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];
Jika Anda ingin memodifikasi penanda setelah menambahkannya ke peta,
pastikan Anda tetap mempertahankan objek GMSAdvancedMarker
. Anda dapat memodifikasi penanda nanti dengan membuat perubahan pada objek ini.
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;
Kemampuan peta
Properti mapCapabilities
di GMSMapView
menambahkan pemeriksaan terprogram untuk fitur khusus peta. Hal ini berguna jika ingin mengetahui apakah
kemampuan peta tertentu tersedia sebelum memanggil API tertentu. Fungsi
didChangeMapCapabilities
dari
GMSMapViewDelegate
juga
dipanggil saat kemampuan berubah.
Kueri ini menentukan apakah tampilan peta mendukung penanda lanjutan.
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; }