【问题标题】:How do you show a blue dot instead of a Pin when showing current location in mapview?在地图视图中显示当前位置时,如何显示蓝点而不是 Pin?
【发布时间】:2016-07-02 08:42:08
【问题描述】:

在地图视图中显示当前位置时如何显示蓝点而不是 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()

}

}

【问题讨论】:

    标签: mkmapview mapkit cllocationmanager mkannotation currentlocation


    【解决方案1】:
    self.myMapView.showsUserLocation = true
    

    showsUserLocation 是您所需要的。这是MKMapView 属性将其设置为viewDidLoad 或使用IB(如果可能)。您不需要在LocationManager 的委托didUpdateLocations 中做任何额外的事情,只需将其设置为MKMapView 即可完成其余的事情

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多