微信sdk swift版
by Dejan Atanasov
通過Dejan Atanasov
使用Swift 4的iOS版Google Maps SDK終極指南 (Your ultimate guide to the Google Maps SDK on iOS, using Swift 4)
Many iOS apps use Google Maps. This is a very common feature, so I have decided to prepare an ultimate guide on the Google Maps SDK for iOS. This tutorial covers everything that you might need to know.
許多iOS應用程序都使用Google Maps。 這是一個非常常見的功能,因此我決定在iOS版Google Maps SDK上準備最終指南。 本教程涵蓋了您可能需要了解的所有內容。
I hope that my readers will request features, so I can expand this article. Everything will be documented in this post! ?
我希望我的讀者會要求提供功能,因此我可以擴展本文。 一切都會記錄在這篇文章中! ?
安裝 (Installation)
Before we start coding, we must install the Google Maps iOS SDK first. You might prefer some other dependency manager, but I would recommend CocoaPods.
在開始編碼之前,我們必須先安裝Google Maps iOS SDK。 您可能更喜歡其他依賴管理器,但我建議使用CocoaPods 。
Create a Podfile inside your project root directory, and copy the following code:
在項目根目錄中創建Podfile,然后復制以下代碼:
source 'https://github.com/CocoaPods/Specs.git'target 'YOUR_TARGET_NAME' do pod 'GoogleMaps'end
All you need to do is changing the YOUR_TARGET_NAME string with a real value. Save the file and close it. Open the terminal and cd to the root directory of the project, then type pod install
. You're done! ?
您需要做的就是用真實值更改YOUR_TARGET_NAME字符串。 保存文件并關閉它。 打開終端,并cd到項目的根目錄,然后鍵入pod install
。 你完成了! ?
獲取API密鑰 (Get an API key)
To use the Google Maps iOS SDK, you will need an API Key. To generate the key you will need to visit the Google API Console. Create a project, and navigate to ‘Credentials’.
要使用Google Maps iOS SDK,您需要一個API密鑰。 要生成密鑰,您需要訪問Google API控制臺 。 創建一個項目,然后導航到“憑據”。
Then, click ‘Generate Credentials’ and pick API Key. You will need to provide your projects bundle id. The key is generated by the unique bundle id, so if it’s changed, the Google Maps services won’t work!
然后,點擊“生成憑據” 并選擇API密鑰。 您將需要提供您的項目包ID。 密鑰是由唯一的捆綁包ID生成的,因此,如果更改了捆綁包ID,則Google Maps服務將無法使用 !
Go to your project, and in your AppDelegate.swift
class add import GoogleMaps
. Then, copy the following code to application(_:didFinishLaunchingWithOptions:)
轉到您的項目,然后在AppDelegate.swift
類中添加import GoogleMaps
。 然后,將以下代碼復制到application(_:didFinishLaunchingWithOptions:)
GMSServices.provideAPIKey("YOUR_API_KEY")
Step 1 — Add a map
(Step 1 — Add a map
)
I will start by showing you how to set up the map together with a basic marker. The code that you will see here is tested in parallel as I write.
首先,我將向您展示如何設置地圖以及基本標記。 在編寫本文時,將并行測試您在此處看到的代碼。
Let’s start! ?
開始吧! ?
Visit your UIViewController (where you need to add the map). Create a custom UIView with the size you need. Assign the GMSMapView
class as a Custom Class to the UIView (see the screenshot below). Also, don't forget to connect the delegate.
訪問您的UIViewController(您需要在其中添加地圖)。 創建具有所需大小的自定義UIView。 將GMSMapView
類作為自定義類分配給UIView(請參見下面的屏幕截圖)。 另外,不要忘記連接代理。
最后,一些代碼! (Finally, some code!)
Let’s get back to the UIViewController and write some code. ?? ?In the below snippet, I have added the whole class so you can get a better picture of what is going on.
讓我們回到UIViewController并編寫一些代碼。 the??在下面的代碼片段中,我添加了整個課程,以便您可以更好地了解正在發生的事情。
GMSCameraPosition
tells the map which coordinates to take as a center point. To show a simple marker on the map, use the showMarker()
function.
GMSCameraPosition
告訴地圖以哪個坐標為中心。 要在地圖上顯示簡單的標記,請使用showMarker()
函數。
At the end of the file, add an extension which will “store” the GMSMapViewDelegate
methods that we need.
在文件末尾,添加一個擴展名 ,該擴展名將“存儲”我們所需的GMSMapViewDelegate
方法。
第2步-委托方法 (Step 2 — Delegate methods)
I will now introduce you to some GMSMapViewDelegate
methods and their powers. ?
現在,我將向您介紹一些GMSMapViewDelegate
方法及其功能。 ?
GMSMarker信息窗口 (GMSMarker InfoWindow)
In Google Maps, an InfoWindow is a popup window with extra information about a given location. It is displayed when the user taps on the marker we added above.
在Google Maps中,InfoWindow是一個彈出窗口,其中包含有關給定位置的其他信息。 當用戶點擊我們上面添加的標記時,將顯示它。
Our InfoWindow is customizable. You can attach your own UIView with whatever components you need.
我們的InfoWindow是可定制的。 您可以將自己的UIView與所需的任何組件連接在一起。
I have written an example implementation. This assumes in most cases people will use a custom InfoWindow,
我已經寫了一個示例實現。 假設大多數情況下,人們會使用自定義的InfoWindow,
didTapInfoWindowOf()
detects when the user taps on the InfoWindow.didTapInfoWindowOf()
檢測用戶何時didTapInfoWindowOf()
InfoWindow。markerInfoWindow()
adds the custom UIView that we want to show to the marker.markerInfoWindow()
將要顯示的自定義UIView添加到標記中。didLongPressInfoWindowOf()
detects when the InfoWindow has been long pressed.didLongPressInfoWindowOf()
檢測何時長時間按下InfoWindow。
拖動GMSMarker (Drag GMSMarker)
Another interesting feature in GMSMapViewDelegate is the ability to drag the marker. This can be achieved with a minimal amount of code.
GMSMapViewDelegate中的另一個有趣的功能是能夠拖動標記。 這可以用最少的代碼來實現。
All you have to do is turn on the “switch”, by calling marker.isDragabble=true
on the marker created above.
您所要做的就是通過在上面創建的標記上調用marker.isDragabble=true
來打開“開關”。
In order to drag the marker, you will need to use a long press. If you need to be notified when the user starts and ends dragging, you can implement these three delegate methods:
為了拖動標記,您需要長按。 如果需要在用戶開始和結束拖動時收到通知,則可以實現以下三種委托方法:
didBeginDragging
notifies once — when the dragging has started.didBeginDragging
通知一次-拖動開始時。didDrag
notifies while the marker is being dragged.在拖動標記時,
didDrag
會通知。didEndDragging
notifies once — when the dragging has ended.didEndDragging
通知一次-拖動結束時。
GMSMarker位置 (GMSMarker position)
What if you need to change the GMSMarker
position while the user is tapping on the map? Well, GMSMapViewDelegate
offers a solution for that as well. A single delegate method can intercept the coordinates (latitude and longitude) of the tapped area. It will then assign their values to the marker.
如果在用戶點擊地圖時需要更改GMSMarker
位置怎么辦? 好吧, GMSMapViewDelegate
為此提供了一個解決方案。 單個委托方法可以攔截點擊區域的坐標(緯度和經度)。 然后它將它們的值分配給標記。
didTapAt()
returns the coordinate from the tapped area on the mapdidTapAt()
返回地圖上點擊區域的坐標
第3步-添加形狀 (Step 3 — Adding shapes)
The Google Maps iOS SDK makes it simple to draw a shape. I will cover how to draw with polylines, polygons and circles.
Google Maps iOS SDK使繪制形狀變得簡單。 我將介紹如何使用折線,多邊形和圓形進行繪制。
折線 (Polylines)
Shapes can be built using lines. We can draw lines in Google Maps using ‘polylines’. The object that will help us with the drawing is called GMSPolyline
.
可以使用線來構建形狀。 我們可以使用“折線”在Google地圖中繪制線。 將幫助我們進行繪制的對象稱為GMSPolyline
。
To create a polyline, you will need to create a path using GMSMutablePath
. It needs two or more points to start creating a path.
要創建折線,您將需要使用GMSMutablePath
創建路徑。 它需要兩個或多個點才能開始創建路徑。
If you have used the above example, you will get a rectangular shape like the one shown.
如果您使用了上面的示例,則將得到一個如圖所示的矩形。
Some other useful tips:
其他一些有用的提示:
To remove a polyline from the map, call
mapView.clear()
.要從地圖中刪除折線,請調用
mapView.clear()
。You can change the color of the line by using
polyline.strokeColor=.black
.您可以使用
polyline.strokeColor=.black
更改線條的顏色。Change the width of the line by calling
polyline.strokeWidth=3
.通過調用
polyline.strokeWidth=3
更改線的寬度。
多邊形 (Polygon)
Polygon is almost the same as polylines. It works using the same approach, with a few minor differences.
多邊形與折線幾乎相同。 它使用相同的方法工作,但有一些細微的差異。
For example, GMSPolygon
will draw a shape. You can then use fillColor
to fill in the drawn area. Here is an example of what this looks like.
例如, GMSPolygon
將繪制形狀。 然后,您可以使用fillColor
填充繪制的區域。 這是一個看起來像的例子。
半徑(圓) (Radius (circle))
The final shape we will look at is a circle. This is probably the easiest shape of all, since it’s always the same!
我們將看到的最終形狀是一個圓形。 這可能是所有形狀中最簡單的形狀,因為它總是一樣!
To achieve this, we need to use theGMSCircle
class. Here, we are not passing a path. Instead, we use one coordinate to specify the circle’s center. We also define a radius (measured in meters).
為此,我們需要使用GMSCircle
類。 在這里,我們沒有通過路徑。 相反,我們使用一個坐標來指定圓的中心。 我們還定義了半徑(以米為單位)。
TheGMSCircle
class contains the same properties as the polygon, includingfillColor
, strokeColor
and strokeWidth
.
GMSCircle
類包含與多邊形相同的屬性,包括fillColor
, strokeColor
和strokeWidth
。
第4步-屬性和設置 (Step 4 — Properties and Settings)
This part will cover a few properties and settings that are often used when using Google Maps in your app. Let’s take a look at them.
本部分將介紹一些在應用程序中使用Google Maps時經常使用的屬性和設置。 讓我們看看它們。
更改標記圖標 (Change marker icon)
The GMSMarker
contains two different properties for changing the marker icon.
GMSMarker
包含兩個用于更改標記圖標的不同屬性。
marker.icon=UIImage(named: "image.png")
in this approach, you pass an image filename. This replaces the default one.marker.icon=UIImage(named: "image.png")
在這種方法中,您傳遞圖像文件名。 這將替換默認值。marker.iconView=customView
You can also add a custom view instead of an image. This can be used for more complex markers. For example, you may want to add some animation, or multiple components (instead of a single image). Note theicon
property gets overwritten wheniconView
is called.marker.iconView=customView
您也可以添加自定義視圖而不是圖像。 這可以用于更復雜的標記。 例如,您可能想要添加一些動畫或多個組件(而不是單個圖像)。 請注意,調用iconView
時icon
屬性將被覆蓋。
添加“我的位置”按鈕 (Add ‘My Location’ button)
The ‘My Location’ button appears in the bottom right corner. Clicking the button will animate the map to show the user’s current location.
“我的位置”按鈕顯示在右下角。 單擊該按鈕將為地圖設置動畫以顯示用戶的當前位置。
To add this, set mapView.settings.myLocationButton = true
. The button will appear.
要添加此內容,請設置mapView.settings.myLocationButton = true
。 該按鈕將出現。
縮放控制 (Zoom controls)
Google Maps SDK for iOS doesn’t provide inbuilt zoom controls (but the Android SDK does). You will need to write your own logic instead.
適用于iOS的Google Maps SDK不提供內置的縮放控件(但Android SDK提供)。 您將需要編寫自己的邏輯。
All you need to do is add two buttons with ‘+’ and ‘-’ icons. When tapped, these will call mapView.animate(toZoom: zoom)
.
您需要做的就是添加兩個帶有“ +”和“-”圖標的按鈕。 點擊時,它們將調用mapView.animate(toZoom: zoom)
。
控制手勢 (Control gestures)
You can turn on or off any gesture that you can see on the map. For example, you might want to disable zooming, or turn off scrolling.
您可以打開或關閉在地圖上可以看到的任何手勢。 例如,您可能要禁用縮放或關閉滾動。
There are a total of four gestures available to you:
您總共可以使用四種手勢:
mapView.settings.scrollGestures = falsemapView.settings.zoomGestures = falsemapView.settings.tiltGestures = falsemapView.settings.rotateGestures = false
I hope that you have enjoyed this tutorial. If you want to read more on Google Maps SDK for iOS, write me a comment. I would be very happy to expand this tutorial with your requests.
希望您喜歡本教程。 如果您想了解有關iOS版Google Maps SDK的更多信息,請給我評論。 我很高興根據您的要求擴展本教程。
這是本教程中的內容,如果對您有幫助,請? 或分享這個故事,以便其他人可以找到它。 感謝您的關注! ? (That’s it from this tutorial and if it helped you please ? or share this story so others like you can find it. Thank you for your attention! ?)
查看我的最新項目: (Check out my latest project:)
?1x2 BET - Soccer Tips & Odds?HOT ODDS Each day, we generate a list of the hottest odds in the world. These are odds that have dropped the most…apple.co
1x2賭注-足球技巧和賠率 熱門 賠率 每天,我們生成世界上最熱門賠率的列表。 這些賠率跌幅最大…… apple.co
閱讀我在Medium上撰寫的更多文章: (Read more of my writing on Medium:)
Introducing Clean Swift Architecture (VIP)Forget MVC, now!hackernoon.comYour ultimate guide to the Google Maps SDK on iOS, using Swift 4Many iOS apps use Google Maps. This is a very common feature, so I have decided to prepare an ultimate guide on the…medium.freecodecamp.orgSWIFT — Custom UIView with XIB fileCustom UIView with XIB file is a very common practice in iOS Development. Custom UIView classes don’t contain XIB files…medium.comHow to add Spotlight support to your iOS appA Swift tutorial that will make your app available in Spotlight searchhackernoon.comCore Data RelationshipsUnderstanding One-to-One and One-To-Many relationshipshackernoon.comUnderstanding Auto Layout in Xcode 9All you need to know about Auto Layouthackernoon.com
現在介紹Clean Swift Architecture(VIP) 忘記MVC! hackernoon.com 使用Swift 4的 iOS 上Google Maps SDK的終極指南 許多iOS應用程序都使用Google Maps。 這是一個非常常見的功能,因此,我決定在...上準備一個最終指南 。medium.freecodecamp.org SWIFT —帶有XIB文件的 自 定義UIView是帶有XIB文件的 自定義UIView在iOS開發中很常見。 自定義UIView類不包含XIB文件… medium.com 如何向iOS應用程序添加Spotlight支持 一個Swift教程,可使您的應用程序在Spotlight搜索中 hackernoon.com 核心數據關系 了解一對一和一對一-許多關系 hackernoon.com 了解Xcode 9中的自動版式 您需要了解的有關自動版式 hackernoon.com的所有信息
訂閱我的時事通訊: (Subscribe to my Newsletter:)
翻譯自: https://www.freecodecamp.org/news/how-you-can-use-the-google-maps-sdk-with-ios-using-swift-4-a9bba26d9c4d/
微信sdk swift版