معکوس ژئوکد یک مکان

هنگامی که نقشه به مدت طولانی فشار داده می شود، مختصات ژست به سرویس رمزگذاری جغرافیایی معکوس ارسال می شود. در صورت موفقیت آمیز بودن، یک نشانگر با یک پنجره اطلاعات حاوی نتیجه به نقشه اضافه می شود.

شروع کنید

قبل از اینکه بتوانید کد نمونه را امتحان کنید، باید محیط توسعه خود را پیکربندی کنید. برای اطلاعات بیشتر، Maps SDK برای نمونه کدهای iOS را ببینید.

کد را مشاهده کنید

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
     

برنامه نمونه کامل را به صورت محلی اجرا کنید

برنامه نمونه Maps SDK برای iOS به عنوان یک بایگانی دانلود از GitHub در دسترس است. این مراحل را دنبال کنید تا برنامه نمونه Maps SDK برای iOS را نصب و امتحان کنید.

  1. git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git را اجرا کنید تا مخزن نمونه ها را در یک فهرست محلی کلون کنید.
  2. یک پنجره ترمینال را باز کنید، به دایرکتوری که فایل‌های نمونه را در آن کلون کرده‌اید بروید، و به فهرست راهنمای GoogleMaps بروید:

    cd maps-sdk-for-ios-samples-main/GoogleMaps-Swift
    pod install
    open GoogleMapsSwiftDemos.xcworkspace
    cd maps-sdk-for-ios-samples-main/GoogleMaps
    pod install
    open GoogleMapsDemos.xcworkspace
  3. در Xcode، دکمه کامپایل را فشار دهید تا برنامه با طرح فعلی ساخته شود . بیلد یک خطایی ایجاد می‌کند که از شما می‌خواهد کلید API خود را در فایل SDKConstants.swift برای Swift یا فایل SDKDemoAPIKey.h برای Objective-C وارد کنید.
  4. اگر هنوز کلید API ندارید، دستورالعمل‌ها را برای راه‌اندازی یک پروژه در Google Cloud Console و دریافت یک کلید API دنبال کنید. هنگام پیکربندی کلید در Cloud Console، می توانید کلید را به شناسه بسته نرم افزاری نمونه محدود کنید تا مطمئن شوید که فقط برنامه شما می تواند از کلید استفاده کند. شناسه بسته پیش‌فرض برنامه نمونه‌های SDK com.example.GoogleMapsDemos است.
  5. فایل SDKConstants.swift را برای Swift یا SDKDemoAPIKey.h برای Objective-C ویرایش کنید و کلید API خود را در تعریف ثابت apiKey یا kAPIKey قرار دهید. به عنوان مثال:
    static let apiKey = "YOUR_API_KEY"
    static NSString *const kAPIKey = @"YOUR_API_KEY";
  6. در فایل SDKConstants.swift (Swift) یا فایل SDKDemoAPIKey.h (Objective-C)، خط زیر را حذف کنید، زیرا برای ثبت مشکل تعریف شده توسط کاربر استفاده می شود:
    #error (Register for API Key and insert here. Then delete this line.)
    #error Register for API Key and insert here.
  7. پروژه را بسازید و اجرا کنید. پنجره شبیه‌ساز iOS ظاهر می‌شود که فهرستی از نسخه‌های نمایشی Maps SDK را نشان می‌دهد.
  8. یکی از گزینه های نمایش داده شده را انتخاب کنید تا با یکی از ویژگی های Maps SDK برای iOS آزمایش کنید.
  9. اگر از شما خواسته شد به GoogleMapsDemos اجازه دسترسی به موقعیت مکانی شما را بدهید، Allow را انتخاب کنید.