微信sdk swift版_使用Swift 4的iOS版Google Maps SDK終極指南

微信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=trueon 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 map

    didTapAt()返回地圖上點擊區域的坐標

第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, GMSPolygonwill 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類包含與多邊形相同的屬性,包括fillColorstrokeColorstrokeWidth

第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 GMSMarkercontains 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 the icon property gets overwritten when iconView is called.

    marker.iconView=customView您也可以添加自定義視圖而不是圖像。 這可以用于更復雜的標記。 例如,您可能想要添加一些動畫或多個組件(而不是單個圖像)。 請注意,調用iconViewicon屬性將被覆蓋。

添加“我的位置”按鈕 (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版

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/395425.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395425.shtml
英文地址,請注明出處:http://en.pswp.cn/news/395425.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

精確覆蓋DLX算法模板

代碼 struct DLX {int n,id;int L[maxn],R[maxn],U[maxn],D[maxn];int C[maxn],S[maxn],loc[maxn][2];void init(int nn0) //傳列長{nnn;for(int i0;i<n;i) U[i]D[i]i,L[i]i-1,R[i]i1;L[0]n; R[n]0;idn;memset(S,0,sizeof(S));}void AddRow(int x,int col,int A[]) //傳入參…

android 代碼布局設置wrap_content,android ScrollView布局(wrap_content,最大大小)

我最后編寫了自己的類,擴展了ScrollView既然你問……這是代碼.可能不是最干凈但它做我想要的.請注意,它期望在創建視圖時設置layout_weight,并且不應在父LinearLayout中設置weigthSum,否則你會得到有趣的東西(因為這個的權重從原始值變為0,具體取決于大小ScrollView的內容)首先…

ABAP數據類型

數據類型表&#xff1a; 類型縮寫 類型 默認長度 允許長度 初始值 描述 C 文本型 1 Space 字符串數據,如Program D 日期型 8 8 00000000 日期數據,格式為YYYYMMDD F 浮點型 8 8 0 浮點數 I 整型 4 10 0 帶正負符號的整數 N 數值型 1 31 00……

cocos2d-x C++ 原始工程引擎運行機制解析

新建一個工程&#xff0c;相信感興趣的同學都想知道cocos引擎都是如何運行的 想知道是如何運行的&#xff0c;看懂四個文件即可 話不多說&#xff0c;上代碼&#xff1a; 1、首先解釋 AppDelegate.h 1 #ifndef _APP_DELEGATE_H_2 #define _APP_DELEGATE_H_3 4 #include "…

web高德maker動畫_Web Maker —我如何構建一個快速的離線前端游樂場

web高德maker動畫by kushagra gour由kushagra gour Web Maker —我如何構建一個快速的離線前端游樂場 (Web Maker — How I built a fast, offline front-end playground) Web Maker is a Chrome extension that gives you a blazing fast and offline front-end playground —…

時間小知識對于時間轉換可能有幫助

那么UTC與世界各地的時間應如何換算呢?它是將全世界分為24個時區&#xff0c;地球的東、西經各180(共360)被24個時區平分&#xff0c;每個時區各占15。以經度0(即本初子午線)為基準&#xff0c;東經730′與西經730′之間的區域為零時區&#xff1b;東經和西經的730′與2230′之…

JS——實現短信驗證碼的倒計時功能(沒有驗證碼,只有倒計時)

1、功能描述 當用戶想要獲取驗證碼時&#xff0c;就點擊 免費獲取驗證碼 &#xff0c;然后開始倒計時&#xff0c;倒計時期間按鈕文字為剩余時間x秒&#xff0c;且不可按狀態&#xff0c;倒計時結束后&#xff0c;按鈕更改為點擊重新發送。 2、分析 必須用到定時器。按鈕點擊后…

華為OV小米鴻蒙,華為鴻蒙開源,小米OV們會采用嗎?

華為曾一直聲言不會進入電視市場,由此其他國產電視企業才會采用華為的可見企業是非常擔憂同業競爭關系的,而在智能手機市場,華為毫無疑問與其他國產手機企業都是競爭對手,更何況就在2019年下半年和2020年上半年華為在國內手機市場的份額超過四成直逼五成,其他國產手機企業被壓得…

第22天:如何使用OpenAI Gym和Universe構建AI游戲機器人

by Harini Janakiraman通過哈里尼賈納基拉曼 第22天&#xff1a;如何使用OpenAI Gym和Universe構建AI游戲機器人 (Day 22: How to build an AI Game Bot using OpenAI Gym and Universe) Let’s face it, AI is everywhere. A face-off battle is unfolding between Elon Musk…

軟件測試基礎理論(總結)

1&#xff0e; 軟件的三個要素&#xff1a;程序&#xff08;實行特定功能的代碼&#xff09; 文檔&#xff08;支持代碼運行&#xff09; 數據&#xff08;支持程序運行一切有關&#xff09; 2&#xff0e; 軟件的產品質量 指的是&#xff1f; 1&#xff09;質量是指實體特性…

android studio 7200u,#本站首曬# 多圖殺貓 華為MateBook X上手體驗

#本站首曬# 多圖殺貓 華為MateBook X上手體驗2017-06-09 18:45:4437點贊33收藏78評論前幾天華為開了個發布會&#xff0c;帶來了三款筆記本電腦&#xff0c;有幸在第一時間借到了MateBook X&#xff0c;現在就來來做一個簡單的上手&#xff0c;稍晚一些再跟大家詳細聊聊使用起來…

svn強制解鎖的幾種做法

標簽&#xff1a; svn強制解鎖2013-12-16 17:40 12953人閱讀 評論(0) 收藏 舉報分類&#xff1a;SoftwareProject&#xff08;23&#xff09; 版權聲明&#xff1a;本文為博主原創文章&#xff0c;未經博主允許不得轉載。 作者&#xff1a;朱金燦 來源&#xff1a;http://blog.…

數據結構和算法練習網站_視頻和練習介紹了10種常見數據結構

數據結構和算法練習網站“Bad programmers worry about the code. Good programmers worry about data structures and their relationships.” — Linus Torvalds, creator of Linux“糟糕的程序員擔心代碼。 好的程序員擔心數據結構及其關系。” — Linux的創建者Linus Torva…

突然討厭做前端,討厭代碼_有關互聯網用戶最討厭的廣告類型的新數據

突然討厭做前端,討厭代碼You know that feeling when you’re scrolling through a blog post and then — BAM! — one of those “Sign up for our newsletter” modals pops up?當您滾動瀏覽博客文章&#xff0c;然后-BAM時&#xff0c;您就會知道這種感覺。 -彈出“注冊我…

iOS設計模式-生成器

定義&#xff1a;將一個產品的內部表象與產品的生成過程分割開來&#xff0c;從而可以使一個建造過程生成具有不同的內部表象的產品對象。 類型&#xff1a;對象創建 類圖&#xff1a; #import <Foundation/Foundation.h> interface Character : NSObject property(nonat…

《Android 應用案例開發大全(第二版)》——導讀

本節書摘來自異步社區《Android 應用案例開發大全&#xff08;第二版&#xff09;》一書中的目錄 &#xff0c;作者 吳亞峰 , 于復興 , 杜化美&#xff0c;更多章節內容可以訪問云棲社區“異步社區”公眾號查看 目 錄 第1章 初識廬山真面目——Android簡介 1.1 Android的誕生 1…

模塊--sys模塊

sys模塊是與python解釋器交互的一個接口 import sys sys.path #python解釋器找模塊的環境變量import sys print(sys.path)結果:[H:\\王文靜\\python\\4練習\\課堂練習, H:\\王文靜\\python, C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\pyth…

匿名方法

與前面的可空類型是一樣的&#xff0c;匿名方法也是C# 2.0里面提出來的。 1 匿名方法 1.1 什么是匿名方法&#xff1f; 顧名思義&#xff0c;就是沒有名稱的方法&#xff0c;因為沒有名稱&#xff0c;匿名方法只能在函數定義&#xff08;匿名方法是把方法的實現和定義嵌套在了一…

使用React,Redux和Router進行真正的集成測試

by Marcelo Lotif通過馬塞洛洛蒂夫(Marcelo Lotif) 使用React&#xff0c;Redux和Router進行真正的集成測試 (Real integration tests with React, Redux and Router) After being bitten a couple of times by bad refactoring and a broken app?—?even with all my tests…

Go語言從入門到精通 - 數據類型轉換

本節核心內容 介紹 Go語言數據類型轉換的格式介紹 數據轉換代碼示例介紹 數據轉換過程中的注意事項 本小節視頻教程和代碼&#xff1a;百度網盤 可先下載視頻和源碼到本地&#xff0c;邊看視頻邊結合源碼理解后續內容&#xff0c;邊學邊練。 Go語言數據類型轉換 Go 語言使用類型…