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

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

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

วิซาร์ดการจัดรูปแบบแพลตฟอร์มแผนที่

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

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

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

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

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