Khi bản đồ được nhấn và giữ, toạ độ của cử chỉ sẽ được gửi đến dịch vụ mã hoá địa lý ngược. Nếu thành công, một điểm đánh dấu có cửa sổ thông tin chứa kết quả sẽ được thêm vào bản đồ.
Bắt đầu
Trước khi có thể dùng 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 Các đoạn mã ví dụ về Maps SDK cho iOS.
Xem mã
Swift
import GoogleMaps import UIKit // Sample code for GeoCoder service. class GeocoderViewController: UIViewController { private lazy var mapView: GMSMapView = { let camera = GMSCameraPosition(latitude: -33.868, longitude: 151.2086, zoom: 12) return GMSMapView(frame: .zero, camera: camera) }() private lazy var geocoder = GMSGeocoder() override func loadView() { view = mapView mapView.delegate = self } } extension GeocoderViewController: GMSMapViewDelegate { func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) { // On a long press, reverse geocode this location. geocoder.reverseGeocodeCoordinate(coordinate) { response, error in guard let address = response?.firstResult() else { let errorMessage = error.map { String(describing: $0) } ?? "<no error>" print( "Could not reverse geocode point (\(coordinate.latitude), \(coordinate.longitude)): \(errorMessage)" ) return } print("Geocoder result: \(address)") let marker = GMSMarker(position: address.coordinate) marker.appearAnimation = .pop marker.map = mapView guard let lines = address.lines, let title = lines.first else { return } marker.title = title if lines.count > 1 { marker.snippet = lines[1] } } } }
Objective-C
#import "GoogleMapsDemos/Samples/GeocoderViewController.h" #import <GoogleMaps/GoogleMaps.h> @implementation GeocoderViewController { GMSMapView *_mapView; GMSGeocoder *_geocoder; } - (void)viewDidLoad { [super viewDidLoad]; GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868 longitude:151.2086 zoom:12]; _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; _mapView.delegate = self; _geocoder = [[GMSGeocoder alloc] init]; self.view = _mapView; } - (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate { // On a long press, reverse geocode this location. __weak __typeof__(self) weakSelf = self; GMSReverseGeocodeCallback handler = ^(GMSReverseGeocodeResponse *response, NSError *error) { [weakSelf handleResponse:response coordinate:coordinate error:error]; }; [_geocoder reverseGeocodeCoordinate:coordinate completionHandler:handler]; } - (void)handleResponse:(nullable GMSReverseGeocodeResponse *)response coordinate:(CLLocationCoordinate2D)coordinate error:(nullable NSError *)error { GMSAddress *address = response.firstResult; if (address) { NSLog(@"Geocoder result: %@", address); GMSMarker *marker = [GMSMarker markerWithPosition:address.coordinate]; NSArray<NSString *> *lines = [address lines]; marker.title = [lines firstObject]; if (lines.count > 1) { marker.snippet = [lines objectAtIndex:1]; } marker.appearAnimation = kGMSMarkerAnimationPop; marker.map = _mapView; } else { NSLog(@"Could not reverse geocode point (%f,%f): %@", coordinate.latitude, coordinate.longitude, error); } } @end
Chạy toàn bộ ứng dụng mẫu trên thiết bị
Ứng dụng mẫu Maps SDK cho iOS có sẵn dưới dạng tệp lưu trữ có thể 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
để sao chép 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 đã sao chép 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 nút biên dịch để tạo ứng dụng bằng lược đồ hiện tại. Bản dựng sẽ tạo ra một lỗi, nhắc bạn nhập khoá API trong tệp
SDKConstants.swift
cho Swift hoặc tệpSDKDemoAPIKey.h
cho Objective-C. - Lấy khoá API từ dự án của bạn khi đã bật Maps SDK cho iOS.
- Chỉnh sửa tệp
SDKConstants.swift
cho Swift hoặc tệpSDKDemoAPIKey.h
cho Objective-C rồi dán khoá API vào định nghĩa của hằng sốapiKey
hoặckAPIKey
. 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ệpSDKDemoAPIKey.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, cho thấy danh sách Bản minh hoạ SDK Maps.
- Chọn một trong các lựa chọn xuất hiện để thử nghiệm một tính năng của SDK Bản đồ dành cho iOS.
- Nếu đượ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.