Kra*_*han 11 mkannotationview ios swift
一旦点击,我想使用注释.我查了一下Apple的文档并且用谷歌搜索了但我无法找到为什么这与应该如何完成有什么不同.基本上; 我没有得到println("Pin clicked");
为什么不?
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!
{
if !(annotation is MKPointAnnotation) {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.canShowCallout = true
}
else {
anView.annotation = annotation
}
return anView
}
func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
{
println("Pin clicked");
}
func setAnnotationPinOnMap(annotation : NSManagedObject)
{
var subject: AnyObject? = annotation.valueForKey("subject")
var desc: AnyObject? = annotation.valueForKey("desc")
var langitude: AnyObject? = annotation.valueForKey("langitude")
var longitude: AnyObject? = annotation.valueForKey("longitude")
println(annotation);
let pin = MKPointAnnotation()
let location = CLLocationCoordinate2D(
latitude: longitude as CLLocationDegrees,
longitude: langitude as CLLocationDegrees
)
println(location.longitude, location.latitude)
pin.setCoordinate(location)
pin.title = subject as String!
pin.subtitle = desc as String!
println( pin.coordinate.longitude)
viewForAnnotation(pin);
mapView.addAnnotation(pin)
}
Run Code Online (Sandbox Code Playgroud)
我导入了地图工具包并包含了地图视图委托.
小智 15
要回答原始问题,didSelectAnnotationView
很可能不会被调用,因为delegate
未设置地图视图的属性.另一个常见原因是注释title
是空白的.
在更新的问题中,引脚将不会显示viewForAnnotation
已实现的方式,因为它创建了一个MKAnnotationView
但未设置其image
属性.默认情况下image
,a 的属性MKAnnotationView
是nil
这样的,所以引脚在那里但不可见.
如果要显示标准红色引脚,可以:
viewForAnnotation
委托方法,默认情况下地图视图将显示红色引脚.viewForAnnotation
委托方法并创建一个MKPinAnnotationView
自动显示"pin"图像的代替方法.因此要么将引脚设置image
为某个自定义图像:
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.canShowCallout = true
anView.image = UIImage(named:"CustomImage") // <-- add this line
Run Code Online (Sandbox Code Playgroud)
或创建一个MKPinAnnotationView
替代,并可选择设置其pinColor
:
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if anView == nil {
anView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView!.canShowCallout = true
anView!.animatesDrop = true
anView!.pinColor = .Purple // or Red or Green
}
else {
anView!.annotation = annotation
}
Run Code Online (Sandbox Code Playgroud)
在我的情况下问题是我在YES中设置属性canShowCallout,所以如果你在YES中设置此属性
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
Run Code Online (Sandbox Code Playgroud)
不叫.当我删除此属性时,该方法被调用.
annotationView = [[ClusterAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.canShowCallout = YES; //try setting to NO or don't set.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10862 次 |
最近记录: |