เพิ่มแผนที่ที่มีการจัดรูปแบบ

จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เลือกแพลตฟอร์ม: 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

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

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

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

วิซาร์ดการจัดรูปแบบแพลตฟอร์ม Maps

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

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

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

ขั้นตอนถัดไป

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