وقتی نقشه بهمدت طولانی فشرده میشود، مختصات اشاره به سرویس زمینکدیابی معکوس ارسال میشود. اگر موفقیتآمیز باشد، نشانگری با پنجره اطلاعات حاوی نتیجه به نقشه اضافه میشود.
شروع کنید
قبلاز اینکه بتوانید کد نمونه را امتحان کنید، باید محیط توسعه خود را پیکربندی کنید. برای اطلاعات بیشتر، نمونههای کد کیت توسعه نرمافزار Maps برای iOS را ببینید.
مشاهده کد
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] } } } }
آبجکتیو-سی
#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
اجرای برنامه نمونه کامل بهصورت محلی
برنامه نمونه «کیت توسعه نرمافزار Google Maps برای iOS» بهصورت بایگانی بارگیری در GitHub دردسترس است. برای نصب و امتحان کردن برنامه نمونه «کیت توسعه نرمافزار Google Maps برای iOS»، این مراحل را دنبال کنید.
git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.gitرا اجرا کنید تا مخزن نمونهها را در دایرکتوری محلی شبیهسازی کنید.پنجره ترمینال را باز کنید، به دایرکتوریای که فایلهای نمونه را در آن شبیهسازی کردهاید بروید، و به دایرکتوری GoogleMaps بروید:
Swift
cd maps-sdk-for-ios-samples/GoogleMaps-Swift
open GoogleMapsSwiftXCFrameworkDemos.xcodeprojآبجکتیو-سی
cd maps-sdk-for-ios-samples-main/GoogleMaps
open GoogleMapsDemos.xcodeproj- در پروژه Xcode، به File (فایل) > Add Package Dependencies (افزودن وابستگیهای بسته) بروید.
https://github.com/googlemaps/ios-maps-sdkرا بهعنوان نشانی وب وارد کنید، کلید ورود را فشار دهید تا بسته وارد شود، و روی افزودن بسته کلیک کنید. - در Xcode، دکمه کامپایل را فشار دهید تا
برنامه را با طرح فعلی بسازید. ساخت با خطا مواجه میشود و از شما میخواهد کلید میانای برنامهسازی کاربردی را
در فایل
SDKConstants.swiftبرای Swift یا فایلSDKDemoAPIKey.hبرای Objective-C وارد کنید. - کلید میانای API را از پروژهتان با کیت توسعه نرمافزار Google Maps برای iOS فعالشده دریافت کنید.
- فایل
SDKConstants.swiftرا برای Swift یا فایلSDKDemoAPIKey.hرا برای Objective-C ویرایش کنید و کلید میانای API خود را در تعریف ثابتapiKeyیاkAPIKeyجایگذاری کنید. برای مثال:Swift
static let apiKey = "YOUR_API_KEY"آبجکتیو-سی
static NSString *const kAPIKey = @"YOUR_API_KEY";
- در فایل
SDKConstants.swift(Swift) یا فایلSDKDemoAPIKey.h(Objective-C)، خط زیر را بردارید، زیرا برای ثبت مشکل تعریفشده توسط کاربر استفاده میشود:Swift
#error (Register for API Key and insert here. Then delete this line.)
آبجکتیو-سی
#error Register for API Key and insert here.
- پروژه را بسازید و اجرا کنید. پنجره شبیهساز iOS ظاهر میشود و فهرستی از نمونههای کیت توسعه نرمافزار Maps را نشان میدهد.
- یکیاز گزینههای نمایشدادهشده را انتخاب کنید تا ویژگیای از کیت توسعه نرمافزار Google Maps برای iOS را امتحان کنید.
- اگر از شما خواسته شد به GoogleMapsDemos اجازه دهید به مکانتان دسترسی داشته باشد، اجازه دادن را انتخاب کنید.