这是我的代码:
overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.startUpdatingLocation() var latitude:CLLocationdegrees = locationManager.location.coordinate.latitude var longitude:CLLocationdegrees = locationManager.location.coordinate.longitude var homeLati: CLLocationdegrees = 40.01540192 var homeLong: CLLocationdegrees = 20.87901079 var latDelta:CLLocationdegrees = 0.01 var longDelta:CLLocationdegrees = 0.01 var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta,longDelta) var myHome:CLLocationCoordinate2D = CLLocationCoordinate2DMake(homeLati,homeLong) var myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude,longitude) var theRegion:MKCoordinateRegion = MKCoordinateRegionMake(myLocation,theSpan) self.theMapVIEw.setRegion(theRegion,animated: true) self.theMapVIEw.mapType = MKMapType.HybrID self.theMapVIEw.showsUserLocation = true ///Red Pin var myHomePin = MKPointAnnotation() myHomePin.coordinate = myHome myHomePin.Title = "Home" myHomePin.subTitle = "Bogdan's home" self.theMapVIEw.addAnnotation(myHomePin) var anVIEw:MKAnnotationVIEw = MKAnnotationVIEw() anVIEw.annotation = myHomePin anVIEw.image = UIImage(named:"xaxas") anVIEw.canShowCallout = true anVIEw.enabled = true}您需要实现vIEwForAnnotation委托方法并从那里返回一个MKAnnotationVIEw。
以下是一个例子:
func mapVIEw(mapVIEw: MKMapVIEw!,vIEwForAnnotation annotation: MKAnnotation!) -> MKAnnotationVIEw! { if (annotation is MKUserLocation) { //if annotation is not an MKPointAnnotation (eg. MKUserLocation),//return nil so map draws default vIEw for it (eg. blue dot)... return nil } let reuseID = "test" var anVIEw = mapVIEw.dequeueReusableAnnotationVIEwWithIDentifIEr(reuseID) if anVIEw == nil { anVIEw = MKAnnotationVIEw(annotation: annotation,reuseIDentifIEr: reuseID) anVIEw.image = UIImage(named:"xaxas") anVIEw.canShowCallout = true } else { //we are re-using a vIEw,update its annotation reference... anVIEw.annotation = annotation } return anVIEw}
记住,当您要使用自己的自定义图像时,您需要创建一个简单的MKAnnotationVIEw。 MKPinAnnotationVIEw只能用于标准引脚注释。
另请注意,您不应该在调用startUpdatingLocation之后立即尝试访问locationManager.location。它可能还没有准备好
使用dIDUpdateLocations委托方法。
您还需要调用requestAlwaysAuthorization / requestWhenInUseAuthorization(请参阅Location Services not working in iOS 8)。
总结以上是内存溢出为你收集整理的Swift – 将MKAnnotationView添加到MKMapView全部内容,希望文章能够帮你解决Swift – 将MKAnnotationView添加到MKMapView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)