添加带标记的地图
本教程介绍了如何向 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。
- 确保您已移除所有现有的 Maps SDK for iOS 依赖项。
- 打开终端窗口并导航至
tutorials/map-with-marker
目录。 -
确保您的 Xcode 工作区已关闭,然后运行以下命令:
sudo gem install cocoapods-deintegrate cocoapods-clean pod deintegrate pod cache clean --all rm Podfile rm map-with-marker.xcworkspace
- 打开 Xcode 项目并删除 Podfile。
- 依次点击 File > Add Package Dependencies。
- 输入 https://github.com/googlemaps/ios-maps-sdk 作为网址,按 Enter 键提取软件包,然后点击添加软件包。
- 您可能需要通过依次选择 File > Packages > Reset Package Cache 来重置软件包缓存。
使用 CocoaPods
- 下载并安装 Xcode 15.0 版或更高版本。
- 如果您还没有 CocoaPods,请在 macOS 上从终端运行以下命令进行安装:
sudo gem install cocoapods
- 进入
tutorials/map-with-marker
目录。 - 运行
pod install
命令。这将安装Podfile
中指定的 Maps SDK 以及所有依赖项。 - 运行
pod outdated
,将已安装的 Pod 版本与任何新更新进行比较。如果检测到新版本,请运行pod update
以更新Podfile
并安装最新的 SDK。如需了解详情,请参阅 CocoaPods 指南。 - 打开(双击)项目的 map-with-marker.xcworkspace 文件以在 Xcode 中打开它。您必须使用
.xcworkspace
文件打开项目。
获取 API 密钥并启用必要的 API
如需完成本教程,您需要一个已获得 Maps SDK for iOS 使用授权的 Google API 密钥。点击以下按钮获取密钥并激活 API。
开始使用如需了解详情,请参阅获取 API 密钥。
为应用添加API密钥
按照以下方法向 AppDelegate.swift
添加 API 密钥:
- 请注意,此文件中添加了以下 import 语句:
import GoogleMaps
- 在
application(_:didFinishLaunchingWithOptions:)
方法中修改以下行,将 YOUR_API_KEY 替换为您的 API 密钥:GMSServices.provideAPIKey("YOUR_API_KEY")
构建并运行应用
- 将 iOS 设备连接到您的计算机,或从 Xcode 架构菜单中选择一个模拟器。
- 如果您使用的是设备,请确保已启用位置信息服务。 如果您使用的是模拟器,请从功能菜单中选择一个位置。
- 在 Xcode 中,点击 Product/Run 菜单选项(或 play 按钮图标)。
- Xcode 会构建应用,然后在设备或模拟器上运行该应用。
- 您将看到一张带有标记的地图,这张地图以位于澳大利亚东海岸的悉尼为中心,与本页上的图像类似。
问题排查:
- 如果您没有看到地图,请检查您是否已如前所述获取 API 密钥并将其添加到该应用。请查看 Xcode 的调试控制台,看看有没有关于 API 密钥的错误消息。
- 如果您通过 iOS 软件包标识符限制了 API 密钥,请修改密钥以添加应用的软件包标识符:
com.google.examples.map-with-marker
。 - 确保 Wi-Fi 或 GPS 连接信号良好。
- 使用 Xcode 调试工具查看日志并调试应用。
理解代码
- 创建地图并在
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;
- 在
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。