简体   繁体   English

如何在 map 视图 swift ios 中的两个注释之间画线?

[英]How to draw line between two annotations in map view swift ios?

let latitude = NSUserDefaults.standardUserDefaults().doubleForKey(klat)
let longitude = NSUserDefaults.standardUserDefaults().doubleForKey(klong)

let location = CLLocationCoordinate2DMake(latitude, longitude)

// Second Location lat and long
let latitudeSec: CLLocationDegrees = 10.0100
let longitudeSec: CLLocationDegrees = 76.3620

let locationSec = CLLocationCoordinate2DMake(latitudeSec, longitudeSec)
let span = MKCoordinateSpanMake(1, 1)
let region = MKCoordinateRegionMake(location, span)

mapView.setRegion(region, animated: true)

I have two locations(lat and long) with two annotations.我有两个带有两个注释的位置(纬度和经度)。 I need to know how to draw the line between these two annonations on MKMap?我需要知道如何在 MKMap 上划清这两个注释之间的界限?

Create an array of [CLLocationCoordinate2D], convert to MKPolyline and add to map.创建一个 [CLLocationCoordinate2D] 数组,转换为 MKPolyline 并添加到地图。

If you have a CLLocation could get CLLocationCoordinate2D from CLLocation.coordinate如果你有一个 CLLocation 可以从 CLLocation.coordinate 得到 CLLocationCoordinate2D

In your case...在你的情况...

let pointArry = [location, locationSec]  
let myPolyline = MKPolyline(coordinates: pointArray, count: pointArray.count)
mapView.addOverlay(myPolyline)

//MARK:  MKMapViewDelegate Method (make sure class has been set as delegate)
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if overlay.isKind(of: MKPolyline.self) {
        // draw the track
        let polyLine = overlay
        let polyLineRenderer = MKPolylineRenderer(overlay: polyLine)
        polyLineRenderer.strokeColor = UIColor.blue
        polyLineRenderer.lineWidth = 2.0
        
        return polyLineRenderer
    }
    
    return MKPolylineRenderer()
}

If you have both annotations' coordinates then just add a new MKPolyline to the map view. 如果您同时具有两个注释的坐标,则只需向地图视图添加一个新的MKPolyline。 You can find several examples of this by googling. 您可以通过谷歌搜索找到几个示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM