הוספת מפה מעוצבת

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

בדף הזה תמצאו מדריך קצר לעיצוב המפה, בעזרת מצב לילה כדוגמה.

סקירה

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

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

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

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

שימוש בקובץ JSON

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

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 בשביל עיצוב במצב לילה:

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

בדוגמאות הבאות אפשר לראות קריאה ל-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
      

כדי להגדיר את אפשרויות הסגנון, מדביקים את מחרוזת הסגנון הבאה כערך של המשתנה kMapStyle:

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

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

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

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

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

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

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

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

השלב הבא

בקישור הבא מוסבר איך להסתיר תכונות במפה באמצעות עיצוב.