添加带标记的地图

本教程介绍了如何向 iOS 应用中添加带标记的简单 Google 地图。它适合具备 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/mapwithmarker 目录。
  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. 依次转到 File > Add Package Dependencies
  6. 输入 https://github.com/googlemaps/ios-maps-sdk 作为网址,按 Enter 键拉取软件包,然后点击 Add Package(添加软件包)。
  7. 您可能需要依次转到文件 > 软件包 > 重置软件包缓存,重置软件包缓存。

使用 CocoaPods

  1. 下载并安装 Xcode 版本 14.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

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

开始使用

如需了解详情,请参阅获取 API 密钥

为应用添加API密钥

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

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

构建并运行应用

  1. 将 iOS 设备连接到您的计算机,或从 Xcode 方案菜单中选择模拟器
  2. 如果您使用的是设备,请确保已启用位置信息服务。 如果您使用的是模拟器,请从功能菜单中选择一个营业地点。
  3. 在 Xcode 中,点击 Product/Run(产品/运行)菜单选项(或 Play 按钮图标)。
    • 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 应用,该应用显示一张 Google 地图,地图上使用标记来指示特定位置。您还学习了如何使用 Maps SDK for iOS

后续步骤

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