gpt4 book ai didi

ios - 如何更改坐标注释的图像?

转载 作者:搜寻专家 更新时间:2023-10-31 19:35:28 28 4
gpt4 key购买 nike

我不明白如何在 Swift 中更改标记点的图像。我已经尝试了

的几种变体
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方法永远不会被调用,因为 map View 的 delegateviewDidLoad 中以编程方式创建 map View 时未设置.

创建 map View 后,设置其delegate属性:

mapHole = MKMapView(frame: mapRect.textRect)
mapHole.delegate = self // <-- add this line
span = MKCoordinateSpanMake(0.001, 0.001)

此外,如果您还没有声明,则必须声明 View Controller 实现了 MKMapViewDelegate协议(protocol)。例如:

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

注意设置 delegate必须完成 map View 上的属性以及声明 View Controller 实现协议(protocol)。


您还应该从 addStroke 中删除与 View 相关的代码方法,因为它不属于那里,没有效果,也没有意义:

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)

可能会崩溃,因为 manager.location可能仍然是nil在调用startUpdatingLocation 后不久.首先检查是否 manager.location 会更安全不是 nil或将该代码移至 didUpdateLocations委托(delegate)方法。


最后,关于:

...I added the MKMapView programmatically to so I can switch sizes (horizontal and vertical) based on which iPhone is being used. (I still can't believe this isn't intuitive to Xcode and the programmer has to program around it.)

请注意,Xcode 只是让您编写和编译代码的 IDE。是 iOS SDK 知道正在使用哪个 iPhone。 SDK 可以根据屏幕尺寸自动调整 View 大小。在文档、WWDC 视频、SO 或其他资源中搜索“Auto Layout”和/或“Size Classes”。

关于ios - 如何更改坐标注释的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26435572/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com