设置 Xcode 项目

启用结算功能并创建 API 密钥后,您就可以设置 Xcode 了 一个项目。

每个版本都有相应的版本说明 发布。

第 1 步:安装所需的软件

若要使用 Maps SDK for iOS 构建项目,您必须下载并安装:

  • Xcode 版本 15.0 或更高版本

第 2 步:创建 Xcode 项目并安装 Maps SDK for iOS

Swift Package Manager

可以通过 安装 Maps SDK for iOS Swift Package Manager 的相关文档。要添加 SDK,请确保您已 移除了所有现有的 Maps SDK for iOS 依赖项。

如需将 SDK 添加到新项目或现有项目,请按以下步骤操作:

  1. 打开 Xcode projectworkspace,然后转到 File >添加软件包依赖项
  2. 输入 https://github.com/googlemaps/ios-maps-sdk 作为网址,然后按 Enter 键 以提取软件包,然后点击“Add Package”(添加软件包)。
  3. 如需安装特定的 version,请将 Dependency Rule 字段设置为以下其中一项: 基于版本的选项对于新项目,我们建议您指定最新版本和 使用“确切版本”选项。完成后,点击“添加软件包”。
  4. Choose Package Products(选择软件包产品)窗口中,验证 GoogleMaps(对于 9.0.0 之前的版本,请验证 GoogleMapsGoogleMapsBaseGoogleMapsCore)将添加到 您指定的main目标。完成后,点击“添加软件包”。
  5. 如需验证安装,请前往目标的 General 窗格。 在框架、库和嵌入式内容中,您应该会看到已安装的软件包。 您还可以查看“Package Dependencies”“Project Navigator”部分 以验证软件包及其版本。

如需更新现有项目的 package,请按以下步骤操作:

  1. 如果您要从 9.0.0 之前的版本升级,就必须移除 以下依赖项:GoogleMapsBaseGoogleMapsCoreGoogleMapsM4B。请勿移除 GoogleMaps。有关详情,请参阅 9.0.0 版版本说明

    在您的 Xcode 项目配置设置中,找到 Frameworks, Libraries, 和嵌入式内容。使用减号(-) 移除以下框架:

    • GoogleMapsBase(仅适用于从 9.0.0 之前的版本进行的升级)
    • GoogleMapsCore(仅适用于从 9.0.0 之前的版本进行的升级)
    • GoogleMapsM4B(仅适用于从 9.0.0 之前的版本进行的升级)
  2. 在 Xcode 中,转到“File”(文件)>文件包 >更新到最新的软件包版本”。
  3. 如需验证安装,请前往 Project NavigatorPackage Dependencies 部分 以验证软件包及其版本。
。 <ph type="x-smartling-placeholder">

如需移除使用以下代码添加的现有 Maps SDK for iOS 依赖项 CocoaPods,请按以下步骤操作:

  1. 关闭 Xcode 工作区。打开终端并执行以下命令:
    sudo gem install cocoapods-deintegrate cocoapods-clean 
    pod deintegrate 
    pod cache clean --all
  2. 移除 PodfilePodfile.resolved 和 Xcode workspace(如果您不将其用于除 CocoaPods 之外的任何其他目的)。

如需移除手动安装的现有 Maps SDK for iOS,请按以下步骤操作:
  1. 在您的 Xcode 项目配置设置中,找到 Frameworks, Libraries, 和嵌入式内容。使用减号(-) 移除以下框架:
    • GoogleMaps
    • GoogleMapsBase(仅适用于安装) 版本 9.0.0 以下)
    • GoogleMapsCore(仅适用于安装) 版本 9.0.0 以下)
    • GoogleMapsM4B(仅适用于安装) 版本 9.0.0 以下)
  2. 从 Xcode 项目的顶级目录中,移除 GoogleMaps 软件包。

CocoaPods

Maps SDK for iOS 以 CocoaPods pod 的形式提供。CocoaPods 是一种用于 Swift 和 Objective-C Cocoa 项目的开源依赖项管理器。

如果您尚未安装 CocoaPods 工具,请在 macOS 上从终端运行以下命令进行安装。如需了解详情,请参阅 CocoaPods 入门指南

sudo gem install cocoapods

