RxGoogleMaps
RxGoogleMaps是RxSwift的GoogleMaps包装器。
示例用法
配置GoogleMaps
// Setup API Key
GMSServices.provideAPIKey("Your-API-Key")
// Setup GMSMapview from Interface Builder
@IBOutlet weak var mapView: GMSMapView!
或者
// Setup GMSMapview
let mapView = GMSMapView(frame: self.view.bounds)
self.view.addSubview(mapView)
监听属性
// Camera position
mapView.rx.didChange.asDriver()
.drive(onNext: { print("Did change position: \($0)") })
.disposed(by: disposeBag)
// Marker tapped
mapView.rx.didTapAt.asDriver()
.drive(onNext: { print("Did tap at coordinate: \($0)") })
.disposed(by: disposeBag)
// Location update
mapView.rx.myLocation
.subscribe(onNext: { location in
if let l = location {
print("My location: (\(l.coordinate.latitude), \(l.coordinate.longitude))")
} else {
print("My location: nil")
}
})
.disposed(by: disposeBag)
绑定属性
// Properties
button.rx.tap
.map { false }
.bind(to: mapView.rx.trafficEnabled.asObserver())
.disposed(by: disposeBag)
// Camera animations
button.rx.tap
.map { 14 }
.bind(to: mapView.rx.zoomToAnimate)
.disposed(by: disposeBag)
button.rx.tap
.map { GMSCameraPosition.camera(withLatitude: latitude, longitude: longitude, zoom: 8, bearing: 10, viewingAngle: 30) }
.bind(to: mapView.rx.cameraToAnimate)
.disposed(by: disposeBag)
// Selected marker
button.rx.tap
.map { myMarker }
.bind(to: mapView.rx.selectedMarker.asObserver())
.disposed(by: disposeBag)
// GMSMarker or GMSOverlay properties
button.rx.tap
.map{ 180.0 }
.bind(to: marker.rx.rotation.asObserver())
.disposed(by: disposeBag)
button.rx.tap
.map{ UIColor.red }
.bind(to: circle.rx.fillColor.asObserver())
.disposed(by: disposeBag)
带有返回值的委托
// func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool
mapView.rx.handleTapMarker { false }
// func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView?
mapView.rx.handleMarkerInfoWindow { marker in
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 180, height: 60))
label.textAlignment = .center
label.textColor = UIColor.brown
label.font = UIFont.boldSystemFont(ofSize: 16)
label.backgroundColor = UIColor.yellow
label.text = marker.title
return label
}
注意:更多示例可以在本仓库的示例项目中找到!。
安装
CocoaPods
注意:由于Google Maps是以静态库的形式提供的,你必须安装CocoaPods 1.4.0才能安装这个库。
- 添加到你的
Podfile
pod 'RxGoogleMaps'
然后运行 pod install
,你应该就可以正常使用了!
示例项目
- 在自己的API Key https://developers.google.com/maps/documentation/ios-sdk/ 上获取一个密钥
- 打开示例项目,并在AppDelegate中设置你的API Key
要求
许可
MIT