Swift Google Maps API
在 Swift 中使用 Google Maps Web Service APIs
由于官方 GooglePlaces
pod 即将推出,GooglePlaces
已更名为 GooglePlacesAPI
。
功能
需求
- iOS 9.0+ / Mac OS X 10.11+ / tvOS 9.0+ / watchOS 2.0+
- Xcode 8.0+
- Swift 3.0+
安装
CocoaPods
-
Google Maps Directions API
use_frameworks! pod 'GoogleMapsDirections', '~> 1.1'
-
Google 地点 API
use_frameworks! pod 'GooglePlacesAPI', '~> 1.1'
使用
- Google Maps Directions API
import GoogleMapsDirections GoogleMapsDirections.provide(apiKey: "A VALID GOOGLE MAPS KEY") let origin = GoogleMapsDirections.Place.stringDescription(address: "Davis Center, Waterloo, Canada") let destination = GoogleMapsDirections.Place.stringDescription(address: "Conestoga Mall, Waterloo, Canada") // You can also use coordinates or placeID for a place // let origin = Place.Coordinate(coordinate: LocationCoordinate2D(latitude: 43.4697354, longitude: -80.5397377)) // let origin = Place.PlaceID(id: "ChIJb9sw59k0K4gRZZlYrnOomfc") GoogleMapsDirections.direction(fromOrigin: origin, toDestination: destination) { (response, error) -> Void in // Check Status Code guard response?.status == GoogleMapsDirections.StatusCode.ok else { // Status Code is Not OK debugPrint(response?.errorMessage) return } // Use .result or .geocodedWaypoints to access response details // response will have same structure as what Google Maps Directions API returns debugPrint("it has \(response?.routes.count ?? 0) routes") }
- Google 地点 API
- 地点自动完成
import GooglePlacesAPI GooglePlacesAPI.provide(apiKey: "A VALID GOOGLE MAPS KEY") GooglePlacesAPI.placeAutocomplete(forInput: "Pub") { (response, error) -> Void in // Check Status Code guard response?.status == GooglePlaces.StatusCode.ok else { // Status Code is Not OK debugPrint(response?.errorMessage) return } // Use .predictions to access response details debugPrint("first matched result: \(response?.predictions.first?.description)") }
- 地点详情
import GooglePlacesAPI GooglePlacesAPI.provide(apiKey: "A VALID GOOGLE MAPS KEY") GooglePlacesAPI.placeDetails(forPlaceID: "ChIJb9sw59k0K4gRZZlYrnOomfc") { (response, error) -> Void in // Check Status Code guard response?.status == GooglePlaces.StatusCode.ok else { // Status Code is Not OK debugPrint(response?.errorMessage) return } // Use .result to access response details debugPrint("the formated address is: \(response?.result?.formattedAddress)") }
- 地点自动完成
许可
MIT 许可协议 (MIT)
版权所有 © 2015 张宏昊 (张宏昊)
在此特此授予任何人,免费使用此软件(“软件”)和相关文档(“文件”)的复制件的任何形式的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售副本来此软件,并允许将软件提供给人,壳软件能在以下条件下执行上述操作:
上述版权声明和本许可声明应包含在此软件的所有副本或实质性部分中。
本软件按“原样”提供,不提供任何形式的保证,无论是明示的还是隐含的,包括但不限于适销性、针对特定目的的适用性和非侵权性。在任何情况下,作者或版权所有人不对任何索赔、损害或其他责任承担任何责任,无论起源于合同、侵权或任何其他行为,与软件有关,或与使用或依赖软件有关。