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

בחירת פלטפורמה: 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() והעברת מחרוזת resource:

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

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

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

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

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

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

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

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