用户位置(蓝点)不断变成红色图钉
Posted
技术标签:
【中文标题】用户位置(蓝点)不断变成红色图钉【英文标题】:Users location (blue dot) keeps turning into red pin 【发布时间】:2016-07-28 11:46:39 【问题描述】:我希望将用户位置显示为蓝点,同时在地图视图上也有图钉。我希望引脚有一个带有信息按钮的注释。 我可以获得用户位置的蓝点,并使引脚具有注释,例如标题和副标题。但是,当我将信息按钮添加到红色引脚时,用户位置(蓝点)会变成红色引脚。
我似乎无法发现我哪里出错了。它与最后一个函数有关,因为这是将信息按钮放在注释上的函数。但它也选择了用户当前的位置,并出于某种原因将其变成了一个 pin :(
class GetToTheStart: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate
@IBOutlet weak var mapView: MKMapView!
let myLocMgr = CLLocationManager()
override func viewDidLoad()
super.viewDidLoad()
myLocMgr.desiredAccuracy = kCLLocationAccuracyBest
myLocMgr.requestWhenInUseAuthorization()
myLocMgr.startUpdatingLocation()
myLocMgr.delegate = self
mapView.delegate = self
var zoo = CLLocationCoordinate2DMake(53.3562, -6.3053)
var zoopin = MKPointAnnotation()
zoopin.coordinate = zoo
zoopin.title = "dublin zoo"
zoopin.subtitle = "hello this is the zoo"
mapView.addAnnotation(zoopin)
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)
self.mapView.showsUserLocation = true
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
let reuseIdentifier = "pin"
var pin = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView
if pin == nil
pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
pin!.pinColor = .Red
pin!.canShowCallout = true
pin!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
else
pin!.annotation = annotation
return pin
【问题讨论】:
【参考方案1】:viewForAnnotation 委托方法将被地图上的所有注解调用,包括 userLocation。
所以你只需检查并返回 nil ,如下所示......
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
if annotation is MKUserLocation
//return nil so map view draws "blue dot" for standard user location
return nil
let reuseIdentifier = "pin"
var pin = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView
if pin == nil
pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
pin!.pinColor = .Red
pin!.canShowCallout = true
pin!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
else
pin!.annotation = annotation
return pin
【讨论】:
很高兴...在发布问题时,至少为代码部分保持一些标准以上是关于用户位置(蓝点)不断变成红色图钉的主要内容,如果未能解决你的问题,请参考以下文章
iPhone:如何启用 MKUserLocation '蓝点'?