添加带标记的地图

本教程介绍了如何向 iOS 设备添加带标记的简单 Google 地图 应用。适合掌握了 Swift 或 Swift 或 Objective-C 以及 Xcode 的一般知识。有关学习本课程的高级指南, 创建地图,请阅读开发者的指南。

您将按照本教程创建以下地图。标记所在位置为 澳大利亚悉尼。

显示悉尼上方带有标记的地图的屏幕截图

获取代码

克隆或下载 GitHub 上的 Google 地图 iOS 示例代码库

或者,点击以下按钮下载源代码:

请告诉我代码

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 camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
        let mapView = GMSMapView.map(withFrame: self.view.frame, camera: camera)
        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.
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                          longitude:151.20
                                                               zoom:6];
  GMSMapView *mapView = [GMSMapView mapWithFrame:self.view.frame camera:camera];
  mapView.myLocationEnabled = YES;
  [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

      

开始使用

Swift Package Manager

您可以使用 Swift Package Manager 安装 Maps SDK for iOS。

  1. 确保您已移除所有现有的 Maps SDK for iOS 依赖项。
  2. 打开终端窗口并导航至 tutorials/map-with-marker 目录。
  3. 确保您的 Xcode 工作区已关闭,然后运行以下命令:
    sudo gem install cocoapods-deintegrate cocoapods-clean
    pod deintegrate
    pod cache clean --all
    rm Podfile
    rm map-with-marker.xcworkspace
  4. 打开 Xcode 项目并删除 Podfile。
  5. 转到文件 >添加软件包依赖项
  6. 输入 https://github.com/googlemaps/ios-maps-sdk 作为网址,按 Enter 键提取软件包,然后点击添加软件包
  7. 您可能需要使用文件 >文件包 >重置软件包缓存

使用 CocoaPods

  1. 下载并安装 Xcode 15.0 版或更高版本。
  2. 如果您还没有 CocoaPods, 在 macOS 上从终端运行以下命令进行安装:
    sudo gem install cocoapods
  3. 进入 tutorials/map-with-marker 目录。
  4. 运行 pod install 命令。这将安装 Podfile 中指定的 Maps SDK 以及所有依赖项。
  5. 运行 pod outdated,将已安装的 Pod 版本与任何新更新进行比较。如果检测到新版本,请运行 pod update 以更新 Podfile 并安装最新的 SDK。如需了解详情,请参阅 CocoaPods 指南
  6. 打开(双击)项目的 map-with-marker.xcworkspace 以在 Xcode 中打开它。您必须使用 .xcworkspace 文件打开项目。

获取 API 密钥并启用必要的 API

要完成本教程,您需要一个已获得授权的 Google API 密钥 使用 Maps SDK for iOS。点击以下按钮即可 密钥并激活 API。

开始使用

有关详情,请参阅 获取 API 密钥

为应用添加API密钥

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

  1. 请注意,该文件中添加了以下 import 语句:
    import GoogleMaps
  2. 修改 application(_:didFinishLaunchingWithOptions:) 中的以下行 方法,将 YOUR_API_KEY 替换为您的 API 密钥:
    GMSServices.provideAPIKey("YOUR_API_KEY")

构建并运行应用

  1. 将 iOS 设备连接到您的计算机,或者选择一个 模拟器 从 Xcode scheme 菜单中。
  2. 如果您使用的是设备,请确保已启用位置服务。 如果您使用的是模拟器,请从地图项中选择一个位置 菜单。
  3. 在 Xcode 中,点击 Product/Run(产品/运行)菜单选项(或点击 按钮图标)。
    • Xcode 构建应用,然后在设备或模拟器上运行该应用。
    • 您将看到一张带有标记的地图,这张地图以位于澳大利亚东海岸的悉尼为中心,与本页上的图像类似。

问题排查:

  • 如果您没有看到地图,请检查您是否已获取 API 密钥并添加 如前所述。查看 Xcode 的调试控制台,用于获取有关 API 密钥的错误消息。
  • 如果您通过 iOS 软件包标识符限制了 API 密钥,请修改 键添加应用的软件包标识符: com.google.examples.map-with-marker
  • 确保 Wi-Fi 或 GPS 连接信号良好。
  • 使用 Xcode 调试工具 查看日志和调试应用。

了解代码

  1. 创建地图并在 viewDidLoad() 中将其设置为视图。

    Swift

    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    view = mapView
          

    Objective-C

    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                            longitude:151.20
                                                                 zoom:6.0];
    GMSMapView *mapView = [[GMSMapView alloc] initWithFrame: CGRectZero camera:camera];
    self.view = mapView;
          
  2. viewDidLoad() 中向地图添加标记。

    Swift

    // 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

    // 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;
          

默认情况下,Maps SDK for iOS 会显示 窗口。无需为监听器添加点击监听器 标记即可。

恭喜!您已构建了一个 iOS 应用,该应用显示 标记来指示特定的位置。您还学习了如何使用 Maps SDK for iOS

后续步骤

详细了解地图对象以及您 标记的功能。