[SWIFT] The most polite way to use the Google Maps SDK for iOS

** "Display Map !!" **

… And, as a starting line for creating somehow iOS apps in earnest, I first learned how to use the API.

There are many tutorials for the Maps SDK for iOS, but I couldn't find a very polite article for beginners who don't know right or left, so I will introduce them step by step as well as output.

※Development environment Xcode 12 Swift 5.3

What is API? API is an abbreviation for Application Programming Interface.

About API

API is a convention that defines the procedure and data format for calling and using the functions of a certain computer program (software) and the data to be managed from another external program.

(I see, I don't know ...!)

To put it briefly, the API is ** the function of bridging the interaction between software **. Many companies, such as Google and Twitter, offer APIs. An API is an instruction or function that allows another program to call the function (service) that you want to use.

Set to use Google Map

I want to use Google's Map API (iOS), so I will set it.

Jump to Google Maps Platform Go to Console at the top right of the screen.

image_01.png

Click Select Project to create a new project.

image_02.png

After entering the project name, press the create button.

image_03.png

I think the project will open, so select ** Maps SDK for iOS ** from the API

* If you have transitioned to home

image_04.png

APIs and Services → Select Dashboard

image_05.png

Click Enable API and Services

image_06.png image_08_1.png

The API library screen will appear. Select ** Maps SDK for iOS **.

image_07.png

Click ** Enable **.

image_08.png

The management screen will appear. From Credentials, press "Create Credentials" and select "API Key".

image_09.png

The API key is now created. However, the setting is not finished yet. You will be warned that there are no restrictions on the keys, so we will apply the restrictions. Select and set the target API key.

image_10.png

Select the ** Maps SDK for iOS ** you want to use this time from the API key restrictions and rename, check and save.

This completes the settings for using the Google Maps API.

Use API Key

Create a project from Xcode, put the following in the Podfile and pod install.

Podfile


target 'Project name' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!
  source 'https://github.com/CocoaPods/Specs.git' #add to
  pod 'GoogleMaps' #add to
  pod 'GooglePlaces' #add to
  #Pods for project name

end

Next, set the API Key in AppDelegate. Open AppDelegate.swift and add ʻimport Google Maps`.

AppDelegate.swift


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        #Set the issued API key
        GMSServices.provideAPIKey("The issued API key is listed here")
        
        return true
    }

Then go to ʻInfo.plist and add googlechromesandcomgooglemaps to the Array to ʻInformation Property List. image_11.png

Open ViewController.swift and add import.

import GoogleMaps import CoreLocation

Write the following code in viewDidLoad.

ViewController.swift


 override func viewDidLoad() {
        super.viewDidLoad()
        #Generate the coordinate position of the map to be displayed and the size to be displayed
        let camera = GMSCameraPosition.camera(withLatitude: 34.6862, 
                                                                                  longitude: 135.5196, zoom: 6.0)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        mapView.isMyLocationEnabled = true #Enable current location information
        view = mapView #UIView instance
        
        #Put a pin at the designated place
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: 34.6862,
                                                                                    longitude: 135.5196)#Set latitude and longitude
        marker.title = "Osaka"
        marker.snippet = "Japan"
        marker.map = mapView
    }

Start

Let's build and run. After execution, you should see a pin at the selected location.

image_12.png

That's how to use the Google Maps API.

Recommended Posts

The most polite way to use the Google Maps SDK for iOS
The strongest way to use MeCab and CaboCha with Google Colab
The fastest way for beginners to master Python
How to use MkDocs for the first time
How to use the Google Cloud Translation API
The easiest way to use OpenCV with python
[python] How to use the library Matplotlib for drawing graphs
[Hyperledger Iroha] Notes on how to use the Python SDK
I didn't know how to use the [python] for statement
Tips for Python beginners to use the Scikit-image example for themselves
How to use the generator
How to use the decorator
How to use Google Colaboratory
How to change the log level of Azure SDK for Python
[Introduction to Python] How to use the in operator in a for statement?
How to use machine learning for work? 01_ Understand the purpose of machine learning
Script for backing up folders on the server to Google Drive
How to use the zip function
How to use the optparse module
[Memo] How to use Google MµG
Customize infoWindow for google maps for Android
The fastest way to try EfficientNet
How to use the ConfigParser module
The easiest way to make Flask
The easiest way to try PyQtGraph
Use Docker Desktop for Windows to start the latest odo with 2 commands
Tips for Python beginners to use the Scikit-image example for themselves 9 Use from C
I want to use the Qore SDK to predict the success of NBA players
Tips for Python beginners to use the Scikit-image example for themselves 6 Improve Python code