Các bản đồ được biểu thị trong API bằng lớp GMSMapView, một lớp con của UIView. Bản đồ là đối tượng quan trọng nhất trong Maps SDK cho iOS và cung cấp các phương thức cần thiết để thêm, xoá và quản lý các đối tượng khác như điểm đánh dấu và đường nhiều đoạn.
Giới thiệu
Maps SDK dành cho iOS cho phép bạn hiển thị bản đồ Google trong ứng dụng iOS của mình. Những bản đồ này có giao diện giống như bản đồ mà bạn thấy trong ứng dụng Google Maps dành cho iOS và SDK này cung cấp nhiều tính năng tương tự.
Ngoài chức năng lập bản đồ, API này còn hỗ trợ nhiều hoạt động tương tác phù hợp với mô hình giao diện người dùng iOS. Ví dụ: bạn có thể thiết lập các hoạt động tương tác với bản đồ bằng cách xác định các đối tượng phản hồi phản ứng với cử chỉ của người dùng, chẳng hạn như nhấn và nhấn đúp.
Lớp khoá khi làm việc với đối tượng Bản đồ là lớp GMSMapView.
GMSMapView tự động xử lý các thao tác sau:
Đang kết nối với dịch vụ Google Maps.
Đang tải các ô bản đồ xuống.
Hiển thị các ô trên màn hình thiết bị.
Hiển thị nhiều chế độ kiểm soát như xoay và thu phóng.
Phản hồi các cử chỉ kéo và thu phóng bằng cách di chuyển bản đồ và phóng to hoặc thu nhỏ.
Phản hồi cử chỉ bằng hai ngón tay bằng cách nghiêng góc nhìn của bản đồ.
Ngoài các thao tác tự động này, bạn có thể kiểm soát hành vi và giao diện của bản đồ thông qua các thuộc tính và phương thức do lớp GMSMapView hiển thị. Sử dụng GMSMapView để thêm và xoá điểm đánh dấu, lớp phủ mặt đất và đường nhiều đoạn, thay đổi loại bản đồ được hiển thị và kiểm soát nội dung xuất hiện trên bản đồ thông qua lớp GMSCameraPosition.
Xây dựng bản đồ bằng SwiftUI
SwiftUI cung cấp một cách khác để tạo giao diện người dùng bằng phương pháp khai báo. Bạn cho SwiftUI biết giao diện bạn muốn cho khung hiển thị của mình cùng với tất cả các trạng thái khác nhau của khung hiển thị đó, còn hệ thống sẽ làm phần còn lại. SwiftUI xử lý việc cập nhật khung hiển thị bất cứ khi nào trạng thái cơ bản thay đổi do một sự kiện hoặc hành động của người dùng.
Maps SDK dành cho iOS được xây dựng dựa trên UIKit và không cung cấp chế độ xem tương thích với SwiftUI. Để thêm bản đồ trong SwiftUI, bạn cần tuân thủ UIViewRepresentable hoặc UIViewControllerRepresentable. Để tìm hiểu thêm, hãy xem Lớp học lập trình thêm bản đồ vào ứng dụng iOS bằng SwiftUI.
Thêm bản đồ
Các bước cơ bản để thêm bản đồ là:
Để tải SDK, lấy khoá API và thêm các khung hình bắt buộc, hãy làm theo các bước trong:
Trong AppDelegate, hãy cung cấp khoá API cho phương thức lớp provideAPIKey: trên GMSServices.
Tạo hoặc cập nhật ViewController. Nếu bản đồ xuất hiện khi bộ điều khiển khung hiển thị này hiển thị, hãy nhớ tạo bản đồ trong phương thức viewDidLoad.
Khi khởi tạo chế độ xem bản đồ, hãy đặt các lựa chọn cấu hình bằng GMSMapViewOptions. Các thuộc tính bao gồm frame, camera, mapID,backgroundColor hoặc screen.
Đặt thuộc tính các lựa chọn về bản đồcamera bằng đối tượng GMSCameraPosition. Tham số này chỉ định tâm và mức thu phóng của bản đồ.
Tạo và khởi tạo một lớp GMSMapView bằng phương thức GMSMapViewoptions:. Nếu bản đồ này chỉ được dùng làm khung hiển thị duy nhất của trình điều khiển khung hiển thị, thì bạn có thể dùng giá trị mặc định CGRectZero của lựa chọn bản đồ frame làm khung hiển thị frame – bản đồ sẽ tự động đổi kích thước.
Đặt đối tượng GMSMapView làm khung hiển thị của trình điều khiển khung hiển thị. Ví dụ: self.view = mapView;.
Ví dụ dưới đây thêm một bản đồ vào ứng dụng, với tâm là trung tâm Singapore.
[[["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-09-05 UTC."],[[["\u003cp\u003eThe Maps SDK for iOS allows you to integrate Google Maps into your iOS application, offering features and interactions similar to the Google Maps app.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eGMSMapView\u003c/code\u003e is the primary class for working with maps, handling tasks like connecting to Google Maps, downloading tiles, and responding to user gestures.\u003c/p\u003e\n"],["\u003cp\u003eTo add a map, obtain an API key, configure the \u003ccode\u003eGMSMapView\u003c/code\u003e object with desired settings like camera position and frame, and then set it as the view controller's view.\u003c/p\u003e\n"],["\u003cp\u003eSwiftUI integration requires using \u003ccode\u003eUIViewRepresentable\u003c/code\u003e or \u003ccode\u003eUIViewControllerRepresentable\u003c/code\u003e due to the SDK being built on \u003ccode\u003eUIKit\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eAfter adding the map, you can further configure its settings to customize its appearance and behavior.\u003c/p\u003e\n"]]],[],null,["\u003cbr /\u003e\n\nSelect platform: [Android](/maps/documentation/android-sdk/map \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/map \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/maptypes \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\n| **Note:** This document describes how to add a Google Map to an iOS application. If you would like to know how to launch the Google Maps standalone application, refer to the [URL Scheme](/maps/documentation/ios-sdk/urlscheme) documentation.\n\nMaps are represented in the API by the [`GMSMapView`](/maps/documentation/ios-sdk/reference/objc/Classes/GMSMapView) class, a\nsubclass of `UIView`. The map is the most significant object in the\nMaps SDK for iOS, and provides necessary methods for adding, removing\nand managing other objects such as markers and polylines.\n\nIntroduction\n\nThe Maps SDK for iOS lets you to display a Google map in\nyour iOS application. These maps have the same appearance as the maps you see in\nthe [Google Maps](https://itunes.apple.com/app/id585027354)\niOS app, and the SDK exposes many of the same features.\n\nIn addition to mapping functionality, the API also supports a range of\ninteractions that are consistent with the iOS UI model. For example, you can set\nup interactions with a map by defining responders that react to user gestures,\nsuch as tap and double-tap.\n\nThe key class when working with a Map object is the `GMSMapView` class.\n`GMSMapView` handles the following operations automatically:\n\n- Connecting to the Google Maps service.\n- Downloading map tiles.\n- Displaying tiles on the device screen.\n- Displaying various controls such as pan and zoom.\n- Responding to pan and zoom gestures by moving the map and zooming in or out.\n - Responding to two finger gestures by tilting the viewing angle of the map.\n\nIn addition to these automatic operations, you can control the behavior and\nappearance of the map through the properties and methods exposed by the\n`GMSMapView` class. Use `GMSMapView` to add and remove markers, ground\noverlays and polylines, change the type of map that is displayed, and control\nwhat is shown on the map through the [`GMSCameraPosition`](/maps/documentation/ios-sdk/reference/objc/Classes/GMSCameraPosition)\nclass.\n\nBuild Maps with SwiftUI\n\nSwiftUI offers an additional way to create UI using a declarative approach. You\ntell SwiftUI how you want your view to look along with all the different states\nfor it, and the system does the rest. SwiftUI handles updating the view whenever\nthe underlying state changes due to an event or user action.\n\nMaps SDK for iOS is built on top of `UIKit` and doesn't provide a\nSwiftUI-compatible view. Adding maps in SwiftUI requires conforming to either\n`UIViewRepresentable` or `UIViewControllerRepresentable`. To learn more, see the\nCodelab [adding a map to your iOS app with\nSwiftUI](/codelabs/maps-platform/maps-platform-ios-swiftui#0).\n\nAdd a map\n\nThe basic steps for adding a map are:\n\n1. To get the SDK, obtain an API key, and add the required frameworks, follow\n the steps in:\n\n 1. [Set Up in the Google Cloud console](/maps/documentation/ios-sdk/cloud-setup)\n\n 2. [Use an API key](/maps/documentation/ios-sdk/get-api-key)\n\n 3. [Set up an Xcode Project](/maps/documentation/ios-sdk/config)\n\n 4. In your `AppDelegate`, provide your API key to the `provideAPIKey:`\n class method on `GMSServices`.\n\n 5. Create or update a `ViewController`. If the map is displayed when this\n view controller becomes visible, be sure to create it within the\n `viewDidLoad` method.\n\n 6. When initializing your map view, set configuration options with\n `GMSMapViewOptions`. Properties include the `frame`, `camera`,\n `mapID`,`backgroundColor` or `screen`.\n\n 7. Set your [map options](/maps/documentation/ios-sdk/configure-map#map_options) `camera` property with a\n `GMSCameraPosition` object. This specifies the center and zoom level of\n the map.\n\n 8. Create and instantiate a `GMSMapView` class using the `GMSMapView`\n `options:` method. If this map is to be used as the view controller's\n only view, the map option `frame` default value of `CGRectZero` can be\n used as the view `frame` --- the map is resized automatically.\n\n 9. Set the `GMSMapView` object as the view controller's view. For example,\n `self.view = mapView;`.\n\nThe below example adds a map, centered at downtown Singapore, to an app. \n\nSwift \n\n```swift\nimport GoogleMaps\n\nclass MapObjects : UIViewController {\n override func viewDidLoad() {\n super.viewDidLoad()\n\n let options = GMSMapViewOptions()\n options.camera = GMSCameraPosition(latitude: 1.285, longitude: 103.848, zoom: 12)\n options.frame = self.view.bounds;\n\n let mapView = GMSMapView(options:options)\n self.view = mapView\n }\n}\n```\n\nObjective-C \n\n```objective-c\n- (void)viewDidLoad {\n [super viewDidLoad];\n\n GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init];\n options.camera = [GMSCameraPosition cameraWithLatitude:1.285\n longitude:103.848\n zoom:12];\n options.frame = self.view.bounds;\n\n GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options];\n self.view = mapView;\n}\n```\n\nOnce you've followed these steps, you may further configure the `GMSMapView`\nobject.\n\nWhat's next\n\nAfter you complete these steps, you can [configure the map\nsettings](/maps/documentation/ios-sdk/configure-map)."]]