Ios 如何更改坐标注释的图像?
我不明白如何在Swift中更改标记点的图像。我试过几种不同的方法Ios 如何更改坐标注释的图像?,ios,swift,mapkit,mkannotation,mkannotationview,Ios,Swift,Mapkit,Mkannotation,Mkannotationview,我不明白如何在Swift中更改标记点的图像。我试过几种不同的方法 func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! 但它似乎从未被调用,我不知道如何强迫它被调用 我要说的是,我也以编程方式添加了MKMapView,这样我就可以根据使用的iPhone切换大小(水平和垂直)。(我仍然不能相信这对Xcode来说是不直观的,程序员必须围绕它编程。
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!
但它似乎从未被调用,我不知道如何强迫它被调用
我要说的是,我也以编程方式添加了MKMapView
,这样我就可以根据使用的iPhone切换大小(水平和垂直)。(我仍然不能相信这对Xcode来说是不直观的,程序员必须围绕它编程。)
这是我的密码:
@IBAction func addStroke(sender: UIButton) {
NSLog("Add Stroke Touched")
NSLog("X %f Y %f", manager.location.coordinate.latitude, manager.location.coordinate.longitude)
GPSspot = manager.location.coordinate
annotation.setCoordinate(GPSspot)
annotation.title = "Stroke"
let useID = "test"
var myAnnotation = mapHole.dequeueReusableAnnotationViewWithIdentifier(useID)
if (myAnnotation == nil) {
myAnnotation = MKAnnotationView(annotation: annotation, reuseIdentifier: useID)
myAnnotation.image = UIImage(named: "ballSmall.png")
myAnnotation.canShowCallout = true
myAnnotation.enabled = true
} else {
myAnnotation.annotation = annotation
}
mapHole.viewForAnnotation(annotation)
mapHole.addAnnotation(annotation)
}
//MARK: - Map Kit
var mapRect = MMRect(xLoc: 502, yLoc:20, yWidth:218, xHeight:386)
var mapHole:MKMapView! //= MKMapView() as MKMapView
var annotation = MKPointAnnotation()
var MAView:MKAnnotationView!
//MARK: - GPS Locations
var manager:CLLocationManager!
var GPSspot:CLLocationCoordinate2D!
var span:MKCoordinateSpan!
var region:MKCoordinateRegion!
//MARK: - Default Initializer
override func viewDidLoad() {
super.viewDidLoad()
//MARK: Locations
manager = CLLocationManager()
if (CLLocationManager.locationServicesEnabled()) {
NSLog("locations services started")
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestAlwaysAuthorization()
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
} else {
NSLog("location services failed")
}
//MARK: MKMapView
mapHole = MKMapView(frame: mapRect.textRect)
span = MKCoordinateSpanMake(0.001, 0.001)
GPSspot = manager.location.coordinate
region = MKCoordinateRegion(center: GPSspot, span: span)
mapHole.setRegion(region, animated: true)
mapHole.mapType = MKMapType.Satellite
mapHole.showsUserLocation = true
self.view.addSubview(mapHole)
}
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is MKUserLocation {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView (annotation: annotation, reuseIdentifier: reuseId)
anView.image = UIImage(named:"ballSmall.png")
anView.canShowCallout = true
} else {
anView.annotation = annotation
}
return anView
}
有人能帮忙吗?非常感谢。我一直在做这件事,读了大约一个星期的书,我就是不明白。从未调用
viewForAnnotation
方法,因为当在viewDidLoad
中以编程方式创建地图视图时,地图视图的委托
未设置
创建地图视图后,设置其委托
属性:
mapHole = MKMapView(frame: mapRect.textRect)
mapHole.delegate = self // <-- add this line
span = MKCoordinateSpanMake(0.001, 0.001)
请注意,必须在地图视图上设置委托
属性,并声明视图控制器实现该协议
您还应该从
addStroke
方法中删除与视图相关的代码,因为它不属于该方法,没有任何效果,并且毫无意义:
annotation.title = "Stroke"
/* This code doesn't belong here...
let useID = "test"
var myAnnotation = mapHole.dequeueReusableAnnotationViewWithIdentifier(useID)
if (myAnnotation == nil) {
myAnnotation = MKAnnotationView(annotation: annotation, reuseIdentifier: useID)
myAnnotation.image = UIImage(named: "ballSmall.png")
myAnnotation.canShowCallout = true
myAnnotation.enabled = true
} else {
myAnnotation.annotation = annotation
}
mapHole.viewForAnnotation(annotation)
*/
mapHole.addAnnotation(annotation)
还要注意
viewDidLoad
中的此代码:
GPSspot = manager.location.coordinate
region = MKCoordinateRegion(center: GPSspot, span: span)
mapHole.setRegion(region, animated: true)
可能会崩溃,因为在调用startUpdatingLocation
之后不久,manager.location
此时可能仍然是nil
。首先检查manager.location
是否为nil
或将该代码移动到didUpdateLocations
委托方法会更安全
最后,关于: …我以编程方式将MKMapView添加到,以便可以切换大小 (水平和垂直)基于iPhone的使用情况。(一) 仍然不能相信这对Xcode和程序员来说是不直观的 必须围绕它进行编程。)
请注意,Xcode只是让您编写和编译代码的IDE。只有iOS SDK才能知道使用的是哪款iPhone。SDK可以根据屏幕大小自动调整视图大小。在文档、WWDC视频、SO或其他资源中搜索“自动布局”和/或“尺寸类别”。谢谢!!!!!我错过了添加代理,现在它可以工作了。非常感谢。(现在开始下一周要解决的问题。)
GPSspot = manager.location.coordinate
region = MKCoordinateRegion(center: GPSspot, span: span)
mapHole.setRegion(region, animated: true)