我希望用户能够将针的位置拖到另一条街道上,因此应用程序会占用这些新位置。
以下代码:
答案 0 :(得分:0)
// Function to manage the location of the User
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location:CLLocationCoordinate2D = manager.location!.coordinate
locationManager.stopUpdatingLocation() // Stop location
print("location = \(location.latitude)\(location.latitude)")
latitude = location.latitude
longitude = location.longitude
let center = CLLocationCoordinate2D (latitude: location.latitude, longitude: location.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpanMake(0.01, 0.01))
self.map.setRegion(region, animated: true)
self.map.removeAnnotations(map.annotations)
let pinLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.latitude, location.longitude)
let pinAnnotation = MKPointAnnotation()
pinAnnotation.coordinate = pinLocation
pinAnnotation.title = "Você"
self.map.addAnnotation(pinAnnotation)
//Funcao para pegar a localizacao e transformar em nome
let requestCLLocation = CLLocation(latitude: self.latitude, longitude: self.longitude)
CLGeocoder().reverseGeocodeLocation(requestCLLocation, completionHandler: {(placemarks, error) -> Void in
if (error != nil){
print(error!)
}else{
if placemarks!.count > 0 {
let p = CLPlacemark(placemark: (placemarks?[0])! as CLPlacemark)
var locationName = p.addressDictionary!["Name"] as? NSString
var bairro = p.addressDictionary!["SubLocality"] as? NSString
var city = p.addressDictionary!["City"] as? NSString
var state = p.addressDictionary!["State"] as? NSString
if (locationName == nil){
locationName = "Rua não encontrada"
}
if (bairro == nil){
bairro = "Bairro não encontrado"
}
if (city == nil){
city = "Cidade não encontrada"
}
if (state == nil){
state = "Erro"
}
//Street name in Label
self.streetName.text = ((locationName!) as String)+", "+((bairro!) as String)+", "+((city!) as String)+"-"+((state!) as String)
}
}})
}