1 mapkit
Xcode 11.1、斯威夫特 4
如何在 Swift 中使用 Mapkit 在三个位置(点)之间画一条线?
let london = Capital(title: "London", coordinate: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), info: "Home to the 2012 Summer Olympics.")
let oslo = Capital(title: "Oslo", coordinate: CLLocationCoordinate2D(latitude: 59.95, longitude: 10.75), info: "Founded over a thousand years ago.")
Run Code Online (Sandbox Code Playgroud)
谢谢
小智 5
要连接任意数量的点,您可以使用MKPolyline
它及其MKPolylineRenderer
视图。首先,将叠加层添加到地图中,然后在委托方法中为视图提供折线的视觉设置:
import UIKit
import MapKit
struct Capital {
let title: String
let coordinate: CLLocationCoordinate2D
let info: String
}
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
let london = Capital(title: "London", coordinate: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), info: "Home to the 2012 Summer Olympics.")
let oslo = Capital(title: "Oslo", coordinate: CLLocationCoordinate2D(latitude: 59.95, longitude: 10.75), info: "Founded over a thousand years ago.")
mapView.addOverlay(MKPolyline(coordinates: [london.coordinate, oslo.coordinate], count: 2))
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let polylineRenderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
polylineRenderer.strokeColor = UIColor.black
polylineRenderer.lineWidth = 4.0
return polylineRenderer
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
649 次 |
最近记录: |