# Add a Map with a Marker

[Video](https://www.youtube.com/watch?v=tmqwGK1h0iQ)

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.

![A screenshot showing a map with a marker over Sydney](https://developers.google.com/static/maps/documentation/ios-sdk/images/map-with-marker.png)

## Get the code

Clone or download the
[Google Maps iOS samples repository](https://github.com/googlemaps-samples/maps-sdk-for-ios-samples) on GitHub.

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

[Give me the code](https://github.com/googlemaps-samples/maps-sdk-for-ios-samples/archive/main.zip)


### Swift

```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 options = GMSMapViewOptions()
        options.camera = camera
        options.frame = self.view.frame
        let mapView = GMSMapView(options: options)
        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

```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];
  GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init];
  options.camera = camera;
  options.frame = self.view.frame;
  GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options];
  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
      
```

<br />

## Get started

### Swift Package Manager

The Maps SDK for iOS can be installed using [Swift Package Manager](https://developer.apple.com/documentation/xcode/swift-packages).

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:

   ```swift
   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**.

## 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](https://cloud.google.com/maps-platform/#get-started)

For more details, see
[Get an API key](https://developers.google.com/maps/documentation/ios-sdk/get-api-key.md.txt).

## 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]
> **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.

## Build and run your app

1. 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.
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).
   - Xcode builds the app, and then runs the app on the device or on the simulator.
   - You should see a map with a marker centered on Sydney on the east coast of Australia, similar to the image on this page.

Troubleshooting:

- If you don't see a map, check that you've obtained an API key and added it to the app, [as described previously](https://developers.google.com/maps/documentation/ios-sdk/map-with-marker.md.txt#add_api_key). Check Xcode's debugging console for error messages about the API key.
- 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`.
- Make sure that you have a good Wifi or GPS connection.
- 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.

## Understand the code

1. Create a map and set it as the view in `viewDidLoad()`.

   ### Swift

   ```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 options = GMSMapViewOptions()
   options.camera = camera
   let mapView = GMSMapView(options: options)
   view = mapView
         
   ```

   ### Objective-C

   ```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];
   GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init];
   options.camera = camera;
   GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options];
   self.view = mapView;
         
   ```
2. Add a marker to the map in `viewDidLoad()`.

   ### Swift

   ```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

   ```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](https://developers.google.com/maps/documentation/ios-sdk).

## Next steps

Learn more about the [map object](https://developers.google.com/maps/documentation/ios-sdk/map.md.txt), and what you
can do with [markers](https://developers.google.com/maps/documentation/ios-sdk/marker.md.txt).