** "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
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.
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.
Click Select Project to create a new project.
After entering the project name, press the create button.
I think the project will open, so select ** Maps SDK for iOS ** from the API
APIs and Services → Select Dashboard
Click Enable API and Services
The API library screen will appear. Select ** Maps SDK for iOS **.
Click ** Enable **.
The management screen will appear. From Credentials, press "Create Credentials" and select "API Key".
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.
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.
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`.
didFinishLaunchingWithOptions
.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
googlechromesand
comgooglemaps to the Array to ʻInformation Property List
.
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
}
Let's build and run. After execution, you should see a pin at the selected location.
That's how to use the Google Maps API.
Recommended Posts