MapLoader-iOS
MapLoader 是一个工具,帮助您快速在 iOS 上加载谷歌和/或苹果地图。此外,该库还包含高度可定制的注释、标记和簇视图,允许您设计自己的地图 UI。
您可以下载 MapLoaderDemo 来查看其如何在您的应用程序中使用。
关键特性
- 轻松加载 iOS 上的谷歌和苹果地图
- 可定制的标记和注释
- 内置标记和注释的背景和前景图像
- 标记和注释聚类
- 支持标记和注释动画
要求
- Swift 4.0
- iOS 10.0+
用法
按照以下说明快速设置 MapView。
步骤 1: 初始化一个 MapLoader 对象
let mapLoader = MapLoader()
初始化一个 GoogleMapLoader 对象
let mapLoader = GoogleMapLoader()
注意:在使用 Google Map 之前,需要设置 API 键到 AppDelegate.swift 中,更多详情请参阅 补充资料。
步骤 2: 在 viewWillAppear() 函数中将视图插入到您的当前视图
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
mapLoader.setupMapView(mapContainer: self.view, viewAboveMap: nil, delegate: self)
}
注意:如果您需要使用 MKMapViewDelegate
,则需要设置委托。否则,可以将其留为 nil
步骤 3: 在 viewDidLayoutSubviews() 函数中调整 MapView
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
mapLoader.layoutMapView()
}
遵循以下说明以使用 聚类 和 注释/标记
步骤 4: 添加您的自定义注释
使用StyledAnnotationView进行标注视图
var annotations: [MLAnnotation] = []
let annotView = StyledAnnotationView(annotImg: .hazard, color: UIColor.white, background: .bubble, bgColor: UIColor.blue)
let annotation = MLAnnotation(coordinate: CLLocationCoordinate2D(latitude: 42.36, longitude: -71.06), annotView: annotView, data: nil)
annotations.append(annotation)
mapLoader.addAnnotations(annotations: annotations)
将对象简单地更改为MLMarker以用于Google地图
let annotation = MLMarker(coordinate: CLLocationCoordinate2D(latitude: 42.36, longitude: -71.06), annotView: annotView, data: nil)
第5步:设置MKMapViewDelegate并使用以下函数。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
return mapLoader.generateClusteringView(annotation: annotation) as? MKAnnotationView
}
第6步:当MapView区域更改时刷新标注和簇
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
return mapLoader.generateClusteringView(annotation: annotation) as? MKAnnotationView
}
对于Google地图
用户完成手势后更新地图。
func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
mapLoader.refreshMap()
}
补充
将自定义图像用作标注视图
let image = UIImage(named: "your_image")
let annotView = StyledAnnotationView(annotImg: image, background: .bubble, bgColor: UIColor.blue)
内置类似于背景图像的注释功能可用。
/**
Built-in background images.
- bubble: bubble background
- square: square-shaped background
- circle: circular background
- heart: heart-shaped background
- flag: flag background
*/
public enum BgImg {
case bubble
case square
case circle
case heart
case flag
}
在AppDelegate.swift中设置Google地图API密钥
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
GoogleMapLoader.setAPIKey("YOUR_API_KEY")
return true
}
安装
MapLoader 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile
$ pod 'MapLoader'
如果您不使用 CocoaPods,可以下载整个项目,然后拖放所有类到项目中并使用它们。