0
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)

我有两个带有两个注释的位置(纬度和经度)。我需要知道如何在 MKMap 上的这两个注释之间划清界限?

4

2 回答 2

0

创建一个 [CLLocationCoordinate2D] 数组,转换为 MKPolyline 并添加到地图。

如果你有一个 CLLocation 可以从 CLLocation.coordinate 获得 CLLocationCoordinate2D

在你的情况...

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()
}
于 2021-10-27T17:14:36.337 回答
-3

如果您有两个注释的坐标,那么只需将新的 MKPolyline 添加到地图视图中。你可以通过谷歌搜索找到几个这样的例子。

于 2016-02-04T07:55:37.087 回答