为 Maps SDK for iOS 创建一个 Podfile,并使用它来安装 API 及其依赖项:

  1. 如果您还没有 Xcode 项目,请立即创建一个并将其保存到 本地机器如果您是 iOS 开发新手:
    1. 创建新项目。
    2. 选择 iOS >App 模板。
    3. 在项目选项屏幕上:
      1. 输入项目名称
      2. 记录 Bundle identifier 字段的值。 您可以使用该值在下方限制您的 API 密钥。
      3. 将项目的 Interface 设置为 Storyboard
      4. 语言设置为 SwiftObjective-C
  2. 在您的项目目录中创建一个名为 Podfile 的文件。此文件可定义您项目的依赖项。
  3. 修改 Podfile 并添加您的依赖项及其依赖项 versions. 这是一个示例,其中包含创建 Maps SDK for iOS:
    source 'https://github.com/CocoaPods/Specs.git'
    
    platform :ios, '15.0'
    
    target 'YOUR_APPLICATION_TARGET_NAME_HERE' do
      pod 'GoogleMaps', '8.4.0'
    end
    
    确保定期运行 pod outdated,以检测何时有较新的 以确保您始终使用的是最新版本。如有必要,请升级到最新版本
  4. 保存 Podfile
  5. 打开终端并转到包含 Podfile 的目录:

    cd <path-to-project>
  6. 运行 pod install 命令。此操作会安装 Podfile 中指定的 API 及其可能具有的任何依赖项。

    pod install
  7. 关闭 Xcode,然后打开(双击)您项目的 .xcworkspace 文件以启动 Xcode。从此刻开始,您必须使用 .xcworkspace 文件打开项目。

如需更新现有项目的 API,请按以下步骤操作:

  1. 打开终端,转到包含 Podfile 的项目目录。
  2. 运行 pod update 命令。这将更新所有 API Podfile 中指定的版本更新到最新版本。

手动安装

本指南介绍了如何将包含 Maps SDK for iOS 的 XCFrameworks 手动添加到您的项目中,以及如何在 Xcode 中配置您的构建设置。XCFramework 是一个二进制软件包,可在多个平台(包括使用 Apple 芯片的计算机)上使用。
  1. 下载以下 SDK 二进制文件和资源文件:
  2. 解压缩文件以访问 XCFrameworks 和资源。
  3. 如果您还没有 Xcode 项目,请立即创建一个并将其保存到 本地机器如果您是 iOS 开发新手:
    1. 创建新项目。
    2. 选择 iOS >App 模板。
    3. 在项目选项屏幕上:
      1. 输入项目名称
      2. 记录 Bundle identifier 字段的值。 您可以使用该值在下方限制您的 API 密钥。
      3. 将项目的 Interface 设置为 Storyboard
      4. 语言设置为 SwiftObjective-C
  4. 打开常规标签页。将以下 XCFrameworks 拖到您的项目中 (在框架、库和嵌入式内容下)。请确保 为每个 XCFramework 选择 Do Not Embed
    • GoogleMaps.xcframework
    • GoogleMapsBase.xcframework
    • GoogleMapsCore.xcframework
  5. GoogleMapsResources 中复制 GoogleMaps.bundle 您下载到 Xcode 项目的顶级目录中。请务必选择 出现提示时,将内容复制到目标群组的文件夹
  6. 从项目导航器中选择您的项目,然后选择 应用的目标
  7. 打开应用目标的 Build Phases 标签页。 在 Link Binary with Libraries 中,添加以下内容: 框架和库:
    • Accelerate.framework
    • Contacts.framework
    • CoreData.framework
    • CoreGraphics.framework
    • CoreImage.framework
    • CoreLocation.framework
    • CoreTelephony.framework
    • CoreText.framework
    • GLKit.framework
    • ImageIO.framework
    • libc++.tbd
    • libz.tbd
    • Metal.framework
    • OpenGLES.framework(仅适用于使用 OpenGL)
    • QuartzCore.framework
    • SystemConfiguration.framework
    • UIKit.framework
  8. 选择您的项目而不是特定目标,然后打开 Build Settings 标签页。在关联 - 常规 ->其他链接器 Flags 部分,将 -ObjC 添加到“Debug”和“发布”如果这些 设置不可见,请更改 Build Settings(构建设置)栏中的过滤条件 从基本改为全部

  9. 如需安装 Places SDK for iOS XCFramework,请参阅 开始使用 与 Places SDK for iOS 集成

第 3 步:检查 Apple 隐私清单文件

Apple 要求为 App Store 中的应用提供应用隐私权详细信息。如需了解最新动态和更多信息,请访问 Apple App Store 隐私权详情页面

