user8188756
user8188756

Reputation:

Put latitude and longitude on a UILabel programmatically

I can't seem to get the latitude and longitude to show up on the labels. I've tried many things though I'm guessing it might be something simple.

import Foundation
import UIKit
import CoreLocation
import MapKit  

class LocationVC: UIViewController, CLLocationManagerDelegate {
    var location: CLLocation! {
        didSet{
            longLabel.text = ("\(location.coordinate.latitude)")

            latLabel.text  = ("\(location.coordinate.longitude)")
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let location = locations[0]

        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.5, 0.5)
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)

        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
        map.setRegion(region, animated: true)

        print("hhhhh\(location.coordinate.latitude)")

        print(location.altitude)
        print(location.speed)

        self.map.showsUserLocation = true
    }

All the views are set up correctly.

Upvotes: 0

Views: 248

Answers (2)

maxkoriakin
maxkoriakin

Reputation: 337

Use this

longLabel.text = location.coordinate.latitude.description
latLabel.text  = location.coordinate.longitude.description

Upvotes: 0

glyvox
glyvox

Reputation: 58049

You did not change the value of the location variable of your class, which would trigger the text change of the label. Append this to didUpdateLocations:

self.location = location

Upvotes: 3

Related Questions