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?