با مجموعهها، منظم بمانید ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
یک نقشه با نشانگر اضافه کنید
این آموزش نشان می دهد که چگونه یک نقشه گوگل را با یک نشانگر به برنامه iOS خود اضافه کنید. این برای افرادی مناسب است که دانش مبتدی یا متوسط از Swift یا Objective-C به همراه دانش عمومی Xcode دارند. برای راهنمای پیشرفته برای ایجاد نقشه، راهنمای توسعه دهندگان را بخوانید.
نقشه زیر را با استفاده از این آموزش ایجاد خواهید کرد. نشانگر در سیدنی، استرالیا قرار دارد.
importUIKitimportGoogleMapsclassViewController:UIViewController{overridefuncviewDidLoad(){super.viewDidLoad()// Do any additional setup after loading the view.// Create a GMSCameraPosition that tells the map to display the// coordinate -33.86,151.20 at zoom level 6.letcamera=GMSCameraPosition.camera(withLatitude:-33.86,longitude:151.20,zoom:6.0)letmapView=GMSMapView.map(withFrame:self.view.frame,camera:camera)self.view.addSubview(mapView)// Creates a marker in the center of the map.letmarker=GMSMarker()marker.position=CLLocationCoordinate2D(latitude:-33.86,longitude:151.20)marker.title="Sydney"marker.snippet="Australia"marker.map=mapView}}
هدف-C
#import "ViewController.h"#import <GoogleMaps/GoogleMaps.h>@interfaceViewController()@end@implementationViewController-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.// Create a GMSCameraPosition that tells the map to display the// coordinate -33.86,151.20 at zoom level 6.GMSCameraPosition*camera=[GMSCameraPositioncameraWithLatitude:-33.86longitude:151.20zoom:6];GMSMapView*mapView=[GMSMapViewmapWithFrame:self.view.framecamera:camera];mapView.myLocationEnabled=YES;[self.viewaddSubview:mapView];// Creates a marker in the center of the map.GMSMarker*marker=[[GMSMarkeralloc]init];marker.position=CLLocationCoordinate2DMake(-33.86,151.20);marker.title=@"Sydney";marker.snippet=@"Australia";marker.map=mapView;}@end
اگر از قبل CocoaPods ندارید، با اجرای دستور زیر از ترمینال، آن را روی macOS نصب کنید:
sudo gem install cocoapods
به دایرکتوری tutorials/map-with-marker بروید.
دستور pod install اجرا کنید. با این کار Maps SDK مشخص شده در Podfile به همراه هر گونه وابستگی نصب می شود.
برای مقایسه نسخه پاد نصب شده با هر بهروزرسانی جدید، pod outdated را اجرا کنید. اگر نسخه جدیدی شناسایی شد، pod update اجرا کنید تا Podfile بهروزرسانی شود و آخرین SDK نصب شود. برای جزئیات بیشتر، به راهنمای CocoaPods مراجعه کنید.
فایل map-with-marker.xcworkspace پروژه را باز کنید (دوبار کلیک کنید) تا در Xcode باز شود. برای باز کردن پروژه باید از فایل .xcworkspace استفاده کنید.
یک کلید API دریافت کنید و API های لازم را فعال کنید
برای تکمیل این آموزش، به یک کلید Google API نیاز دارید که مجاز به استفاده از Maps SDK برای iOS باشد. برای دریافت کلید و فعال کردن API روی دکمه زیر کلیک کنید.
کلید API خود را به صورت زیر به AppDelegate.swift خود اضافه کنید:
توجه داشته باشید که عبارت import زیر به فایل اضافه شده است:
importGoogleMaps
خط زیر را در روش application(_:didFinishLaunchingWithOptions:) خود ویرایش کنید و کلید API خود را جایگزین YOUR_API_KEY کنید:
GMSServices.provideAPIKey("YOUR_API_KEY")
اپلیکیشن خود را بسازید و اجرا کنید
یک دستگاه iOS را به رایانه خود وصل کنید یا یک شبیه ساز را از منوی طرح Xcode انتخاب کنید.
اگر از دستگاهی استفاده میکنید، مطمئن شوید که خدمات موقعیت مکانی فعال هستند. اگر از شبیه ساز استفاده می کنید، یک مکان را از منوی ویژگی ها انتخاب کنید.
در Xcode، روی گزینه منوی Product/Run (یا نماد دکمه پخش) کلیک کنید.
Xcode برنامه را می سازد و سپس برنامه را روی دستگاه یا شبیه ساز اجرا می کند.
شما باید نقشه ای را با یک نشانگر در مرکز سیدنی در ساحل شرقی استرالیا، مشابه تصویر این صفحه ببینید.
عیب یابی:
اگر نقشهای را نمیبینید، بررسی کنید که کلید API را دریافت کردهاید و آن را به برنامه اضافه کردهاید، همانطور که قبلاً توضیح داده شد . کنسول اشکال زدایی Xcode را برای پیام های خطا در مورد کلید API بررسی کنید.
اگر کلید API را با شناسه بسته iOS محدود کردهاید، کلید را ویرایش کنید تا شناسه بسته را برای برنامه اضافه کنید: com.google.examples.map-with-marker .
یک نقشه ایجاد کنید و آن را به عنوان نمای در viewDidLoad() تنظیم کنید.
سویفت
// Create a GMSCameraPosition that tells the map to display the// coordinate -33.86,151.20 at zoom level 6.letcamera=GMSCameraPosition.camera(withLatitude:-33.86,longitude:151.20,zoom:6.0)letmapView=GMSMapView.map(withFrame:CGRect.zero,camera:camera)view=mapView
هدف-C
// Create a GMSCameraPosition that tells the map to display the// coordinate -33.86,151.20 at zoom level 6.GMSCameraPosition*camera=[GMSCameraPositioncameraWithLatitude:-33.86longitude:151.20zoom:6.0];GMSMapView*mapView=[[GMSMapViewalloc]initWithFrame:CGRectZerocamera:camera];self.view=mapView;
در viewDidLoad() یک نشانگر به نقشه اضافه کنید.
سویفت
// Creates a marker in the center of the map.letmarker=GMSMarker()marker.position=CLLocationCoordinate2D(latitude:-33.86,longitude:151.20)marker.title="Sydney"marker.snippet="Australia"marker.map=mapView
هدف-C
// Creates a marker in the center of the map.GMSMarker*marker=[[GMSMarkeralloc]init];marker.position=CLLocationCoordinate2DMake(-33.86,151.20);marker.title=@"Sydney";marker.snippet=@"Australia";marker.map=mapView;
به طور پیشفرض، Maps SDK برای iOS محتوای پنجره اطلاعات را زمانی که کاربر روی یک نشانگر ضربه میزند، نمایش میدهد. اگر از رفتار پیشفرض راضی هستید، نیازی به افزودن یک کلیک شنونده برای نشانگر نیست.
تبریک می گویم! شما یک برنامه iOS ساخته اید که نقشه گوگل را با یک نشانگر برای نشان دادن یک مکان خاص نمایش می دهد. همچنین نحوه استفاده از Maps SDK برای iOS را یاد گرفتهاید.
مراحل بعدی
درباره شی نقشه و کارهایی که می توانید با نشانگرها انجام دهید بیشتر بیاموزید.
تاریخ آخرین بهروزرسانی 2025-07-23 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-07-23 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eThis tutorial provides a step-by-step guide for adding a Google map with a marker to your iOS app, targeting beginners and intermediate Swift/Objective-C developers.\u003c/p\u003e\n"],["\u003cp\u003eThe tutorial covers installation using Swift Package Manager or CocoaPods, obtaining and implementing an API key, and building/running the app.\u003c/p\u003e\n"],["\u003cp\u003eCode examples in Swift and Objective-C demonstrate creating a map view, setting camera position, and adding a marker with title and snippet.\u003c/p\u003e\n"],["\u003cp\u003eTroubleshooting tips address common issues like missing maps, API key errors, and connectivity problems.\u003c/p\u003e\n"],["\u003cp\u003eUsers are encouraged to further explore the map object and marker functionalities within the Maps SDK for iOS.\u003c/p\u003e\n"]]],["This tutorial guides you to add a Google map with a marker to an iOS app. First, you'll download the sample code, then install the Maps SDK using Swift Package Manager or CocoaPods. Next, you'll acquire a Google API key and add it to your `AppDelegate.swift`. You'll then build and run the app on a device or simulator. The code creates a map centered on coordinates -33.86, 151.20 (Sydney) at zoom level 6, and adds a marker at that location with \"Sydney\" as the title and \"Australia\" as the snippet.\n"],null,["Add a Map with a Marker \n\nThis tutorial shows how to add a Google map with a marker to your iOS\napp. It suits people with a beginner or intermediate knowledge of Swift or\nObjective-C along with general knowledge of Xcode. For an advanced guide to\ncreating maps, read the developers' guide.\n\nYou'll create the following map using this tutorial. The marker is positioned at\nSydney, Australia.\n\nGet the code\n\nClone or download the\n[Google Maps iOS samples repository](https://github.com/googlemaps-samples/maps-sdk-for-ios-samples) on GitHub.\n\nAlternatively, click the following button to download the source code:\n\n[Give me the code](https://github.com/googlemaps-samples/maps-sdk-for-ios-samples/archive/main.zip)\n\n\nSwift \n\n```swift\nimport UIKit\nimport GoogleMaps\n\nclass ViewController: UIViewController {\n\n override func viewDidLoad() {\n super.viewDidLoad()\n // Do any additional setup after loading the view.\n // Create a GMSCameraPosition that tells the map to display the\n // coordinate -33.86,151.20 at zoom level 6.\n let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)\n let mapView = GMSMapView.map(withFrame: self.view.frame, camera: camera)\n self.view.addSubview(mapView)\n\n // Creates a marker in the center of the map.\n let marker = GMSMarker()\n marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)\n marker.title = \"Sydney\"\n marker.snippet = \"Australia\"\n marker.map = mapView\n }\n}\n \n```\n\nObjective-C \n\n```objective-c\n#import \"ViewController.h\"\n#import \u003cGoogleMaps/GoogleMaps.h\u003e\n\n@interface ViewController ()\n\n@end\n\n@implementation ViewController\n\n- (void)viewDidLoad {\n [super viewDidLoad];\n // Do any additional setup after loading the view.\n // Create a GMSCameraPosition that tells the map to display the\n // coordinate -33.86,151.20 at zoom level 6.\n GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86\n longitude:151.20\n zoom:6];\n GMSMapView *mapView = [GMSMapView mapWithFrame:self.view.frame camera:camera];\n mapView.myLocationEnabled = YES;\n [self.view addSubview:mapView];\n\n // Creates a marker in the center of the map.\n GMSMarker *marker = [[GMSMarker alloc] init];\n marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);\n marker.title = @\"Sydney\";\n marker.snippet = @\"Australia\";\n marker.map = mapView;\n}\n\n@end\n \n```\n\n\u003cbr /\u003e\n\nGet started \n\nSwift Package Manager\n\nThe Maps SDK for iOS can be installed using [Swift Package Manager](https://developer.apple.com/documentation/xcode/swift-packages).\n\n1. Remove any existing Maps SDK for iOS dependencies.\n2. Open a terminal window and navigate to the `tutorials/map-with-marker` directory.\n3. Close your Xcode workspace and run the following commands: \n\n ```swift\n sudo gem install cocoapods-deintegrate cocoapods-clean\n pod deintegrate\n pod cache clean --all\n rm Podfile\n rm map-with-marker.xcworkspace\n ```\n4. Open your Xcode project and delete the podfile.\n5. Go to **File \\\u003e Add Package Dependencies**.\n6. Enter \u003chttps://github.com/googlemaps/ios-maps-sdk\u003e as the URL, press **Enter** to pull in the package, and click **Add Package**.\n7. You may need to reset your package cache using **File \\\u003e Packages \\\u003e Reset Package Cache**.\n\nUse CocoaPods\n\n1. Download and install [Xcode](https://developer.apple.com/xcode/) **version 16.0** or later.\n2. If you don't already have [CocoaPods](https://guides.cocoapods.org/using/getting-started.html), install it on macOS by running the following command from the terminal: \n\n ```text\n sudo gem install cocoapods\n ```\n3. Navigate to the `tutorials/map-with-marker` directory.\n4. Run the `pod install` command. This will install the [Maps SDK](https://developers.google.com/maps/documentation/ios-sdk) specified in the `Podfile`, along with any dependencies.\n5. Run `pod outdated` to compare the installed pod version with any new updates. If a new version is detected, run `pod update` to update the `Podfile` and install the latest SDK. For more details, see the [CocoaPods Guide](https://guides.cocoapods.org/using/pod-install-vs-update.html).\n6. Open (double-click) the project's **map-with-marker.xcworkspace** file to open it in Xcode. You must use the `.xcworkspace` file to open the project.\n\nGet an API key and enable the necessary APIs\n\nTo complete this tutorial, you need a Google API key that's authorized to\nuse the Maps SDK for iOS. Click the following button to get\na key and activate the API.\n[Get Started](https://cloud.google.com/maps-platform/#get-started)\n\nFor more details, see\n[Get an API key](/maps/documentation/ios-sdk/get-api-key).\n\nAdd the API key to your application\n\nAdd your API key to your `AppDelegate.swift` as follows:\n\n1. Note that following import statement has been added to the file: \n\n ```python\n import GoogleMaps\n ```\n2. Edit the following line in your `application(_:didFinishLaunchingWithOptions:)` method, replacing *YOUR_API_KEY* with your API key: \n\n ```scdoc\n GMSServices.provideAPIKey(\"YOUR_API_KEY\")\n ```\n\n| **Note:** The [sample code](https://github.com/googlemaps-samples/maps-sdk-for-ios-samples) for the completed tutorial is configured to run as a Swift Xcode project but includes Swift and Objective-C examples.\n\nBuild and run your app\n\n1. Connect an iOS device to your computer, or select a [simulator](https://developer.apple.com/library/content/documentation/IDEs/Conceptual/iOS_Simulator_Guide/Introduction/Introduction.html) from the Xcode scheme menu.\n2. If you're using a device, make sure that location services are enabled. If you're using a simulator, select a location from the **Features** menu.\n3. In Xcode, click the **Product/Run** menu option (or the play button icon).\n - Xcode builds the app, and then runs the app on the device or on the simulator.\n - You should see a map with a marker centered on Sydney on the east coast of Australia, similar to the image on this page.\n\nTroubleshooting:\n\n- If you don't see a map, check that you've obtained an API key and added it to the app, [as described previously](#add_api_key). Check Xcode's debugging console for error messages about the API key.\n- If you have restricted the API key by the iOS bundle identifier, edit the key to add the bundle identifier for the app: `com.google.examples.map-with-marker`.\n- Make sure that you have a good Wifi or GPS connection.\n- Use the [Xcode debugging tools](https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/debugging_with_xcode/chapters/debugging_tools.html) to view logs and debug the app.\n\nUnderstand the code\n\n1. Create a map and set it as the view in `viewDidLoad()`. \n\n Swift \n\n ```swift\n // Create a GMSCameraPosition that tells the map to display the\n // coordinate -33.86,151.20 at zoom level 6.\n let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)\n let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)\n view = mapView\n \n ```\n\n Objective-C \n\n ```objective-c\n // Create a GMSCameraPosition that tells the map to display the\n // coordinate -33.86,151.20 at zoom level 6.\n GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86\n longitude:151.20\n zoom:6.0];\n GMSMapView *mapView = [[GMSMapView alloc] initWithFrame: CGRectZero camera:camera];\n self.view = mapView;\n \n ```\n2. Add a marker to the map in `viewDidLoad()`. \n\n Swift \n\n ```swift\n // Creates a marker in the center of the map.\n let marker = GMSMarker()\n marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)\n marker.title = \"Sydney\"\n marker.snippet = \"Australia\"\n marker.map = mapView\n \n ```\n\n Objective-C \n\n ```objective-c\n // Creates a marker in the center of the map.\n GMSMarker *marker = [[GMSMarker alloc] init];\n marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);\n marker.title = @\"Sydney\";\n marker.snippet = @\"Australia\";\n marker.map = mapView;\n \n ```\n\nBy default, the Maps SDK for iOS displays the content of the info\nwindow when the user taps a marker. There's no need to add a click listener for\nthe marker if you're happy to use the default behavior.\n\n**Congratulations!** You've built an iOS app that displays a Google map with a\nmarker to indicate a particular location. You've also learned how to use the [Maps SDK for iOS](/maps/documentation/ios-sdk).\n\nNext steps\n\nLearn more about the [map object](/maps/documentation/ios-sdk/map), and what you\ncan do with [markers](/maps/documentation/ios-sdk/marker)."]]