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

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

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

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

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

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

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

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

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