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

בחירת פלטפורמה: 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 options = GMSMapViewOptions()
    options.camera = camera
    let mapView = GMSMapView(options: options)

    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];
  GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init];
  options.camera = camera;
  GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options];
  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 options = GMSMapViewOptions()
    options.camera = camera
    let mapView = GMSMapView(options: options)

    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];
  GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init];
  options.camera = camera;
  GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options];
  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

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

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

בהפניית הסגנון מופיע תיאור מפורט של אפשרויות העיצוב ב-JSON.

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

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

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

המאגר ApiDemos ב-GitHub כולל דוגמאות שממחישות את השימוש בעיצוב.