Apple 隐私清单文件包含在 SDK 的资源包中。如需验证隐私清单文件是否包含在内并检查其内容,请创建应用的归档,并从归档中生成隐私报告

第 4 步:将您的 API 密钥添加到项目中

获取 API 密钥中,您需要 为您的应用生成了一个 API 密钥。现在,将该密钥添加到您的 Xcode 项目中。

在下例中,请将 YOUR_API_KEY 替换为您的 API 密钥。

Swift

按照以下方法向 AppDelegate.swift 添加 API 密钥:

  1. 添加以下 import 语句:
    import GoogleMaps
  2. 将以下内容添加到您的 application(_:didFinishLaunchingWithOptions:) 中 方法:
    GMSServices.provideAPIKey("YOUR_API_KEY")
  3. 如果您还使用 Places API,请再次添加密钥,如下所示:
    GMSPlacesClient.provideAPIKey("YOUR_API_KEY")

Objective-C

按照以下方法向 AppDelegate.m 添加 API 密钥:

  1. 添加以下 import 语句:
    @import GoogleMaps;
  2. 将以下内容添加到您的 application:didFinishLaunchingWithOptions: 中 方法:
    [GMSServices provideAPIKey:@"YOUR_API_KEY"];
  3. 如果您还使用 Places API,请再次添加密钥,如下所示:
    [GMSPlacesClient provideAPIKey:@"YOUR_API_KEY"];

第 5 步:添加地图

Swift

/*
 *   Copyright 2020 Google Inc. All rights reserved.
 *
 *
 *   Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 *   file except in compliance with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software distributed under
 *   the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
 *   ANY KIND, either express or implied. See the License for the specific language governing
 *   permissions and limitations under the License.
 */

import UIKit
import GoogleMaps

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        // Create a GMSCameraPosition that tells the map to display the
        // coordinate -33.86,151.20 at zoom level 6.

        let options = GMSMapViewOptions()
        options.camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
        options.frame = self.view.bounds

        let mapView = GMSMapView(options: options)
        self.view.addSubview(mapView)

        // Creates a marker in the center of the map.
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView
  }
}

Objective-C

/*
*   Copyright 2020 Google Inc. All rights reserved.
*
*
*   Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
*   file except in compliance with the License. You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software distributed under
*   the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
*   ANY KIND, either express or implied. See the License for the specific language governing
*   permissions and limitations under the License.
*/

#import "ViewController.h"
#import <GoogleMaps/GoogleMaps.h>

@interface ViewController()

@end

@implementation ViewController

-   (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  // Create a GMSCameraPosition that tells the map to display the
  // coordinate -33.86,151.20 at zoom level 6.
  GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init];
  options.camera = [GMSCameraPosition cameraWithLatitude:-33.8683
                                                        longitude:151.2086
                                                             zoom:6];
  options.frame = self.view.bounds;
  GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options];

  [self.view addSubview:mapView];

  // Creates a marker in the center of the map.
  GMSMarker *marker = [[GMSMarker alloc] init];
  marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
  marker.title = @"Sydney";
  marker.snippet = @"Australia";
  marker.map = mapView;
}

@end

第 5 步(可选):声明 API 使用的网址架构

从 iOS 9 和 Xcode 7 开始,应用可以声明其 打算打开哪个 activity,只需在应用的 Info.plist 文件中指定架构即可。通过 用户点击地图时,Maps SDK for iOS 会打开 Google 地图移动应用 在地图上显示 Google 徽标,因此您的应用可以声明相关网址 架构。

如需声明 Maps SDK for iOS 使用的网址架构,请将 以下几行代码添加到您的 Info.plist 中:

LSApplicationQueriesSchemes Google Chrome comgooglemaps

下面的屏幕截图显示了 Xcode 用户界面中的配置:

中的 LSApplicationQUERYSchemes 配置
Xcode

如果没有上述声明,当用户点按屏幕时可能会出现以下错误 地图上的 Google 徽标:

  • -canOpen网址: 无法运行网址“comgooglemaps://”- 错误:“此应用不是 可以查询 comgooglemaps 架构”
  • -canOpen网址: 无法运行网址:“googlechromes://”- 错误:“此应用不是 可以查询 googlechromes 协议”

为了消除这些错误,请将声明添加到 Info.plist 中。

后续步骤

现在,您已经有了 API 密钥和 Xcode 项目,接下来就可以创建和运行应用了。 适用于 iOS 的 Navigation SDK 提供了许多教程和示例应用,可帮助您 开始使用。如需了解详情,请参阅以下文档: