เริ่มใช้งาน

เลือกแพลตฟอร์ม: Android iOS JavaScript

ทำตามขั้นตอนต่อไปนี้เพื่อตั้งค่าการจัดรูปแบบตามข้อมูลสำหรับชุดข้อมูล

รับคีย์ API และเปิดใช้ API

ก่อนที่จะใช้การจัดรูปแบบตามข้อมูลสำหรับชุดข้อมูล คุณต้องมีโปรเจ็กต์ Cloud ที่มีบัญชีสำหรับการเรียกเก็บเงิน และเปิดใช้ทั้ง Maps SDK สำหรับ iOS และ Maps Datasets API ดูข้อมูลเพิ่มเติมได้ที่

สร้างรหัสแผนที่

รหัสแผนที่เป็นตัวระบุที่ไม่ซ้ำกันซึ่ง แสดงถึงข้อกำหนดของ Google Maps คุณสามารถสร้างรหัสแผนที่และอัปเดตรูปแบบที่เชื่อมโยงกับรหัสแผนที่ได้ทุกเมื่อใน Google Cloud Console

ภาพหน้าจอของคอนโซล Google Cloud

สร้างรูปแบบแผนที่ใหม่

หากต้องการสร้างรูปแบบแผนที่ใหม่ ให้ทำตามวิธีการใน สร้างและใช้รูปแบบแผนที่เพื่อสร้าง รูปแบบ เมื่อสร้างเสร็จแล้ว ให้เชื่อมโยงรูปแบบกับรหัสแผนที่ที่สร้างขึ้นใหม่

อัปเดตโค้ดการเริ่มต้นแผนที่

ขั้นตอนนี้กำหนดให้ต้องเชื่อมโยงรหัสแผนที่กับรูปแบบที่มีการเปิดใช้เลเยอร์ฟีเจอร์อย่างน้อย 1 รายการ หากต้องการยืนยันว่าได้ตั้งค่ารหัสแผนที่อย่างถูกต้องใน Cloud Console แล้ว ให้ตรวจสอบวิธีที่กำหนดค่าไว้ในส่วนการจัดการ Maps

Swift

// A map ID using a style with one or more feature layers enabled

let mapID = GMSMapID(identifier: "YOUR_MAP_ID")
let camera = GMSCameraPosition(latitude: 40, longitude: -80, zoom: 7)
let options = GMSMapViewOptions()
options.camera = camera
options.mapID = mapID
let mapView = GMSMapView(options: options)

Objective-C

// A map ID using a style with one or more feature layers enabled

GMSMapID *mapID = [GMSMapID mapIDWithIdentifier:@"MAP_ID"];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40 longitude:-80 zoom:7];
GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init];
options.camera = camera;
options.mapID = mapID;
GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options];

ตรวจสอบความสามารถของแผนที่

การจัดรูปแบบตามข้อมูลสำหรับชุดข้อมูลต้องใช้ความสามารถที่เปิดใช้ใน Google Cloud Console และเชื่อมโยงกับรหัสแผนที่ เนื่องจากรหัสแผนที่อาจมีการเปลี่ยนแปลง คุณจึงเรียก mapView.mapCapabilities ใน GMSMapView เพื่อยืนยันว่าความสามารถบางอย่าง (เช่น การจัดรูปแบบตามข้อมูล) พร้อมใช้งานหรือไม่ก่อนที่จะเรียกใช้

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

Swift

class SampleViewController: UIViewController {

  private lazy var mapView: GMSMapView = {
    let options = GMSMapViewOptions()
    options.mapID = GMSMapID(identifier: "YOUR_MAP_ID")
    options.camera = GMSCameraPosition(latitude: 40, longitude: -80, zoom: 7)
    return GMSMapView(options: options)
  }()

  override func loadView() {
    self.view = mapView
    mapView.delegate = self
  }
}

extension SampleViewController: GMSMapViewDelegate {
  func mapView(_ mapView: GMSMapView, didChange mapCapabilities: GMSMapCapabilityFlags) {
    if (!mapCapabilities.contains(.dataDrivenStyling)) {
      // Data-driven styling is *not* available, add a fallback.
      // Existing feature layers are also unavailable.
    }
  }
}

Objective-C

@interface SampleViewController: UIViewController <GMSMapViewDelegate>
@end

@implementation SampleViewController
- (void)loadView {
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:40 longitude:-80 zoom:7];
  GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init];
  options.camera = camera;
  options.mapID = [GMSMapID mapIDWithIdentifier:@"MAP_ID"];
  GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options];
  mapView.delegate = self;
  self.view = mapView;
}

- (void)mapView:(GMSMapView *)mapView didChangeMapCapabilities:(GMSMapCapabilityFlags)mapCapabilities {
  if (!(mapCapabilities & GMSMapCapabilityFlagsDataDrivenStyling)) {
    // Data-driven styling is *not* available, add a fallback.
    // Existing feature layers are also unavailable.
  }
}
@end

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