เริ่มใช้งาน

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

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

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

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

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

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

ภาพหน้าจอของ Google Cloud Console

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

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

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

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

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 และเชื่อมโยงกับรหัสแผนที่ เนื่องจากรหัสแผนที่อาจมีการเปลี่ยนแปลง คุณจึงเรียกใช้ 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

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