معکوس ژئوکد یک مکان
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
توسعه دهندگان منطقه اقتصادی اروپا (EEA). هنگامی که نقشه به مدت طولانی فشار داده می شود، مختصات ژست به سرویس رمزگذاری جغرافیایی معکوس ارسال می شود. در صورت موفقیت آمیز بودن، یک نشانگر با یک پنجره اطلاعات حاوی نتیجه به نقشه اضافه می شود.
شروع کنید
قبل از اینکه بتوانید کد نمونه را امتحان کنید، باید محیط توسعه خود را پیکربندی کنید. برای اطلاعات بیشتر، 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]
}
}
}
}
هدف-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
برنامه نمونه کامل را به صورت محلی اجرا کنید
برنامه نمونه Maps SDK برای iOS به عنوان یک بایگانی دانلود از GitHub در دسترس است. این مراحل را دنبال کنید تا برنامه نمونه Maps SDK برای iOS را نصب و امتحان کنید.
-
git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git
را اجرا کنید تا مخزن نمونه ها را در یک فهرست محلی کلون کنید. یک پنجره ترمینال را باز کنید، به دایرکتوری که فایلهای نمونه را در آن کلون کردهاید بروید، و به فهرست راهنمای GoogleMaps بروید:
سویفت
cd maps-sdk-for-ios-samples-main/GoogleMaps-Swift
pod install
open GoogleMapsSwiftDemos.xcworkspace
هدف-C
cd maps-sdk-for-ios-samples-main/GoogleMaps
pod install
open GoogleMapsDemos.xcworkspace
- در Xcode، دکمه کامپایل را فشار دهید تا برنامه با طرح فعلی ساخته شود . بیلد یک خطایی ایجاد میکند که از شما میخواهد کلید API خود را در فایل
SDKConstants.swift
برای Swift یا فایل SDKDemoAPIKey.h
برای Objective-C وارد کنید. - با فعال کردن Maps SDK برای iOS، یک کلید API از پروژه خود دریافت کنید .
- فایل
SDKConstants.swift
را برای Swift یا SDKDemoAPIKey.h
برای Objective-C ویرایش کنید و کلید API خود را در تعریف ثابت apiKey
یا kAPIKey
قرار دهید. به عنوان مثال: سویفت
static let apiKey = "YOUR_API_KEY"
هدف-C
static NSString *const kAPIKey = @"YOUR_API_KEY";
- در فایل
SDKConstants.swift
(Swift) یا فایل SDKDemoAPIKey.h
(Objective-C)، خط زیر را حذف کنید، زیرا برای ثبت مشکل تعریف شده توسط کاربر استفاده می شود: سویفت
#error (Register for API Key and insert here. Then delete this line.)
هدف-C
#error Register for API Key and insert here.
- پروژه را بسازید و اجرا کنید. پنجره شبیهساز iOS ظاهر میشود که فهرستی از نسخههای نمایشی Maps SDK را نشان میدهد.
- یکی از گزینه های نمایش داده شده را انتخاب کنید تا با یکی از ویژگی های Maps SDK برای iOS آزمایش کنید.
- اگر از شما خواسته شد به GoogleMapsDemos اجازه دسترسی به موقعیت مکانی شما را بدهید، Allow را انتخاب کنید.
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eThis code demonstrates reverse geocoding, converting coordinates to a human-readable address when a user long-presses a location on a Google Map.\u003c/p\u003e\n"],["\u003cp\u003eUpon a long press, a marker with an info window displaying the address is placed on the map at the specified coordinates.\u003c/p\u003e\n"],["\u003cp\u003eTo run the code, you need to configure your development environment with the Maps SDK for iOS and obtain a Google Maps API key.\u003c/p\u003e\n"],["\u003cp\u003eThe sample app provides various demos showcasing different features of the Maps SDK for iOS, accessible after building and running the project.\u003c/p\u003e\n"]]],["Upon a long press on the map, the gesture's coordinates are sent to the reverse geocoding service. A successful response results in a marker being added to the map at that location. The marker's info window then displays the address obtained from the service. The marker's title shows the first line of the address and the marker snippet displays the second line of the address if present.\n"],null,["**European Economic Area (EEA) developers** If your billing address is in the European Economic Area, effective on 8 July 2025, the [Google Maps Platform EEA Terms of Service](https://cloud.google.com/terms/maps-platform/eea) will apply to your use of the Services. Functionality varies by region. [Learn more](/maps/comms/eea/faq).\n\nWhen the map is long pressed, the coordinates of the gesture are sent to the reverse geocoding service. If successful, a marker with an info window containing the result is added to the map.\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\n// Sample code for GeoCoder service.\nclass GeocoderViewController: UIViewController {\n\n private lazy var mapView: GMSMapView = {\n let camera = GMSCameraPosition(latitude: -33.868, longitude: 151.2086, zoom: 12)\n return GMSMapView(frame: .zero, camera: camera)\n }()\n\n private lazy var geocoder = GMSGeocoder()\n\n override func loadView() {\n view = mapView\n mapView.delegate = self\n }\n}\n\nextension GeocoderViewController: GMSMapViewDelegate {\n func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) {\n // On a long press, reverse geocode this location.\n geocoder.reverseGeocodeCoordinate(coordinate) { response, error in\n guard let address = response?.firstResult() else {\n let errorMessage = error.map { String(describing: $0) } ?? \"\u003cno error\u003e\"\n print(\n \"Could not reverse geocode point (\\(coordinate.latitude), \\(coordinate.longitude)): \\(errorMessage)\"\n )\n return\n }\n print(\"Geocoder result: \\(address)\")\n let marker = GMSMarker(position: address.coordinate)\n marker.appearAnimation = .pop\n marker.map = mapView\n\n guard let lines = address.lines, let title = lines.first else { return }\n marker.title = title\n if lines.count \u003e 1 {\n marker.snippet = lines[1]\n }\n }\n }\n}https://github.com/googlemaps-samples/maps-sdk-for-ios-samples/blob/86408feffd008565cd2cafce8aff3dc27f1f41bb/GoogleMaps-Swift/GoogleMapsSwiftDemos/Swift/Samples/GeocoderViewController.swift#L14-L56\n \n```\n\nObjective-C \n\n```objective-c\n#import \"GoogleMapsDemos/Samples/GeocoderViewController.h\"\n\n#import \u003cGoogleMaps/GoogleMaps.h\u003e\n\n@implementation GeocoderViewController {\n GMSMapView *_mapView;\n GMSGeocoder *_geocoder;\n}\n\n- (void)viewDidLoad {\n [super viewDidLoad];\n GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868\n longitude:151.2086\n zoom:12];\n\n _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];\n _mapView.delegate = self;\n\n _geocoder = [[GMSGeocoder alloc] init];\n\n self.view = _mapView;\n}\n\n- (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate {\n // On a long press, reverse geocode this location.\n __weak __typeof__(self) weakSelf = self;\n GMSReverseGeocodeCallback handler = ^(GMSReverseGeocodeResponse *response, NSError *error) {\n [weakSelf handleResponse:response coordinate:coordinate error:error];\n };\n [_geocoder reverseGeocodeCoordinate:coordinate completionHandler:handler];\n}\n\n- (void)handleResponse:(nullable GMSReverseGeocodeResponse *)response\n coordinate:(CLLocationCoordinate2D)coordinate\n error:(nullable NSError *)error {\n GMSAddress *address = response.firstResult;\n if (address) {\n NSLog(@\"Geocoder result: %@\", address);\n\n GMSMarker *marker = [GMSMarker markerWithPosition:address.coordinate];\n NSArray\u003cNSString *\u003e *lines = [address lines];\n\n marker.title = [lines firstObject];\n if (lines.count \u003e 1) {\n marker.snippet = [lines objectAtIndex:1];\n }\n\n marker.appearAnimation = kGMSMarkerAnimationPop;\n marker.map = _mapView;\n } else {\n NSLog(@\"Could not reverse geocode point (%f,%f): %@\", coordinate.latitude, coordinate.longitude,\n error);\n }\n}\n\n@end \nhttps://github.com/googlemaps-samples/maps-sdk-for-ios-samples/blob/86408feffd008565cd2cafce8aff3dc27f1f41bb/GoogleMaps/GoogleMapsDemos/Samples/GeocoderViewController.m#L16-L71\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**."]]