Cannot convert value of type when trying to get latitude and longitude from text fields

Viewed 154

Image of the project

My code isn't working. When the user puts in a latitude and longitude it's supposed to go to Maps but it just throws an error. It only works when I hard-code the latitude and longitude.

import UIKit
import MapKit

class ViewController: UIViewController {
    @IBOutlet weak var Longitude: UITextField!
    @IBOutlet weak var Latitude_button: UITextField!

    @IBAction func showMeWhere(_ sender: Any)
    {
        //Defining destination
        let latitude:CLLocationDegrees = Latitude_button
        let longitude:CLLocationDegrees = Longitude

      //        let latitude:CLLocationDegrees = 39.048825
    //        let longitude:CLLocationDegrees = -120.981227

        let regionDistance:CLLocationDistance = 1000;
        let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
        let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)

        let options = [MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center), MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)]

        let placemark = MKPlacemark(coordinate: coordinates)
        let mapItem = MKMapItem(placemark: placemark)
        mapItem.name = "Test Location"
        mapItem.openInMaps(launchOptions: options)
    }
}
3 Answers

The type isn't correct.

Change let latitude:CLLocationDegrees = Latitude_button to Double(Latitude_button.text ?? "") ?? 0.

Same to longitude

let longitude:CLLocationDegrees = Double(Longitude.text ?? "") ?? 0

Also, your name conventions isn's very proper. Should be like latitudeButton and longitude.

On your line let latitude:CLLocationDegrees = Latitude_button you are attempting to assign a variable of type UITextField to a variable of type CLLocationDegrees.

What you need to do is get the text from the text field and attempt to convert it into a number and then assign that number to your variable.

guard let latitude = CLLocationDegrees(Latitude_button.text!),
      let longitude = CLLocationDegrees(Longitude.text!) else {
    // show some sort message to the user that the values are invalid
    return
}

Here are some things that might help you getting started, as I've been there:

  1. In Swift, variables are in camelCase, and, by convention, stay away from snake_case and Capitalized for variable declarations. I would do something like this for your @IBOutlets...

    @IBOutlet weak var longitudeField: UITextField!
    @IBOutlet weak var latitudeField: UITextField!
    
  2. UITextFields have Strings in them, but inside your showMeWhere method, you're trying to pull assign a UITextField to a CLLocationDegrees.

    • UITextField has a property called text. That's what you want, not the field...you want the text IN the field.

    • CLLocationDegrees is a typealias for a Double...so the issue before you is converting a String to a Double.

Here's how you'd do it:

guard let latitude = Double(latitudeField.text), 
      let longitude = Double(longitudeField.text) else { return }
Related