New basemap styling is coming soon to Google Maps Platform. This update to map styling includes a new default color palette, modernized pins, and improvements to map experiences and usability. All map styles will be automatically updated in March 2025. For more information on availability and how to opt in earlier, see New map style for Google Maps Platform.
Stay organized with collections
Save and categorize content based on your preferences.
The My Location button is displayed in the bottom right corner of the map
view. When the user taps the button, the map pans to the user's current
location.
Get started
Before you can try the sample code, you must configure your development environment.
For more information, see Maps SDK for iOS code samples.
View the code
Swift
importGoogleMapsimportUIKitclassMyLocationViewController:UIViewController{privateletcameraLatitude:CLLocationDegrees=-33.868privateletcameraLongitude:CLLocationDegrees=151.2086privateletcameraZoom:Float=12lazyvarmapView:GMSMapView={letcamera=GMSCameraPosition(latitude:cameraLatitude,longitude:cameraLongitude,zoom:cameraZoom)returnGMSMapView(frame:.zero,camera:camera)}()varobservation:NSKeyValueObservation?varlocation:CLLocation?{didSet{guardoldValue==nil,letfirstLocation=locationelse{return}mapView.camera=GMSCameraPosition(target:firstLocation.coordinate,zoom:14)}}overridefuncviewDidLoad(){super.viewDidLoad()mapView.delegate=selfmapView.settings.compassButton=truemapView.settings.myLocationButton=truemapView.isMyLocationEnabled=trueview=mapView// Listen to the myLocation property of GMSMapView.observation=mapView.observe(\.myLocation,options:[.new]){[weakself]mapView,_inself?.location=mapView.myLocation}}deinit{observation?.invalidate()}}extensionMyLocationViewController:GMSMapViewDelegate{funcmapView(_mapView:GMSMapView,didTapMyLocationlocation:CLLocationCoordinate2D){letalert=UIAlertController(title:"Location Tapped",message:"Current location: <\(location.latitude), \(location.longitude)>",preferredStyle:.alert)alert.addAction(UIAlertAction(title:"OK",style:.default))present(alert,animated:true)}}
#import "GoogleMapsDemos/Samples/MyLocationViewController.h"#import <GoogleMaps/GoogleMaps.h>@implementationMyLocationViewController{GMSMapView*_mapView;BOOL_firstLocationUpdate;}-(void)viewDidLoad{[superviewDidLoad];GMSCameraPosition*camera=[GMSCameraPositioncameraWithLatitude:-33.868longitude:151.2086zoom:12];_mapView=[GMSMapViewmapWithFrame:CGRectZerocamera:camera];_mapView.delegate=self;_mapView.settings.compassButton=YES;_mapView.settings.myLocationButton=YES;// Listen to the myLocation property of GMSMapView.[_mapViewaddObserver:selfforKeyPath:@"myLocation"options:NSKeyValueObservingOptionNewcontext:NULL];self.view=_mapView;// Ask for My Location data after the map has already been added to the UI.GMSMapView*mapView=_mapView;dispatch_async(dispatch_get_main_queue(),^{mapView.myLocationEnabled=YES;});}-(void)mapView:(GMSMapView*)mapViewdidTapMyLocation:(CLLocationCoordinate2D)location{NSString*message=[NSStringstringWithFormat:@"My Location Dot Tapped at: [lat: %f, lng: %f]",location.latitude,location.longitude];UIAlertController*alertController=[UIAlertControlleralertControllerWithTitle:@"Location Tapped"message:messagepreferredStyle:UIAlertControllerStyleAlert];UIAlertAction*okAction=[UIAlertActionactionWithTitle:@"OK"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*action){}];[alertControlleraddAction:okAction];[selfpresentViewController:alertControlleranimated:YEScompletion:nil];}-(void)dealloc{[_mapViewremoveObserver:selfforKeyPath:@"myLocation"context:NULL];}#pragma mark - KVO updates-(void)observeValueForKeyPath:(NSString*)keyPathofObject:(id)objectchange:(NSDictionary*)changecontext:(void*)context{if(!_firstLocationUpdate){// If the first location update has not yet been received, then jump to that location._firstLocationUpdate=YES;CLLocation*location=[changeobjectForKey:NSKeyValueChangeNewKey];_mapView.camera=[GMSCameraPositioncameraWithTarget:location.coordinatezoom:14];}}@end
The Maps SDK for iOS sample app is available as a
download archive
from GitHub.
Follow these steps to install and try the Maps SDK for iOS sample app.
Run git clone https://github.com/googlemaps-samples/maps-sdk-for-ios-samples.git
to clone the samples repository into a local directory.
Open a terminal window, navigate to the directory where you cloned the sample files, and
drill down into the GoogleMaps directory:
Swift
cd maps-sdk-for-ios-samples-main/GoogleMaps-Swift
pod installopen GoogleMapsSwiftDemos.xcworkspace
Objective-C
cd maps-sdk-for-ios-samples-main/GoogleMaps
pod installopen GoogleMapsDemos.xcworkspace
In Xcode, press the compile button to
build the app
with the current scheme. The build produces an error, prompting you to enter your API key
in the SDKConstants.swift file for Swift orSDKDemoAPIKey.h
file for Objective-C.
If you don't yet have an API key, follow the
instructions to set up
a project on the Google Cloud console and get an API key. When configuring the
key on the Cloud console, you can
restrict the key to the
sample app's bundle identifier to
ensure that only your app can use the key. The default bundle identifier of
the SDK samples app is com.example.GoogleMapsDemos.
Edit the SDKConstants.swift file for Swift orSDKDemoAPIKey.h
file for Objective-C and paste your API key into the definition of either the
apiKey or kAPIKey constant. For example:
Swift
static let apiKey = "YOUR_API_KEY"
Objective-C
staticNSString*constkAPIKey=@"YOUR_API_KEY";
In the SDKConstants.swift file (Swift) orSDKDemoAPIKey.h
file (Objective-C), remove the following line, because it's used to register the user-defined issue:
Swift
#error (Register for API Key and insert here. Then delete this line.)
Objective-C
#error Register for API Key and insert here.
Build and run the project. The iOS simulator window appears, showing
a list of Maps SDK Demos.
Choose one of the options displayed, to experiment with a feature of the
Maps SDK for iOS.
If prompted to allow GoogleMapsDemos to access your location, choose
Allow.
[[["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-02-24 UTC."],[[["The \"My Location\" button, located in the bottom right corner of the map, centers the map on the user's current location when tapped."],["The provided code samples (Swift and Objective-C) demonstrate how to implement the \"My Location\" functionality within a map view."],["To run the sample code, you'll need to configure your development environment, including obtaining and integrating a Google Maps API key."],["The full sample app can be downloaded and explored to experiment with various features of the Maps SDK for iOS."]]],["The content describes implementing a \"My Location\" feature in a map view using the Google Maps SDK for iOS. When enabled, a button appears, allowing the user to center the map on their current location upon tapping. The code examples show how to set up the map, enable the \"My Location\" button and feature, and observe changes to the user's location. Additionally, it details setting up a project and API key. It also describes setting up a tap handler on the current user's location in the map.\n"]]