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?
– Guilherme Nascimento
Cannot invoke initialize for type "Cllocationcordinate2d" with an argument list of type "(latitude : String ... You know how to fix ?
– Augusto Furlan