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

เลือกแพลตฟอร์ม แอนดรอยด์ iOS JavaScript

หน้านี้เป็นคู่มือฉบับย่อสำหรับการจัดรูปแบบแผนที่ของคุณ โดยใช้โหมดกลางคืนเป็นตัวอย่าง

ภาพรวม

ด้วยตัวเลือกรูปแบบ คุณสามารถปรับแต่งการนำเสนอรูปแบบ รูปแบบแผนที่ การเปลี่ยนการแสดงภาพของคุณลักษณะต่างๆ เช่น ถนน สวนสาธารณะ ธุรกิจ และจุดที่น่าสนใจอื่นๆ ซึ่งหมายความว่าคุณสามารถเน้นย้ำ ส่วนประกอบบางอย่างของแผนที่ หรือทำให้แผนที่ส่งเสริมรูปแบบ แอป

การจัดรูปแบบใช้ได้เฉพาะในแผนที่ประเภท 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

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

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

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

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

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

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

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

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

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