显示从当前位置到另一个点的注释距离

时间:2016-12-09 11:41:15

标签: ios swift mkmapview mapkit core-location

我想显示从当前位置到另一个点的总距离,我希望距离显示在Annotation的副标题中。 这是我的代码,但它不起作用:

func shopDeal () {
    var inKm = ""
    let locationsq =
        [["title": "Shop Deal","subtitle": "\(inKm) km", "latitude": 31.352792, "longitude": 74.253201],
        ["title": "Shop Deal", "subtitle": "\(inKm) km", "latitude": 31.403563, "longitude":  74.258200]]

    // Span
    for location in locationsq {
        let annotation = MKPointAnnotation()
        annotation.title = location["title"] as? String
        annotation.subtitle = location["subtitle"] as? String
        annotation.coordinate = CLLocationCoordinate2D(latitude: location["latitude"] as! Double, longitude: location["longitude"] as! Double)
        mapView.addAnnotation(annotation)
        func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

            let myCurrentLoc = locations[locations.count - 1]
            let coor1 = CLLocation(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude)
            let coor2 = CLLocation (latitude: myCurrentLoc.coordinate.latitude, longitude: myCurrentLoc.coordinate.longitude )

            let distanceInMeters:CLLocationDistance = coor1.distanceFromLocation(coor2)

            inKm = String(format: "%.2f", distanceInMeters / 1000)

            print("\(inKm) km")
        }
    }
    mapView.delegate = self
}

非常感谢任何帮助。提前致谢

2 个答案:

答案 0 :(得分:1)

在地图上查找两点(LAT and LONG)之间的距离:

let coordinate1 = CLLocation(latitude: 5.0, longitude: 5.0)
let coordinate2 = CLLocation(latitude: 5.0, longitude: 3.0)
//Decalare distanceInMeters as global variables so that you can show distance on subtitles
let distanceInMeters = coordinate1.distance(from: coordinate2)

现在,如果你想在注释的副标题中显示距离,那么:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    if annotation is MKUserLocation {
            return nil
    }

    let reuseId = "pinIdentifier"

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
    }
    else {
        pinView!.annotation = annotation
    }

    //Adding Subtitle text
    let subtitleView = UILabel()
    //Decalare distanceInMeters as global variables so that you can show distance on subtitles
    subtitleView.text = "\(distanceInMeters)"
    pinView!.detailCalloutAccessoryView = subtitleView

    return pinView
}

如果有任何问题,请随时发表评论

答案 1 :(得分:0)

试试这个!

// 1. Get Source & Destination coordinates (Hard-cored here)
let locSource : CLLocation = CLLocation.init(latitude: 17.428031, longitude: 78.377837)
let locDestination : CLLocation = CLLocation.init(latitude: 17.441879, longitude: 78.429990)


// 2. Find distance between the Source & Destination
let nDistanceInMeters : CLLocationDistance = locSource.distance(from: locDestination)
let nDistanceInKiloMeters : CLLocationDistance = nDistanceInMeters / 1000.0;

// 3. Convert to String
let strDistanceInMeters : String = String.init(format: "%f", nDistanceInMeters)
let strDistanceInKM : String = String.init(format: "%f", nDistanceInKiloMeters)


// 4. Assign the calculated distance to your label,
// Probably in mapView(mapView:viewForAnnotation annotation:
lblDistanceLabelOnAnnotationView.text = strDistanceInKM

希望有所帮助!

相关问题