Error Can not invoke initialize for type "CLLocationCordinate2D" with an argument of type "latitude: String"

3

I'm developing an app that captures the user's location with Core Location and saves it to a file.

Now I have a mapview and I need to display this location on the map (this location comes from the file).

I'm using this code:

override func viewDidLoad() {
    super.viewDidLoad()
    // 1
    let location = CLLocationCoordinate2D(
      latitude: 51.50007773,
      longitude: -0.1246402
    )
    // 2
    let span = MKCoordinateSpanMake(0.05, 0.05)
    let region = MKCoordinateRegion(center: location, span: span)
    mapView.setRegion(region, animated: true)

    //3   
    let annotation = MKPointAnnotation()
    annotation.coordinate = location
    annotation.title = "Big Ben"
    annotation.subtitle = "London"
    mapView.addAnnotation(annotation)
}

But when I put it in place of latitude and longitude where it asks, it gives the error because I'm passing a variable.

full code:

class mapViewController : UIViewController {

@IBOutlet weak var mapView: MKMapView!
// RECupera Arquivo
func getFilepath() -> String
{
    let userDomainPaths: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

    let filepath: String = userDomainPaths.objectAtIndex(0) as! String

    return "\(filepath)/afMyLocation.plist"
}

//FIM
//Abre Posicao 



// FIM

override func viewDidLoad() {
    super.viewDidLoad()
    print("LOG : Map carregado com sucesso")
    //Verifica arquivo


    let FLatitude : Double?
    let FLongitude : Double?

    let filepathAux: String = self.getFilepath()
    if(NSFileManager.defaultManager().fileExistsAtPath(filepathAux) == true) {
        print("Verificou se existe arquivo - crioui cordenadas")
        let array: NSArray = NSArray(contentsOfFile: filepathAux)!
        let FLatitude : String?
        let FLongitude : String?
        FLatitude = (array.objectAtIndex(0) as? String)!
        FLongitude = (array.objectAtIndex(1) as? String)!
                } else {
        print("Erro ao recuperar posicao")
        //exibe alerta
    }

    //FIM
    //EXIBE PONTO NO MAPA
        // 1

    //let latitude = (FLatitude as NSString).doubleValue
        let location = CLLocationCoordinate2D(
            latitude: 51.50007773,
            longitude: -0.1246402
        )
        // 2
        let span = MKCoordinateSpanMake(0.05, 0.05)
        let region = MKCoordinateRegion(center: location, span: span)
        mapView.setRegion(region, animated: true)

        //3
        let annotation = MKPointAnnotation()
        annotation.coordinate = location
        annotation.title = "Big Ben"
        annotation.subtitle = "London"
        mapView.addAnnotation(annotation)
    }
    }


    //let initialLocation = CLLocation(latitude: 21.282778, longitude: -157.829444)
    // *** Carega o point - Augusto Furlan ***

    //let lat :Double = NSString(string: FLatitude).floatValue
            // FIM

Would anyone know the solution?

    
asked by anonymous 25.09.2015 / 03:14

1 answer

3

What happens is that you are passing a String and the expected values in the parameters latitude and longitude are a double .

In this case, you need to convert these variables first, for example:

let latitude = (strLat as NSString).doubleValue
let longitude = (strLng as NSString).doubleValue

Where strLat and strLng are the variables you are getting.

With your most complete code, I was able to understand your problem a bit more. In some points there is a certain mistake, for example, you declare the variables FLatitude and FLongitude twice. One out and another within your condition.

To fix this, here is a snippet of code as my suggestion:

var fLatitude: Double = 0.0
var fLongitude: Double = 0.0

let filepathAux: String = getFilepath()

if (NSFileManager.defaultManager().fileExistsAtPath(filepathAux)) {
    let array: NSArray = NSArray(contentsOfFile: filepathAux)!

    fLatitude = (array.objectAtIndex(0) as! NSString).doubleValue
    fLongitude = (array.objectAtIndex(1) as! NSString).doubleValue
} else {
    print("Erro ao recuperar posicao")
}

let location = CLLocationCoordinate2D(
    latitude: fLatitude,
    longitude: fLongitude
)
    
25.09.2015 / 13:56