Kamboh
Kamboh

Reputation: 41

show distance on annotation from current location to another point

I want to show my total distance from my current location to another point and I want the distance shows in subtitle of Annotation. Here is my code but it doesn't work :

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
}

Any help is highly appreciated. Thanks in advance

Upvotes: 1

Views: 1389

Answers (2)

Naresh Reddy M
Naresh Reddy M

Reputation: 1096

Try this!

// 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

Hope that helps!

Upvotes: 0

Shobhakar Tiwari
Shobhakar Tiwari

Reputation: 7892

Finding Distance between two points(LAT and LONG) on Map :

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)

Now if you want to show distance in subtitle of annotation then :

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
}

Feel free to comment if further any issue

Upvotes: 2

Related Questions