הסתרת תכונות המפה באמצעות שינוי הסגנון

בחירת פלטפורמה: Android iOS JavaScript

אפשר להסתיר אותן לגמרי במפה בנוסף לשינוי הסגנון של התכונות במפה. בדוגמה הזו אפשר לראות איך להסתיר נקודות עניין של עסקים (POI) וסמלים של תחבורה ציבורית במפה.

העיצוב פועל רק בסוג המפה kGMSTypeNormal.

המערכת מחילה סגנונות על המפה שלך

כדי להחיל סגנונות מפה מותאמים אישית על מפה, קוראים לפונקציה GMSMapStyle(...) כדי ליצור מכונה של GMSMapStyle, להעביר את כתובת ה-URL של קובץ JSON מקומי או להזין מחרוזת JSON שמכילה את הגדרות הסגנון. מקצים את המופע של GMSMapStyle למאפיין mapStyle במפה.

שימוש בקובץ JSON

בדוגמאות הבאות אפשר לראות קריאה ל-GMSMapStyle(...) והעברת כתובת URL של קובץ מקומי:

דוגמת הקוד הבאה מבוססת על ההנחה שהפרויקט שלכם מכיל קובץ בשם style.json:

Swift

import GoogleMaps

class MapStyling: UIViewController {

  // Set the status bar style to complement night-mode.
  override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
  }

  override func loadView() {
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 14.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

    do {
      // Set the map style by passing the URL of the local file.
      if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") {
        mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
      } else {
        NSLog("Unable to find style.json")
      }
    } catch {
      NSLog("One or more of the map styles failed to load. \(error)")
    }

    self.view = mapView
  }
}
      

Objective-C

#import "MapStyling.h"
@import GoogleMaps;

@interface MapStyling ()

@end

@implementation MapStyling

// Set the status bar style to complement night-mode.
- (UIStatusBarStyle)preferredStatusBarStyle {
  return UIStatusBarStyleLightContent;
}

- (void)loadView {
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                          longitude:151.20
                                                               zoom:12];
  GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  mapView.myLocationEnabled = YES;

  NSBundle *mainBundle = [NSBundle mainBundle];
  NSURL *styleUrl = [mainBundle URLForResource:@"style" withExtension:@"json"];
  NSError *error;

  // Set the map style by passing the URL for style.json.
  GMSMapStyle *style = [GMSMapStyle styleWithContentsOfFileURL:styleUrl error:&error];

  if (!style) {
    NSLog(@"The style definition could not be loaded: %@", error);
  }

  mapView.mapStyle = style;
  self.view = mapView;
}

@end
      

כדי להגדיר את אפשרויות הסגנון, מוסיפים קובץ חדש לפרויקט style.json, ומדביקים את הצהרת סגנון ה-JSON הבאה כדי להסתיר את נקודות העניין של העסק (POI) ואת הסמלים של התחבורה הציבורית:

שימוש במשאב מחרוזות

בדוגמאות הבאות אפשר לראות קריאה ל-GMSMapStyle() והעברת משאב מחרוזת:

Swift

class MapStylingStringResource: UIViewController {

  let MapStyle = "JSON_STYLE_GOES_HERE"

  // Set the status bar style to complement night-mode.
  override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
  }

  override func loadView() {
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 14.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

    do {
      // Set the map style by passing a valid JSON string.
      mapView.mapStyle = try GMSMapStyle(jsonString: MapStyle)
    } catch {
      NSLog("One or more of the map styles failed to load. \(error)")
    }

    self.view = mapView
  }
}
      

Objective-C

@implementation MapStylingStringResource

// Paste the JSON string to use.
static NSString *const kMapStyle = @"JSON_STYLE_GOES_HERE";

// Set the status bar style to complement night-mode.
- (UIStatusBarStyle)preferredStatusBarStyle {
  return UIStatusBarStyleLightContent;
}

- (void)loadView {
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                          longitude:151.20
                                                               zoom:12];
  GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  mapView.myLocationEnabled = YES;

  NSError *error;

  // Set the map style by passing a valid JSON string.
  GMSMapStyle *style = [GMSMapStyle styleWithJSONString:kMapStyle error:&error];

  if (!style) {
    NSLog(@"The style definition could not be loaded: %@", error);
  }

  mapView.mapStyle = style;
  self.view = mapView;
}

@end
      

הצהרת הסגנון הבאה מסתירה נקודות עניין של העסק (POI) וסמלים של תחבורה ציבורית. מדביקים את מחרוזת הסגנון הבאה כערך של המשתנה kMapStyle:

הצהרות בסגנון JSON

במפות מעוצבות נעשה שימוש בשני מושגים כדי להחיל צבעים ושינויי סגנון אחרים במפה:

  • סלקטורים מציינים את הרכיבים הגיאוגרפיים שאפשר לעצב במפה. הם כוללים כבישים, פארקים, גופי מים ועוד, והתוויות שלהם. הסלקטורים כוללים features ורכיבים, שצוינו כמאפיינים featureType ו-elementType.
  • סטיילרים הם מאפייני צבע וחשיפה שאפשר להחיל על רכיבי מפה. הם מגדירים את הצבע המוצג באמצעות שילוב של ערכי גוון, צבע ובהירות/גמא.

בחומר העזר בנושא סגנון תוכלו לקרוא תיאור מפורט של אפשרויות הסגנון של JSON.

אשף העיצוב בפלטפורמה של מפות Google

אפשר להשתמש באשף עיצוב הפלטפורמה של מפות Google כדי ליצור במהירות אובייקט בעיצוב JSON. ה-SDK של מפות Google ל-iOS תומך באותו סגנון של הצהרות כמו API JavaScript של מפות Google.

דוגמאות קוד מלא

מאגר ApiDemos ב-GitHub כולל דוגמאות שמדגימות את השימוש בסגנון.