כדי להשתמש בסגנון מבוסס-נתונים לגבולות, צריך ליצור מזהה מפה. לאחר מכן, צריך ליצור סגנון מפה חדש, לבחור את שכבות התכונות של הגבולות שרוצים ולהקצות את הסגנון למזהה המפה.
יצירת מזהה מפה
mapID הוא מזהה ייחודי שמייצג מופע יחיד של מפת Google. במסוף Google Cloud אפשר ליצור מזהי מפות ולעדכן סגנון שמשויך למזהה מפה בכל שלב.

יצירת סגנון מפה חדש
כדי ליצור סגנון מפה חדש, פועלים לפי ההוראות שבמאמר ניהול סגנונות מפה. אחרי שיוצרים את מזהה המפה החדש, מקשרים אליו את הסגנון.
בחירת שכבות של ישויות
במסוף Google Cloud אפשר לבחור אילו שכבות של תכונות יוצגו. ההגדרה הזו קובעת אילו סוגים של גבולות יופיעו במפה (לדוגמה, יישובים, מדינות וכו').
ניהול שכבות של תכונות
במסוף Google Cloud, עוברים לדף Map Styles.
אם מתבקשים, בוחרים פרויקט.
בוחרים סגנון מפה.
לוחצים על התפריט הנפתח שכבות של תכונות כדי להוסיף או להסיר שכבות.
לוחצים על שמירה כדי לשמור את השינויים ולהפוך אותם לזמינים במפות.
עדכון קוד ההפעלה של המפה
בשלב הזה צריך לשייך מזהה מפה לסגנון עם שכבת תכונות אחת או יותר שמופעלות. כדי לוודא שמזהה המפה מוגדר בצורה נכונה במסוף Cloud, בודקים את ההגדרה שלו בקטע ניהול מפות.
Swift
// A map ID using a style with one or more feature layers enabled let mapID = GMSMapID(identifier: "YOUR_MAP_ID") let camera = GMSCameraPosition(latitude: 40, longitude: -80, zoom: 7) let options = GMSMapViewOptions() options.camera = camera options.mapID = mapID let mapView = GMSMapView(options: options)
Objective-C
// A map ID using a style with one or more feature layers enabled GMSMapID *mapID = [GMSMapID mapIDWithIdentifier:@"MAP_ID"]; GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40 longitude:-80 zoom:7]; GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init]; options.camera = camera; options.mapID = mapID; GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options];
הוספת שכבות של ישויות למפה
כדי לקבל הפניה לשכבת ישויות במפה, קוראים לפונקציה
mapView.featureLayer(of:)
כשמאתחלים את המפה:
Swift
let layer = mapView.featureLayer(of: .locality)
Objective-C
GMSFeatureLayer*layer = [mapView featureLayerOfFeatureType:GMSFeatureTypeLocality];
בדיקת יכולות המפה
כדי להשתמש בעיצוב גבולות מבוסס-נתונים, צריך להפעיל יכולות במסוף Google Cloud ולשייך אותן למזהה מפה. מכיוון שמזהי מפות עשויים להשתנות, אפשר להפעיל את mapView.mapCapabilities ב-GMSMapView כדי לוודא שיכולת מסוימת (למשל, סגנון מבוסס-נתונים) זמינה לפני שמפעילים אותה.
אפשר גם להירשם לGMSViewDelegate כדי לקבל עדכונים על שינויים ביכולות של מפות Google. בדוגמה הזו מוצג איך להשתמש בפרוטוקול כדי לבדוק את הדרישות של סגנון מבוסס-נתונים.
Swift
class SampleViewController: UIViewController { private lazy var mapView: GMSMapView = { let options = GMSMapViewOptions() options.mapID = GMSMapID(identifier: "YOUR_MAP_ID") options.camera = GMSCameraPosition(latitude: 40, longitude: -80, zoom: 7) return GMSMapView(options: options) }() override func loadView() { self.view = mapView mapView.delegate = self } } extension SampleViewController: GMSMapViewDelegate { func mapView(_ mapView: GMSMapView, didChange mapCapabilities: GMSMapCapabilityFlags) { if (!mapCapabilities.contains(.dataDrivenStyling)) { // Data-driven styling is *not* available, add a fallback. // Existing feature layers are also unavailable. } } }
Objective-C
@interface SampleViewController: UIViewController <GMSMapViewDelegate> @end @implementation SampleViewController - (void)loadView { GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40 longitude:-80 zoom:7]; GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init]; options.camera = camera; options.mapID = [GMSMapID mapIDWithIdentifier:@"MAP_ID"]; GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options]; mapView.delegate = self; self.view = mapView; } - (void)mapView:(GMSMapView *)mapView didChangeMapCapabilities:(GMSMapCapabilityFlags)mapCapabilities { if (!(mapCapabilities & GMSMapCapabilityFlagsDataDrivenStyling)) { // Data-driven styling is *not* available, add a fallback. // Existing feature layers are also unavailable. } } @end