Getting the Google Maps SDK for iOS
Before starting I want to tell, I have already done a example for this you can find this here...
The Google Maps SDK for iOS is distributed as a zip file containing a static framework. After downloading the SDK, you will need to obtain an API key before you can add a map to your application.
Complete release notes are available for each release.
The Google Maps API Key
Using an API key enables you to monitor your application's Maps API usage, and ensures that Google can contact you about your application if necessary. The key is free, you can use it with any of your applications that call the Maps API, and it supports an unlimited number of users. You obtain a Maps API key from the Google APIs Console by providing your application's bundle identifier. Once you have the key, you add it to your
AppDelegate
as described in the next section.
The Google Maps URL scheme is available without an API key.
Obtaining an API Key
You can obtain a key for your app in the Google APIs Console.
- Create an API project in the Google APIs Console.
- Select the Services pane in your API project, and enable the Google Maps SDK for iOS. This displays the Google Maps Terms of Service.
- Select the API Access pane in the console, and click Create new iOS key.
- Enter one or more bundle identifiers as listed in your application's .plist file, such as
com.example.myapp
. - Click Create.
- In the API Access page, locate the section Key for iOS apps (with bundle identifiers) and note or copy the 40-character API key.
You should repeat this process for each new application.
Adding the Google Maps SDK for iOS to your project
The Google Maps SDK for iOS is packaged as a static framework with an included resource bundle. Before you can add a map to your application, you will need to add the framework to your project, and configure your build settings in Xcode. These instructions assume an installation for a new project. If you are working with an existing project, you may not have to follow the steps exactly as described.
- Launch Xcode and either open an existing project, or create a new project.
- If you're new to iOS, create a Single View Application, and disable Use Storyboards but ensure that Use Automatic Reference Counting is on.
- Drag the
GoogleMaps.framework
bundle to the Frameworks group of your project. When prompted, select Copy items into destination group's folder. - Right-click
GoogleMaps.framework
in your project, and select Show In Finder. - Drag the
GoogleMaps.bundle
from theResources
folder to your project. We suggest putting it in the Frameworks group. When prompted, ensure Copy items into destination group's folder is not selected. - Select your project from the Project Navigator, and choose your application's target.
- Open the Build Phases tab, and within Link Binary with Libraries, add the following frameworks:
- AVFoundation.framework
- CoreData.framework
- CoreLocation.framework
- CoreText.framework
- GLKit.framework
- ImageIO.framework
- libc++.dylib
- libicucore.dylib
- libz.dylib
- OpenGLES.framework
- QuartzCore.framework
- SystemConfiguration.framework
- Choose your project, rather than a specific target, and open the Build Settings tab.
- In the Other Linker Flags section, add
-ObjC
. If these settings are not visible, change the filter in the Build Settings bar fromBasic to All.
- In the Other Linker Flags section, add
- Finally, add your API key to your
AppDelegate
.- #import
<GoogleMaps/GoogleMaps.h>
- Add the following to your
application:didFinishLaunchingWithOptions:
method, replacing API_KEY with your API key.[GMSServices provideAPIKey:@"API_KEY"];
- #import
Upgrade from an earlier version
To upgrade an existing project to the most recent version of the SDK, do the following:
- In the Project Navigator, replace the previous framework with the most recent framework.
- (Optional) Make any necessary changes as a result of the upgrade. Necessary changes will be described in the release notes.
- Clean and rebuild your project by selecting: Product > Clean and then Product > Build.
Add a Map
After adding the SDK to your project, and adding your key, you can try adding a map to your application. The code below demonstrates how to add a simple map to an existing
ViewController
. If you're creating a new app to iOS, first follow the installation instructions above, and create a new Single View Application; disabling Use Storyboards but enabling Use Automatic Reference Counting (ARC).
Now, add or update a few methods inside your app's default
ViewController
to create and initialize an instance of GMSMapView
.#import "YourViewController.h"
#import <GoogleMaps/GoogleMaps.h>
@implementation YourViewController {
GMSMapView *mapView_;
}
- (void)viewDidLoad {
// 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];
mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView_.myLocationEnabled = YES;
self.view = 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
Note: Replace both instances of
YourViewController
in the above example with the name of your View Controller.
Run your application. You should see a map with a single marker centered over Sydney, Australia. If you see the marker, but the map is not visible, confirm that you have provided your API key.