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

יצירת סגנון מפה חדש
כדי ליצור סגנון מפה חדש, פועלים לפי ההוראות במאמר ניהול סגנונות מפה. בסיום, משייכים את הסגנון למזהה המפה החדש שנוצר.
בחירת שכבות במפה
במסוף Google Cloud אתם יכולים לבחור אילו שכבות במפה יוצגו. המאפיין הזה קובע אילו סוגי גבולות יופיעו במפה (לדוגמה, יישובים, מדינות וכו').
ניהול שכבות במפה
במסוף Google Cloud, נכנסים לדף סגנונות המפה.
אם מוצגת בקשה, בוחרים פרויקט.
בוחרים סגנון מפה.
לוחצים על התפריט הנפתח שכבות במפה כדי להוסיף או להסיר שכבות.
לוחצים על שמירה כדי לשמור את השינויים ולהחיל אותם על המפות.
עדכון קוד האתחול של המפה
בשלב הזה צריך לשייך מזהה מפה לסגנון שבו מופעלת שכבה אחת או יותר במפה. כדי לוודא שמזהה המפה מוגדר בצורה נכונה במסוף Cloud, בודקים איך הוא מוגדר בקטע Maps Management.
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. בדוגמה הבאה מוסבר איך להשתמש בפרוטוקול כדי לבדוק את הדרישות לסגנון מבוסס-נתונים.
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