Error Cannot invoke initialize for type "Cllocationcordinate2d" with an argument list of type "latitude : String"

Asked

Viewed 80 times

3

I’m developing an app that captures user location with Core Location and saved in a file.

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

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 in the place of latitude and longitude where he asks, he error because I am passing a variable.

complete 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

Someone would know the solution?

  • Which error appears?

  • Cannot invoke initialize for type "Cllocationcordinate2d" with an argument list of type "(latitude : String ... You know how to fix ?

1 answer

3


What happens is you’re going through one String and the expected values in the parameters latitude and longitude is 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 receiving.


With its more complete code, I was able to understand a little more its problem. At some points there is a certain mistake, as for example, you declare the variables FLatitude and FLongitude twice. One outside and one inside your condition.

To correct this, follow a code snippet 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
)
  • Friend I tested this, it returns me another error : Double? is not Convertible to Nsstring. Would you know the solution ?

  • There’s something that doesn’t fit your question or needs to put us in a better context. In the first error, you are indicating that the variable you are passing to the parameter latitude is a String, and now seems to be a Double, which in the first situation would not generate the first error.

  • Well, as may be occurring communication error, I’m sending all the code in the post below.

  • Augusto, now I understand better the problem. I edited my answer.

  • Thank you very much for trying for 2 days, thank you very much. Let me ask you a question : Before the map appeared more enlarged, how can I do To return this ? So the user doesn’t have to zoom in every time

  • The region of the map can be defined in the class MKCoordinateSpanMake, who in your case is with 0.05 in both parameters. These values are defined as the north-south and east-west distance in degrees. You can, for example, try to put a higher value, such as 3.0 to appear more distant.

  • Okay it worked... thank you

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.