在地图视图中显示当前位置时,如何显示蓝点而不是Pin?此时代码显示了一个红色引脚,显示用户当前移动时的当前位置。我如何将其转换为苹果使用的蓝点?
import UIKit
import MapKit
class ViewController: UIViewController,CLLocationManagerDelegate {
@IBOutlet weak var myMapView: MKMapView!
let myLocMgr = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
myLocMgr.desiredAccuracy = kCLLocationAccuracyBest
myLocMgr.requestWhenInUseAuthorization()
myLocMgr.startUpdatingLocation()
myLocMgr.delegate = self
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
// get most recient coordinate
let myCoor = locations[locations.count - 1]
//get lat & long
let myLat = myCoor.coordinate.latitude
let myLong = myCoor.coordinate.longitude
let myCoor2D = CLLocationCoordinate2D(latitude: myLat, longitude: myLong)
//set span
let myLatDelta = 0.05
let myLongDelta = 0.05
let mySpan = MKCoordinateSpan(latitudeDelta: myLatDelta, longitudeDelta: myLongDelta)
let myRegion = MKCoordinateRegion(center: myCoor2D, span: mySpan)
//center map at this region
myMapView.setRegion(myRegion, animated: true)
//add anotation
let myAnno = MKPointAnnotation()
myAnno.coordinate = myCoor2D
myMapView.addAnnotation(myAnno)
}
@IBAction func stop(sender: AnyObject) {
myLocMgr.stopUpdatingLocation()
}
@IBAction func resume(sender: AnyObject) {
myLocMgr.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
答案 0 :(得分:0)
self.myMapView.showsUserLocation = true
就是您所需要的。它的showsUserLocation
属性将其设置为viewDidLoad
或使用IB(如果可能)。您不需要在LocationManager
的委托didUpdateLocations
中执行任何额外的操作,只需将其设置为MKMapView
即可完成剩下的工作