เริ่มต้นใช้งาน

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

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

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

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

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

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

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

เลือกเลเยอร์ของฟีเจอร์

ใน Google Cloud Console คุณสามารถเลือกเลเยอร์ของฟีเจอร์ที่จะแสดงได้ ช่วงเวลานี้ กำหนดขอบเขตประเภทของขอบเขตที่จะแสดงบนแผนที่ (เช่น ย่าน สถานะ และอื่นๆ)

จัดการเลเยอร์ของฟีเจอร์

  1. ไปที่รูปแบบแผนที่ในคอนโซล Google Cloud

  2. เลือกโปรเจ็กต์เมื่อได้รับแจ้ง

  3. เลือกรูปแบบแผนที่

  4. คลิกเมนูแบบเลื่อนลงเลเยอร์ของฟีเจอร์เพื่อเพิ่มหรือนำเลเยอร์ออก

  5. คลิกบันทึกเพื่อบันทึกการเปลี่ยนแปลงและทำให้พร้อมใช้งานกับแผนที่ของคุณ

ภาพหน้าจอแสดง
แบบเลื่อนลง

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

ขั้นตอนนี้กำหนดให้เชื่อมโยงรหัสแผนที่กับสไตล์ที่มีอย่างน้อย 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 mapView = GMSMapView(frame: .zero, mapID: mapID, camera: GMSCameraPosition(latitude: 40, longitude: -80, zoom: 7))

Objective-C

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

GMSMapID *mapID = [GMSMapID mapIDWithIdentifier:@"MAP_ID"];
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero mapID:mapID camera:[GMSCameraPosition cameraWithLatitude:40 longitude:-80 zoom:7]];

เพิ่มเลเยอร์ฟีเจอร์ลงในแผนที่

หากต้องการทราบการอ้างอิงเลเยอร์ฟีเจอร์บนแผนที่ของคุณ ให้เรียก mapView.featureLayer(of:) เมื่อแผนที่เริ่มต้น:

Swift

let layer = mapView.featureLayer(of: .locality)

Objective-C

GMSFeatureLayer *layer = [mapView featureLayerOfFeatureType:GMSFeatureTypeLocality];

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

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

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

Swift

class SampleViewController: UIViewController {

  private lazy var mapView: GMSMapView = GMSMapView(frame: .zero, mapID: GMSMapID(identifier: "YOUR_MAP_ID"), camera: GMSCameraPosition(latitude: 40, longitude: -80, zoom: 7))

  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 {
  GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero mapID:[GMSMapID mapIDWithIdentifier:@"MAP_ID"] camera:[GMSCameraPosition cameraWithLatitude:40 longitude:-80 zoom:7]];
  mapView.delegete = 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