Hãy làm theo các bước sau để thiết lập kiểu dựa trên dữ liệu cho tập dữ liệu.
Lấy khoá API và bật API
Trước khi sử dụng tính năng tạo kiểu dựa trên dữ liệu cho tập dữ liệu, bạn cần có:
Dự án trên Google Cloud có tài khoản thanh toán và cả Maps SDK cho iOS cũng như Maps Datasets API đều được bật. Để tìm hiểu thêm, hãy xem:
Mã bản đồ là một giá trị nhận dạng duy nhất đại diện cho một quy cách của Google Maps. Bạn có thể tạo mã bản đồ và cập nhật kiểu liên kết với mã bản đồ bất cứ lúc nào trong Bảng điều khiển Google Cloud.
Tạo kiểu bản đồ mới
Để tạo kiểu bản đồ mới, hãy làm theo hướng dẫn trong phần Tạo và sử dụng kiểu bản đồ để tạo kiểu. Sau khi hoàn tất, hãy liên kết kiểu với mã bản đồ mới tạo.
Cập nhật mã khởi chạy bản đồ
Bước này yêu cầu bạn liên kết mã nhận dạng bản đồ với một kiểu có một hoặc nhiều lớp đối tượng được bật. Để xác minh rằng bạn đã thiết lập mã bản đồ đúng cách trong bảng điều khiển Cloud, hãy xem cách mã này được định cấu hình trong phần Maps Management (Quản lý bản đồ).
Swift
// A map ID using a style with one or more feature layers enabledletmapID=GMSMapID(identifier:"YOUR_MAP_ID")letmapView=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 enabledGMSMapID*mapID=[GMSMapIDmapIDWithIdentifier:@"MAP_ID"];GMSMapView*mapView=[GMSMapViewmapWithFrame:CGRectZeromapID:mapIDcamera:[GMSCameraPositioncameraWithLatitude:40longitude:-80zoom:7]];
Kiểm tra các chức năng của bản đồ
Tính năng tạo kiểu dựa trên dữ liệu cho tập dữ liệu yêu cầu các chức năng được bật trong bảng điều khiển Google Cloud và được liên kết với một mã bản đồ. Vì mã bản đồ có thể thay đổi, nên bạn có thể gọi mapView.mapCapabilities trên GMSMapView để xác minh xem một chức năng nhất định (ví dụ: tạo kiểu dựa trên dữ liệu) có sẵn hay không trước khi gọi chức năng đó.
Bạn cũng có thể phát hiện các thay đổi về chức năng của bản đồ bằng cách đăng ký GMSViewDelegate.
Ví dụ này cho thấy cách sử dụng giao thức để kiểm tra các yêu cầu về kiểu dựa trên dữ liệu.
Swift
classSampleViewController:UIViewController{privatelazyvarmapView:GMSMapView=GMSMapView(frame:.zero,mapID:GMSMapID(identifier:"YOUR_MAP_ID"),camera:GMSCameraPosition(latitude:40,longitude:-80,zoom:7))overridefuncloadView(){self.view=mapViewmapView.delegate=self}}extensionSampleViewController:GMSMapViewDelegate{funcmapView(_mapView:GMSMapView,didChangemapCapabilities:GMSMapCapabilityFlags){if(!mapCapabilities.contains(.dataDrivenStyling)){// Data-driven styling is *not* available, add a fallback.// Existing feature layers are also unavailable.}}}
Objective-C
@interfaceSampleViewController: UIViewController<GMSMapViewDelegate>
@end@implementationSampleViewController-(void)loadView{GMSMapView*mapView=[GMSMapViewmapWithFrame:CGRectZeromapID:[GMSMapIDmapIDWithIdentifier:@"MAP_ID"]camera:[GMSCameraPositioncameraWithLatitude:40longitude:-80zoom:7]];mapView.delegete=self;self.view=mapView;}-(void)mapView:(GMSMapView*)mapViewdidChangeMapCapabilities:(GMSMapCapabilityFlags)mapCapabilities{if(!(mapCapabilities&GMSMapCapabilityFlagsDataDrivenStyling)){// Data-driven styling is *not* available, add a fallback.// Existing feature layers are also unavailable.}}@end
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[[["\u003cp\u003eTo utilize data-driven styling for datasets within the Maps SDK for iOS, you must set up a Google Cloud project, enable necessary APIs (Maps SDK for iOS & Maps Datasets API), and acquire an API key.\u003c/p\u003e\n"],["\u003cp\u003eA unique map ID linked to a specific map style with enabled feature layers is crucial for loading datasets and using data-driven styling.\u003c/p\u003e\n"],["\u003cp\u003eBefore implementing data-driven styling features, ensure the map's capabilities are compatible by verifying through \u003ccode\u003emapView.mapCapabilities\u003c/code\u003e or subscribing to \u003ccode\u003eGMSViewDelegate\u003c/code\u003e to detect changes and handle potential fallbacks.\u003c/p\u003e\n"],["\u003cp\u003eInitialization of the map view requires using the designated map ID containing the desired style and feature layers for data-driven styling to function correctly.\u003c/p\u003e\n"],["\u003cp\u003eYou cannot utilize a demo map ID for data-driven styling as it necessitates a map ID specifically associated with a map style containing the dataset.\u003c/p\u003e\n"]]],[],null,["Select platform: [Android](/maps/documentation/android-sdk/dds-datasets/start \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/dds-datasets/start \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/dds-datasets/start \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nFollow these steps to get set up with the data-driven styling for datasets.\n\nGet an API key and enable APIs\n\nBefore using data-driven styling for datasets, you need:\nCloud project with a billing account, and both the\nMaps SDK for iOS and the Maps Datasets API\nenabled. To learn more, see:\n\n- [Set up your Google Cloud project](/maps/documentation/ios-sdk/cloud-setup)\n\n- [Enable Maps SDK for iOS and get an API key](/maps/documentation/ios-sdk/get-api-key)\n\n- [Enable the Maps Datasets API](https://console.cloud.google.com/apis/library/mapsplatformdatasets.googleapis.com)\n\nCreate a map ID\n\nA [map ID](/maps/documentation/get-map-id) is a unique identifier that\nrepresents a Google Map specification. You can create map IDs and update\na style associated with a map ID at any time in the Google Cloud console.\n\nCreate a new map style\n\nTo create a new map style, follow the instructions in [Create and use map\nstyles](/maps/documentation/ios-sdk/cloud-customization/map-styles) to create\nthe style. Once complete associate the style with the newly created map ID.\n\nUpdate your map initialization code\n\nThis step requires a map ID be associated with a style with one or more feature\nlayers enabled. To verify your map ID is set up correctly in\nCloud console, review how it is configured under [Maps\nManagement](https://console.cloud.google.com/google/maps-apis/studio/maps). \n\nSwift \n\n```swift\n// A map ID using a style with one or more feature layers enabled\n\nlet mapID = GMSMapID(identifier: \"YOUR_MAP_ID\")\nlet mapView = GMSMapView(frame: .zero, mapID: mapID, camera: GMSCameraPosition(latitude: 40, longitude: -80, zoom: 7))\n```\n\nObjective-C \n\n```objective-c\n// A map ID using a style with one or more feature layers enabled\n\nGMSMapID *mapID = [GMSMapID mapIDWithIdentifier:@\"MAP_ID\"];\nGMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero mapID:mapID camera:[GMSCameraPosition cameraWithLatitude:40 longitude:-80 zoom:7]];\n```\n| **Important:** You cannot use a demo map ID because a map ID with the linked map style is needed to load the dataset to the client.\n\nCheck map capabilities\n\nData-driven styling for datasets requires capabilities which are enabled in the\nGoogle Cloud console, and associated with a map ID. Because map IDs are subject to\nchange, you can call\n[`mapView.mapCapabilities`](/maps/documentation/ios-sdk/reference/objc/Classes/GMSMapView#mapcapabilities)\non a\n[`GMSMapView`](/maps/documentation/ios-sdk/reference/objc/Classes/GMSMapView)\nto verify whether a certain capability (for example data-driven styling) is\navailable prior to calling it.\n\nYou can also detect changes in map capabilities by subscribing to\n[`GMSViewDelegate`](/maps/documentation/ios-sdk/reference/objc/Protocols/GMSMapViewDelegate).\nThis example shows how to use the protocol to check for data-driven styling\nrequirements. \n\nSwift \n\n```swift\nclass SampleViewController: UIViewController {\n\n private lazy var mapView: GMSMapView = GMSMapView(frame: .zero, mapID: GMSMapID(identifier: \"YOUR_MAP_ID\"), camera: GMSCameraPosition(latitude: 40, longitude: -80, zoom: 7))\n\n override func loadView() {\n self.view = mapView\n mapView.delegate = self\n }\n}\n\nextension SampleViewController: GMSMapViewDelegate {\n func mapView(_ mapView: GMSMapView, didChange mapCapabilities: GMSMapCapabilityFlags) {\n if (!mapCapabilities.contains(.dataDrivenStyling)) {\n // Data-driven styling is *not* available, add a fallback.\n // Existing feature layers are also unavailable.\n }\n }\n}\n```\n\nObjective-C \n\n```objective-c\n@interface SampleViewController: UIViewController \u003cGMSMapViewDelegate\u003e\n@end\n\n@implementation SampleViewController\n- (void)loadView {\n GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero mapID:[GMSMapID mapIDWithIdentifier:@\"MAP_ID\"] camera:[GMSCameraPosition cameraWithLatitude:40 longitude:-80 zoom:7]];\n mapView.delegete = self;\n self.view = mapView;\n}\n\n- (void)mapView:(GMSMapView *)mapView didChangeMapCapabilities:(GMSMapCapabilityFlags)mapCapabilities {\n if (!(mapCapabilities & GMSMapCapabilityFlagsDataDrivenStyling)) {\n // Data-driven styling is *not* available, add a fallback.\n // Existing feature layers are also unavailable.\n }\n}\n@end\n```\n\nNext steps\n\n- [Create and manage a dataset](/maps/documentation/ios-sdk/dds-datasets/create-dataset)"]]