Bật và tắt cử chỉ trên bản đồ
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bản đồ chỉ phản hồi các cử chỉ thu phóng khi nút chuyển được bật.
Bắt đầu
Trước khi có thể thử mã mẫu, bạn phải định cấu hình môi trường phát triển.
Để biết thêm thông tin, hãy xem Mẫu mã SDK Bản đồ dành cho iOS.
Xem mã
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
Chạy ứng dụng mẫu đầy đủ trên máy
Ứng dụng mẫu SDK Bản đồ dành cho iOS có sẵn dưới dạng tài nguyên lưu trữ để tải xuống trên GitHub.
Hãy làm theo các bước sau để cài đặt và dùng thử ứng dụng mẫu Maps SDK cho iOS.
- Chạy
git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git
để nhân bản kho lưu trữ mẫu vào một thư mục cục bộ.
Mở cửa sổ dòng lệnh, chuyển đến thư mục mà bạn đã nhân bản các tệp mẫu và đi sâu vào thư mục 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
- Trong Xcode, hãy nhấn vào nút biên dịch để tạo ứng dụng bằng giao thức hiện tại. Bản dựng sẽ tạo ra lỗi, nhắc bạn nhập khoá API trong tệp
SDKConstants.swift
cho Swift hoặc tệp SDKDemoAPIKey.h
cho Objective-C.
- Lấy khoá API từ dự án của bạn bằng cách bật SDK Bản đồ dành cho iOS.
- Chỉnh sửa tệp
SDKConstants.swift
cho Swift hoặc tệp SDKDemoAPIKey.h
cho Objective-C và dán khoá API vào phần khai báo của hằng số apiKey
hoặc kAPIKey
. Ví dụ:
Swift
static let apiKey = "YOUR_API_KEY"
Objective-C
static NSString *const kAPIKey = @"YOUR_API_KEY";
- Trong tệp
SDKConstants.swift
(Swift) hoặc tệp SDKDemoAPIKey.h
(Objective-C), hãy xoá dòng sau vì dòng này dùng để đăng ký vấn đề do người dùng xác định:
Swift
#error (Register for API Key and insert here. Then delete this line.)
Objective-C
#error Register for API Key and insert here.
- Tạo bản dựng và chạy dự án. Cửa sổ trình mô phỏng iOS sẽ xuất hiện, hiển thị danh sách Bản minh hoạ SDK Bản đồ.
- Chọn một trong các tuỳ chọn hiển thị để thử nghiệm một tính năng của SDK Bản đồ dành cho iOS.
- Nếu bạn được nhắc cho phép GoogleMapsDemos truy cập vào vị trí của bạn, hãy chọn Cho phép.
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eThis webpage provides code samples demonstrating how to control zoom gestures on a Google Map using a switch.\u003c/p\u003e\n"],["\u003cp\u003eWhen the switch is toggled on, users can zoom in and out of the map using gestures.\u003c/p\u003e\n"],["\u003cp\u003eTo run the sample code, you need to configure your development environment and obtain a Google Maps API key.\u003c/p\u003e\n"],["\u003cp\u003eThe webpage provides step-by-step instructions on how to set up and run the sample app locally.\u003c/p\u003e\n"]]],["The provided code demonstrates how to control zoom gestures on a Google Map in iOS using a switch. A `GMSMapView` is created, and a `UISwitch` toggles zoom gesture functionality on or off. When the switch is on, zoom gestures are enabled; when off, they are disabled. The code examples are provided in both Swift and Objective-C and include instructions on how to install the Maps SDK for iOS sample app.\n"],null,["The map responds to zoom gestures only when the switch is toggled on.\n\nGet started\n\nBefore you can try the sample code, you must configure your development environment.\nFor more information, see [Maps SDK for iOS code samples](/maps/documentation/ios-sdk/examples).\n\nView the code \n\nSwift \n\n```swift\nimport GoogleMaps\nimport UIKit\n\nclass GestureControlViewController: UIViewController {\n private let holderHeight: CGFloat = 60\n private let zoomLabelInset: CGFloat = 16\n\n private lazy var mapView: GMSMapView = {\n let camera = GMSCameraPosition(latitude: -25.5605, longitude: 133.605097, zoom: 3)\n return GMSMapView(frame: .zero, camera: camera)\n }()\n private lazy var zoomSwitch: UISwitch = UISwitch(frame: .zero)\n\n override func viewDidLoad() {\n super.viewDidLoad()\n\n view.addSubview(mapView)\n\n let holder = UIView(frame: .zero)\n holder.backgroundColor = UIColor(white: 1, alpha: 0.8)\n view.addSubview(holder)\n\n let zoomLabel = UILabel(frame: .zero)\n zoomLabel.text = \"Zoom gestures\"\n zoomLabel.font = .boldSystemFont(ofSize: 18)\n holder.addSubview(zoomLabel)\n\n // Control zooming.\n holder.addSubview(zoomSwitch)\n zoomSwitch.addTarget(self, action: #selector(toggleZoom), for: .valueChanged)\n zoomSwitch.isOn = true\n\n [mapView, holder, zoomLabel, zoomSwitch].forEach({\n $0.translatesAutoresizingMaskIntoConstraints = false\n })\n NSLayoutConstraint.activate([\n mapView.leftAnchor.constraint(equalTo: view.leftAnchor),\n mapView.rightAnchor.constraint(equalTo: view.rightAnchor),\n mapView.topAnchor.constraint(equalTo: view.topAnchor),\n mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor),\n holder.heightAnchor.constraint(equalToConstant: holderHeight),\n holder.centerXAnchor.constraint(equalTo: view.centerXAnchor),\n holder.widthAnchor.constraint(equalTo: view.widthAnchor),\n zoomLabel.leftAnchor.constraint(equalTo: holder.leftAnchor, constant: zoomLabelInset),\n zoomLabel.centerYAnchor.constraint(equalTo: holder.centerYAnchor),\n zoomSwitch.rightAnchor.constraint(equalTo: holder.rightAnchor, constant: -zoomLabelInset),\n zoomSwitch.centerYAnchor.constraint(\n equalTo: holder.centerYAnchor),\n ])\n NSLayoutConstraint.activate([\n holder.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor)\n ])\n }\n\n @objc func toggleZoom() {\n mapView.settings.zoomGestures = zoomSwitch.isOn\n }\n\n}https://github.com/googlemaps-samples/maps-sdk-for-ios-samples/blob/86408feffd008565cd2cafce8aff3dc27f1f41bb/GoogleMaps-Swift/GoogleMapsSwiftDemos/Swift/Samples/GestureControlViewController.swift#L14-L72\n \n```\n\nObjective-C \n\n```objective-c\n#import \"GoogleMapsDemos/Samples/GestureControlViewController.h\"\n\n#import \u003cGoogleMaps/GoogleMaps.h\u003e\n\n@implementation GestureControlViewController {\n GMSMapView *_mapView;\n UISwitch *_zoomSwitch;\n}\n\n- (void)viewDidLoad {\n [super viewDidLoad];\n GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-25.5605\n longitude:133.605097\n zoom:3];\n\n _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];\n _mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;\n _mapView.accessibilityIdentifier = @\"gestureControlDemoMapView\";\n\n self.view = [[UIView alloc] initWithFrame:CGRectZero];\n [self.view addSubview:_mapView];\n\n UIView *holder = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 59)];\n holder.autoresizingMask =\n UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;\n holder.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.8f];\n [self.view addSubview:holder];\n\n // Zoom label.\n UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 16, 200, 29)];\n label.text = @\"Zooming?\";\n label.font = [UIFont boldSystemFontOfSize:18.0f];\n label.textAlignment = NSTextAlignmentLeft;\n label.backgroundColor = [UIColor clearColor];\n label.layer.shadowColor = [[UIColor whiteColor] CGColor];\n label.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);\n label.layer.shadowOpacity = 1.0f;\n label.layer.shadowRadius = 0.0f;\n [holder addSubview:label];\n\n // Control zooming.\n _zoomSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(-90, 16, 0, 0)];\n _zoomSwitch.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;\n [_zoomSwitch addTarget:self\n action:@selector(didChangeZoomSwitch)\n forControlEvents:UIControlEventValueChanged];\n _zoomSwitch.on = YES;\n [holder addSubview:_zoomSwitch];\n}\n\n- (void)didChangeZoomSwitch {\n _mapView.settings.zoomGestures = _zoomSwitch.isOn;\n}\n\n@end \nhttps://github.com/googlemaps-samples/maps-sdk-for-ios-samples/blob/86408feffd008565cd2cafce8aff3dc27f1f41bb/GoogleMaps/GoogleMapsDemos/Samples/GestureControlViewController.m#L16-L70\n\n \n```\n\nRun the full sample app locally\n\nThe Maps SDK for iOS sample app is available as a\n[download archive](https://github.com/googlemaps-samples/maps-sdk-for-ios-samples/archive/main.zip)\nfrom [GitHub](https://github.com/googlemaps-samples/maps-sdk-for-ios-samples/tree/main/GoogleMaps).\nFollow these steps to install and try the Maps SDK for iOS sample app.\n\n1. Run `git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git` to clone the samples repository into a local directory.\n2. Open a terminal window, navigate to the directory where you cloned the sample files, and\n drill down into the GoogleMaps directory:\n\n Swift \n\n cd maps-sdk-for-ios-samples-main/GoogleMaps-Swift\n pod install\n open GoogleMapsSwiftDemos.xcworkspace\n\n Objective-C \n\n cd maps-sdk-for-ios-samples-main/GoogleMaps\n pod install\n open GoogleMapsDemos.xcworkspace\n\n3. In Xcode, press the compile button to [build the app](https://developer.apple.com/documentation/xcode/building-and-running-an-app) with the current scheme. The build produces an error, prompting you to enter your API key in the `SDKConstants.swift` file for Swift or`SDKDemoAPIKey.h` file for Objective-C.\n4. [Get an API key](/maps/documentation/ios-sdk/get-api-key) from your project with the [Maps SDK for iOS enabled](/maps/documentation/ios-sdk/cloud-setup#enabling-apis).\n5. Edit the `SDKConstants.swift` file for Swift or`SDKDemoAPIKey.h` file for Objective-C and paste your API key into the definition of either the `apiKey` or `kAPIKey` constant. For example: \n\n Swift \n\n ```scdoc\n static let apiKey = \"YOUR_API_KEY\"\n ```\n\n Objective-C \n\n ```objective-c\n static NSString *const kAPIKey = @\"YOUR_API_KEY\";\n ```\n6. In the `SDKConstants.swift` file (Swift) or`SDKDemoAPIKey.h` file (Objective-C), remove the following line, because it's used to register the user-defined issue: \n\n Swift \n\n ```text\n #error (Register for API Key and insert here. Then delete this line.)\n ```\n\n Objective-C \n\n ```text\n #error Register for API Key and insert here.\n ```\n7. Build and run the project. The iOS simulator window appears, showing a list of **Maps SDK Demos**.\n8. Choose one of the options displayed, to experiment with a feature of the Maps SDK for iOS.\n9. If prompted to allow GoogleMapsDemos to access your location, choose **Allow**."]]