启用和停用地图手势

只有在该开关处于开启状态时,地图才会对缩放手势做出响应。

开始使用

您必须先配置开发环境,然后才能试用该示例代码。如需了解详情,请参阅 Maps SDK for iOS 代码示例

查看代码

Swift

import GoogleMaps import UIKit  class GestureControlViewController: UIViewController {   private let holderHeight: CGFloat = 60   private let zoomLabelInset: CGFloat = 16    private lazy var mapView: GMSMapView = {     let camera = GMSCameraPosition(latitude: -25.5605, longitude: 133.605097, zoom: 3)     return GMSMapView(frame: .zero, camera: camera)   }()   private lazy var zoomSwitch: UISwitch = UISwitch(frame: .zero)    override func viewDidLoad() {     super.viewDidLoad()      view.addSubview(mapView)      let holder = UIView(frame: .zero)     holder.backgroundColor = UIColor(white: 1, alpha: 0.8)     view.addSubview(holder)      let zoomLabel = UILabel(frame: .zero)     zoomLabel.text = "Zoom gestures"     zoomLabel.font = .boldSystemFont(ofSize: 18)     holder.addSubview(zoomLabel)      // Control zooming.     holder.addSubview(zoomSwitch)     zoomSwitch.addTarget(self, action: #selector(toggleZoom), for: .valueChanged)     zoomSwitch.isOn = true      [mapView, holder, zoomLabel, zoomSwitch].forEach({       $0.translatesAutoresizingMaskIntoConstraints = false     })     NSLayoutConstraint.activate([       mapView.leftAnchor.constraint(equalTo: view.leftAnchor),       mapView.rightAnchor.constraint(equalTo: view.rightAnchor),       mapView.topAnchor.constraint(equalTo: view.topAnchor),       mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor),       holder.heightAnchor.constraint(equalToConstant: holderHeight),       holder.centerXAnchor.constraint(equalTo: view.centerXAnchor),       holder.widthAnchor.constraint(equalTo: view.widthAnchor),       zoomLabel.leftAnchor.constraint(equalTo: holder.leftAnchor, constant: zoomLabelInset),       zoomLabel.centerYAnchor.constraint(equalTo: holder.centerYAnchor),       zoomSwitch.rightAnchor.constraint(equalTo: holder.rightAnchor, constant: -zoomLabelInset),       zoomSwitch.centerYAnchor.constraint(         equalTo: holder.centerYAnchor),     ])     NSLayoutConstraint.activate([       holder.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor)     ])   }    @objc func toggleZoom() {     mapView.settings.zoomGestures = zoomSwitch.isOn   }  }       

Objective-C

#import "GoogleMapsDemos/Samples/GestureControlViewController.h"  #import <GoogleMaps/GoogleMaps.h>  @implementation GestureControlViewController {   GMSMapView *_mapView;   UISwitch *_zoomSwitch; }  - (void)viewDidLoad {   [super viewDidLoad];   GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-25.5605                                                           longitude:133.605097                                                                zoom:3];    _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];   _mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;   _mapView.accessibilityIdentifier = @"gestureControlDemoMapView";    self.view = [[UIView alloc] initWithFrame:CGRectZero];   [self.view addSubview:_mapView];    UIView *holder = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 59)];   holder.autoresizingMask =       UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;   holder.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.8f];   [self.view addSubview:holder];    // Zoom label.   UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 16, 200, 29)];   label.text = @"Zooming?";   label.font = [UIFont boldSystemFontOfSize:18.0f];   label.textAlignment = NSTextAlignmentLeft;   label.backgroundColor = [UIColor clearColor];   label.layer.shadowColor = [[UIColor whiteColor] CGColor];   label.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);   label.layer.shadowOpacity = 1.0f;   label.layer.shadowRadius = 0.0f;   [holder addSubview:label];    // Control zooming.   _zoomSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(-90, 16, 0, 0)];   _zoomSwitch.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;   [_zoomSwitch addTarget:self                   action:@selector(didChangeZoomSwitch)         forControlEvents:UIControlEventValueChanged];   _zoomSwitch.on = YES;   [holder addSubview:_zoomSwitch]; }  - (void)didChangeZoomSwitch {   _mapView.settings.zoomGestures = _zoomSwitch.isOn; }  @end       

在本地运行完整示例应用

Maps SDK for iOS 示例应用以GitHub 上的下载归档文件的形式提供。请按照以下步骤安装并试用 Maps SDK for iOS 示例应用。

  1. 运行 git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git 以将示例代码库克隆到本地目录。
  2. 打开一个终端窗口,导航到您克隆示例文件的目录,然后展开 GoogleMaps 目录:

    Swift

    cd maps-sdk-for-ios-samples-main/GoogleMaps-Swift pod install open GoogleMapsSwiftDemos.xcworkspace

    Objective-C

    cd maps-sdk-for-ios-samples-main/GoogleMaps pod install open GoogleMapsDemos.xcworkspace
  3. 在 Xcode 中,按下“编译”按钮,使用当前方案构建应用。构建会产生错误,提示您在 Swift 的 SDKConstants.swift 文件或 Objective-C 的 SDKDemoAPIKey.h 文件中输入 API 密钥。
  4. 已启用 Maps SDK for iOS 的项目中获取 API 密钥
  5. 针对 Swift 修改 SDKConstants.swift 文件,针对 Objective-C 修改 SDKDemoAPIKey.h 文件,然后将您的 API 密钥粘贴到 apiKeykAPIKey 常量的定义中。例如:

    Swift

    static let apiKey = "YOUR_API_KEY"

    Objective-C

    static NSString *const kAPIKey = @"YOUR_API_KEY";
  6. SDKConstants.swift 文件 (Swift) 或 SDKDemoAPIKey.h 文件 (Objective-C) 中,移除以下行,因为它用于注册用户定义的问题:

    Swift

    #error (Register for API Key and insert here. Then delete this line.)

    Objective-C

    #error Register for API Key and insert here.
  7. 构建并运行项目。系统随即会显示 iOS 模拟器窗口,其中列出了 Maps SDK 演示版
  8. 选择显示的选项之一,以便试用 Maps SDK for iOS 的某项功能。
  9. 如果系统提示您允许 GoogleMapsDemos 访问您的位置信息,请选择允许