0

I added a location search bar to my app. The tableview controller consists of the user location lat/long as well as the destination (address the user inputs into the search bar). As far as I can tell the latitude / longitude is within the placemark.coordinate:

placemark.coordinate = CLLocationCoordinate2D(latitude: 45.253519203737767, longitude: -66.070974382763978)

I have two variables: destLat and destLong. How I can extract the latitude and longitude from placemark.coordinate and place into the two variables mentioned above?

related code:

// cache the pin
    selectedPin = placemark

    // clear existing pins
    mapView.removeAnnotations(mapView.annotations)
    let annotation = MKPointAnnotation()
    annotation.coordinate = placemark.coordinate
    annotation.title = placemark.name
    if let city = placemark.locality,
        let prov = placemark.administrativeArea {
        annotation.subtitle = "\(city), \(prov)"
    }

    let destination = placemark.coordinate
    print("Destination coordinates: \(destination)")
    let name = placemark.name
    print("Placemark Name: \(name!)")


    mapView.addAnnotation(annotation)
    let span = MKCoordinateSpanMake(0.05, 0.05)
    let region = MKCoordinateRegionMake(placemark.coordinate, span)
    mapView.setRegion(region, animated: true)

Console output:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
LizG
  • 2,246
  • 1
  • 23
  • 38
  • Taking a look over this may help: http://stackoverflow.com/questions/24706885/how-can-i-plot-addresses-in-swift-converting-address-to-longitude-and-latitude – Mr. Xcoder Feb 04 '17 at 17:15

2 Answers2

1

Perhaps your were thinking too hard. You can assign values to these variables just like any other:

let (destLat, destLong) = (placemark.coordinate.latitude, placemark.coordinate.longitude)
Code Different
  • 90,614
  • 16
  • 144
  • 163
1

If both variable you have declare is type of Double then you need to simply need to access the latitude and longitude property of CLLocationCoordinate2D.

destLat = destination.latitude
destLong = destination.longitude

If type of both var is different then simply change it to Double works for you.

Nirav D
  • 71,513
  • 12
  • 161
  • 183