我正在将应用程序从Xcode 6.2重建为6.3.2
在我的info.plist中,我使用字符串消息设置NSLocationWhenInUseUsageDescription,使用另一个字符串消息设置隐私 - 位置使用描述。
我打开了功能/地图 我打开了功能/背景模式/位置更新
在构建阶段导入MapKit和CoreLocation框架
我需要获得单纬度和经度,因为我需要使用Parse。
Imas145回答后的更新
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var anotationsCountLabel: UILabel!
@IBOutlet weak var distanceFilterLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.distanceFilter = 100.0 //tot horizontal meters before sending an update on location
mapView.userTrackingMode = .Follow
locationManager.startUpdatingLocation()
}
//MARK: - Helper - CLLocationManagerDelegate
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let location = locationManager.location {
var myLatitude = locationManager.location.coordinate.latitude
var myLongitude = locationManager.location.coordinate.longitude
var location = CLLocationCoordinate2D(latitude: myLatitude, longitude: myLongitude)
let span = MKCoordinateSpanMake(0.05, 0.05) //how much to show
let region = MKCoordinateRegionMake(location, span) //how much to show
mapView.setRegion(region, animated: true)
// let anotation = MKPointAnnotation()
// anotation.coordinate = location //was : anotation.setCoordinate(location)
// anotation.title = "you are here"
// anotation.subtitle = "City name"
// mapView.removeAnnotation(anotation)
// mapView.addAnnotation(anotation)
var totalAnotations = mapView.annotations.count
anotationsCountLabel.text = "\(totalAnotations)"
distanceFilterLabel.text = "\(locationManager.distanceFilter)"
println("latitude is: \(myLatitude)")
println("longitude is: \(myLongitude)")
// locationManager.stopUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("problem1: error" + error.localizedDescription)
}
}
答案 0 :(得分:1)
使用location
启动位置更新后,您无法立即访问startUpdatingLocation()
。正如文档所说:
The last location received. Will be nil until a location has been received.
相反,您需要在确定位置后处理它。您可以在locationManager:didUpdateLocations:
这是一个快速实现:
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.distanceFilter = 100.0 //distance before notifing a change
locationManager.startUpdatingLocation() //really starts monitoring position
// move the code to delegate method
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let location = locationManager.location {
var myLatitutde = location.coordinate.latitude
var myLongitude = location.coordinate.longitude
var myLocation = CLLocationCoordinate2D()
let span = MKCoordinateSpanMake(0.05, 0.05) //how much to show
let region = MKCoordinateRegionMake(myLocation, span) //how much to show
mapView.setRegion(region, animated: true)
let anotation = MKPointAnnotation()
anotation.coordinate = myLocation //XC 6.3.2
anotation.title = "you are here"
anotation.subtitle = "city name"
mapView.addAnnotation(anotation)
}
}
}
如果出现问题,您还应该处理locationManager:didFailWithError:
。
编辑:
要为引脚下降设置动画,您需要执行以下操作:
将MKMapViewDelegate
协议添加到您的班级
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate
将代理人添加到mapView
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
mapView.delegate = self
// rest of the code, location manager setup etc...
}
实施mapView:viewForAnnotation:
委托方法
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
let pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: nil)
pin.pinColor = .Red // or .Green or .Purple
pin.animatesDrop = true
pin.canShowCallout = true
return pin
}
现在针脚掉落了动画。