【问题标题】:How do i connect the two annotation points?如何连接两个注释点?
【发布时间】:2016-03-17 14:58:19
【问题描述】:

我传递了 fromAddress 文本字段和 toAddress 文本字段的两个值,我需要像路由一样连接两个注释点。我不需要任何基于 api 的指示。只需连接即可。

import UIKit
import MapKit
import CoreLocation
import Contacts

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var fromAddress: UITextField!
@IBOutlet weak var toAddress: UITextField!
@IBOutlet weak var map: MKMapView!

let locationManager = CLLocationManager()
var location = CLLocation!.self

@IBAction func getDirection(sender: AnyObject) {

    ConnectingFromAndTo(fromAddress.text!)
    ConnectingFromAndTo(toAddress.text!)
 }


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.}
}

func ConnectingFromAndTo(address: String){

    CLGeocoder().geocodeAddressString(address, completionHandler:
        {(placemarks, error) in

            if error != nil {
                print("Geocode failed with error: \(error!.localizedDescription)")
            } else if placemarks!.count > 0 {
                let placemark = placemarks![0]
                let location = placemark.location


                let annotation = MKPointAnnotation()
                annotation.coordinate = location!.coordinate
                annotation.title = "\(placemark.locality!)"
                annotation.subtitle = "\(placemark.country!)"
                self.map.addAnnotation(annotation)

                self.map.setRegion(MKCoordinateRegionMake(CLLocationCoordinate2DMake (placemark.location!.coordinate.latitude, placemark.location!.coordinate.longitude), MKCoordinateSpanMake(0.002, 0.002)), animated: true)


            }
    })


}


}

【问题讨论】:

    标签: swift annotations swift2 mapkit mapkitannotation


    【解决方案1】:

    您可以轻松获取两个注释坐标并在地图视图上绘制 MKGeodisicPoliline,如下面的代码所示。折线将有 2 个注释。

     var geodesicPolyline = MKGeodesicPolyline(coordinates: &coordinates[0], count: 2)
    map.addOverlay(geodesicPolyline)
    

    其中坐标数组有注解的位置坐标对象。

    【讨论】:

    • 嗨,我如何传递存储在位置管理器函数中的坐标 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 我需要连接存储在 locations 中的坐标 数组,第 n 个和第 n-1 个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    相关资源
    最近更新 更多