A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.google.com/maps/documentation/ios-sdk/map-with-marker below:

Maps SDK for iOS | Google for Developers

Skip to main content

Stay organized with collections Save and categorize content based on your preferences.

Add a Map with a Marker

This tutorial shows how to add a Google map with a marker to your iOS app. It suits people with a beginner or intermediate knowledge of Swift or Objective-C along with general knowledge of Xcode. For an advanced guide to creating maps, read the developers' guide.

You'll create the following map using this tutorial. The marker is positioned at Sydney, Australia.

Get the code

Clone or download the Google Maps iOS samples repository on GitHub.

Alternatively, click the following button to download the source code:

Give me the code

Swift
import UIKit
import GoogleMaps

class ViewController: UIViewController {

    override func viewDidLoad() {
        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.
        let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
        let mapView = GMSMapView.map(withFrame: self.view.frame, camera: camera)
        self.view.addSubview(mapView)

        // Creates a marker in the center of the map.
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView
  }
}
      
Objective-C
#import "ViewController.h"
#import <GoogleMaps/GoogleMaps.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [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.
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                          longitude:151.20
                                                               zoom:6];
  GMSMapView *mapView = [GMSMapView mapWithFrame:self.view.frame camera:camera];
  mapView.myLocationEnabled = YES;
  [self.view addSubview:mapView];

  // Creates a marker in the center of the map.
  GMSMarker *marker = [[GMSMarker alloc] init];
  marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
  marker.title = @"Sydney";
  marker.snippet = @"Australia";
  marker.map = mapView;
}

@end
      
Get started Swift Package Manager

The Maps SDK for iOS can be installed using Swift Package Manager.

  1. Remove any existing Maps SDK for iOS dependencies.
  2. Open a terminal window and navigate to the tutorials/map-with-marker directory.
  3. Close your Xcode workspace and run the following commands:
    sudo gem install cocoapods-deintegrate cocoapods-clean
    pod deintegrate
    pod cache clean --all
    rm Podfile
    rm map-with-marker.xcworkspace
  4. Open your Xcode project and delete the podfile.
  5. Go to File > Add Package Dependencies.
  6. Enter https://github.com/googlemaps/ios-maps-sdk as the URL, press Enter to pull in the package, and click Add Package.
  7. You may need to reset your package cache using File > Packages > Reset Package Cache.
Use CocoaPods
  1. Download and install Xcode version 16.0 or later.
  2. If you don't already have CocoaPods, install it on macOS by running the following command from the terminal:
    sudo gem install cocoapods
  3. Navigate to the tutorials/map-with-marker directory.
  4. Run the pod install command. This will install the Maps SDK specified in the Podfile, along with any dependencies.
  5. 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.
  6. 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.
Get an API key and enable the necessary APIs

To complete this tutorial, you need a Google API key that's authorized to use the Maps SDK for iOS. Click the following button to get a key and activate the API.

Get Started

For more details, see Get an API key.

Add the API key to your application

Add your API key to your AppDelegate.swift as follows:

  1. Note that following import statement has been added to the file:
    import GoogleMaps
  2. Edit the following line in your application(_:didFinishLaunchingWithOptions:) method, replacing YOUR_API_KEY with your API key:
    GMSServices.provideAPIKey("YOUR_API_KEY")
Note: The sample code for the completed tutorial is configured to run as a Swift Xcode project but includes Swift and Objective-C examples. Build and run your app
  1. Connect an iOS device to your computer, or select a simulator from the Xcode scheme menu.
  2. 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.
  3. In Xcode, click the Product/Run menu option (or the play button icon).

Troubleshooting:

Understand the code
  1. Create a map and set it as the view in viewDidLoad(). Swift
    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    view = mapView
          
    Objective-C
    // Create a GMSCameraPosition that tells the map to display the
    // coordinate -33.86,151.20 at zoom level 6.
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                            longitude:151.20
                                                                 zoom:6.0];
    GMSMapView *mapView = [[GMSMapView alloc] initWithFrame: CGRectZero camera:camera];
    self.view = mapView;
          
  2. Add a marker to the map in viewDidLoad(). Swift
    // Creates a marker in the center of the map.
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView
          
    Objective-C
    // Creates a marker in the center of the map.
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
    marker.title = @"Sydney";
    marker.snippet = @"Australia";
    marker.map = mapView;
          

By default, the Maps SDK for iOS displays the content of the info window when the user taps a marker. There's no need to add a click listener for the marker if you're happy to use the default behavior.

Congratulations! You've built an iOS app that displays a Google map with a marker to indicate a particular location. You've also learned how to use the Maps SDK for iOS.

Next steps

Learn more about the map object, and what you can do with markers.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-23 UTC.

[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-07-23 UTC."],[[["This 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."],["The tutorial covers installation using Swift Package Manager or CocoaPods, obtaining and implementing an API key, and building/running the app."],["Code examples in Swift and Objective-C demonstrate creating a map view, setting camera position, and adding a marker with title and snippet."],["Troubleshooting tips address common issues like missing maps, API key errors, and connectivity problems."],["Users are encouraged to further explore the map object and marker functionalities within the Maps SDK for iOS."]]],["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"]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4