การซ่อนฟีเจอร์แผนที่ด้วยการจัดรูปแบบ

เลือกแพลตฟอร์ม: 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

แผนที่ที่มีสไตล์ใช้แนวคิด 2 อย่างในการใช้สีและการเปลี่ยนแปลงสไตล์อื่นๆ กับแผนที่

  • ตัวเลือก จะระบุคอมโพเนนต์ทางภูมิศาสตร์ที่คุณจัดรูปแบบบนแผนที่ได้ ซึ่งรวมถึงถนน สวนสาธารณะ แหล่งน้ำ และอื่นๆ รวมถึงป้ายกำกับของคอมโพเนนต์เหล่านั้น ตัวเลือกประกอบด้วย ฟีเจอร์ และ องค์ประกอบ ซึ่งระบุเป็น featureType และ elementType พร็อพเพอร์ตี้
  • ตัวจัดรูปแบบ คือพร็อพเพอร์ตี้สีและการมองเห็นที่คุณใช้กับองค์ประกอบแผนที่ได้ โดยจะกำหนดสีที่แสดงผ่านการผสมผสานระหว่างค่าสี ค่าสี และค่าความสว่าง/แกมมา

ดูคำอธิบายโดยละเอียดเกี่ยวกับตัวเลือกการจัดรูปแบบ JSON ได้ที่ข้อมูลอ้างอิงสไตล์

วิซาร์ดการจัดรูปแบบ Maps Platform

ใช้วิซาร์ดการจัดรูปแบบ Maps Platform เพื่อสร้างออบเจ็กต์การจัดรูปแบบ JSON อย่างรวดเร็ว Maps SDK สำหรับ iOS รองรับคำประกาศสไตล์เดียวกับ Maps JavaScript API

ตัวอย่างโค้ดแบบเต็ม

ที่เก็บ ApiDemos ใน GitHub มี ตัวอย่างที่แสดงการใช้การจัดรูปแบบ