Kho lưu trữ Maps SDK cho iOS trên GitHub chứa các mẫu minh hoạ cách sử dụng Maps SDK cho iOS trong ứng dụng iOS của bạn. Ứng dụng mẫu này chứa mã cho các điểm nổi bật về tính năng của SDK và có thể được dùng làm điểm bắt đầu cho ứng dụng của bạn.
Danh sách mẫu Maps SDK dành cho iOS
Các hoạt động mẫu chính trong ứng dụng minh hoạ được liệt kê bên dưới và được sao chép trên các trang được liên kết để bạn tham khảo nhanh. Hãy xem thư mục mẫu trên GitHub để biết danh sách đầy đủ các mẫu hiện có.
Ứng dụng mẫu Maps SDK cho iOS có sẵn dưới dạng tệp lưu trữ có thể tải xuống trên GitHub. Hãy làm theo các bước sau để cài đặt và dùng thử ứng dụng mẫu Maps SDK cho iOS.
Chạy git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git để sao chép kho lưu trữ mẫu vào một thư mục cục bộ.
Mở cửa sổ dòng lệnh, chuyển đến thư mục mà bạn đã sao chép các tệp mẫu và đi sâu vào thư mục GoogleMaps:
Swift
cd maps-sdk-for-ios-samples-main/GoogleMaps-Swift pod installopen GoogleMapsSwiftDemos.xcworkspace
Objective-C
cd maps-sdk-for-ios-samples-main/GoogleMaps pod installopen GoogleMapsDemos.xcworkspace
Trong Xcode, hãy nhấn nút biên dịch để tạo ứng dụng bằng lược đồ hiện tại. Bản dựng sẽ tạo ra một lỗi, nhắc bạn nhập khoá API trong tệp SDKConstants.swift cho Swift hoặc tệp SDKDemoAPIKey.h cho Objective-C.
Chỉnh sửa tệp SDKConstants.swift cho Swift hoặc tệp SDKDemoAPIKey.h cho Objective-C rồi dán khoá API vào định nghĩa của hằng số apiKey hoặc kAPIKey. Ví dụ:
Swift
staticletapiKey="YOUR_API_KEY"
Objective-C
staticNSString*constkAPIKey=@"YOUR_API_KEY";
Trong tệp SDKConstants.swift (Swift) hoặc tệp SDKDemoAPIKey.h (Objective-C), hãy xoá dòng sau vì dòng này dùng để đăng ký vấn đề do người dùng xác định:
[[["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\u003eThe Maps SDK for iOS provides a sample app with code examples showcasing key features, serving as a starting point for your iOS app development.\u003c/p\u003e\n"],["\u003cp\u003eYou can find a comprehensive list of samples in the GitHub repository and easily access specific feature implementations through provided links.\u003c/p\u003e\n"],["\u003cp\u003eTo run the full sample app locally, clone the repository, install dependencies using CocoaPods, and build the project in Xcode after configuring your API key.\u003c/p\u003e\n"],["\u003cp\u003eTroubleshooting steps are available if you encounter issues like not seeing a map after successfully running the app, guiding you to add your API key correctly.\u003c/p\u003e\n"]]],["The provided content focuses on using the Maps SDK for iOS. Key actions include cloning the sample app repository from GitHub, building the app in Xcode, and integrating an API key. The API key is obtained from the Google Cloud console. After entering the API key, you can build and run the project. You then can interact with the various features demonstrated, like displaying a basic map, adding markers, drawing shapes, enabling location, and handling events. Finally, the content mentions the necessity to grant the app location access.\n"],null,["Select platform: [Android](/maps/documentation/android-sdk/examples \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/examples \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/examples \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nThe Maps SDK for iOS repository on\n[GitHub](https://github.com/googlemaps-samples/maps-sdk-for-ios-samples/tree/main/GoogleMaps)\ncontains samples illustrating the use of the Maps SDK for iOS\nin your iOS app. The sample app contains code for feature highlights of the SDK,\nand can be used starting point for your app.\n| **Troubleshooting:** If the sample app runs successfully but you don't see a map, check that you've added your API key to the app's manifest file, as described in [Using API Keys](/maps/documentation/ios-sdk/get-api-key).\n\nMaps SDK for iOS samples list\n\nKey sample activities from the demo app are listed below and reproduced at the\nlinked pages for quick reference. See the GitHub [samples\nfolder](https://github.com/googlemaps-samples/maps-sdk-for-ios-samples/tree/main/GoogleMaps/GoogleMapsDemos/Samples)\nfor the full list of available samples.\n\n- [Display a basic map](/maps/documentation/ios-sdk/examples/basic-map)\n- [Display an info window for a marker](/maps/documentation/ios-sdk/examples/info-window-marker)\n- [Add a marker to a map](/maps/documentation/ios-sdk/examples/add-marker)\n- [Reverse geocode a location](/maps/documentation/ios-sdk/examples/reverse-geocode)\n- [Enable and disable map gestures](/maps/documentation/ios-sdk/examples/enable-disable-gestures)\n- [Handle marker events](/maps/documentation/ios-sdk/examples/handle-marker-events)\n- [Enable the My Location Button](/maps/documentation/ios-sdk/examples/enable-my-location)\n- [Draw polygons on a map](/maps/documentation/ios-sdk/examples/draw-polygons)\n- [Draw polylines on a map](/maps/documentation/ios-sdk/examples/draw-polylines)\n\nRun the full sample app locally\n\nThe Maps SDK for iOS sample app is available as a\n[download archive](https://github.com/googlemaps-samples/maps-sdk-for-ios-samples/archive/main.zip)\nfrom [GitHub](https://github.com/googlemaps-samples/maps-sdk-for-ios-samples/tree/main/GoogleMaps).\nFollow these steps to install and try the Maps SDK for iOS sample app.\n\n1. Run `git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git` to clone the samples repository into a local directory.\n2. Open a terminal window, navigate to the directory where you cloned the sample files, and\n drill down into the GoogleMaps directory:\n\n Swift \n\n cd maps-sdk-for-ios-samples-main/GoogleMaps-Swift\n pod install\n open GoogleMapsSwiftDemos.xcworkspace\n\n Objective-C \n\n cd maps-sdk-for-ios-samples-main/GoogleMaps\n pod install\n open GoogleMapsDemos.xcworkspace\n\n3. In Xcode, press the compile button to [build the app](https://developer.apple.com/documentation/xcode/building-and-running-an-app) with the current scheme. The build produces an error, prompting you to enter your API key in the `SDKConstants.swift` file for Swift or`SDKDemoAPIKey.h` file for Objective-C.\n4. [Get an API key](/maps/documentation/ios-sdk/get-api-key) from your project with the [Maps SDK for iOS enabled](/maps/documentation/ios-sdk/cloud-setup#enabling-apis).\n5. Edit the `SDKConstants.swift` file for Swift or`SDKDemoAPIKey.h` file for Objective-C and paste your API key into the definition of either the `apiKey` or `kAPIKey` constant. For example: \n\n Swift \n\n ```swift\n static let apiKey = \"YOUR_API_KEY\"\n ```\n\n Objective-C \n\n ```objective-c\n static NSString *const kAPIKey = @\"YOUR_API_KEY\";\n ```\n6. In the `SDKConstants.swift` file (Swift) or`SDKDemoAPIKey.h` file (Objective-C), remove the following line, because it's used to register the user-defined issue: \n\n Swift \n\n ```swift\n #error (Register for API Key and insert here. Then delete this line.)\n ```\n\n Objective-C \n\n ```objective-c\n #error Register for API Key and insert here.\n ```\n7. Build and run the project. The iOS simulator window appears, showing a list of **Maps SDK Demos**.\n8. Choose one of the options displayed, to experiment with a feature of the Maps SDK for iOS.\n9. If prompted to allow GoogleMapsDemos to access your location, choose **Allow**."]]