gpt4 book ai didi

ios - 使用图像创建自定义 map 注释标注,从数组访问数据

转载 作者:搜寻专家 更新时间:2023-11-01 07:11:13 25 4
gpt4 key购买 nike

我从一个 JSON 文件中解析了一组数据(参见下面的示例):

{
"locations": [

{
"id": "0001",
"name": "Helensburgh Tunnels",
"type": "Tunnels, Beach, Views",
"location": "Helensburgh, South Coast",
"image": "Helensburgh-Tunnels.jpg",
"activity": "Walking, photography, tunnelling",
"isVisited": false,
"latitude": -34.178985,
"longitude": 150.992867
}
]
}

我能够将所有这些数据正确读入 TableView(一切正常),但是,我还想将 JSON 文件中的所有位置显示为 MapView 上的注释。到目前为止,除了标注框左侧的预览图像外,一切都正确显示(所有注释图钉都会出现,单击时它们会显示带有标题和副标题的标注,但没有图像)。

我需要解决什么问题才能显示此图片?我可以在我的应用程序的其他部分实现它,其中只显示一个位置,但是,我似乎无法弄清楚如何将图像添加到该 View 中的所有注释标注。

enter image description here

这是我用来在我的 NearMeMapViewController 中填充注释的代码:

import UIKit
import MapKit
import CoreLocation

class NearMeMapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet var nearMeMap: MKMapView!

let locationManager = CLLocationManager()

var locations = [Location]()
var location:Location!

override func viewDidLoad() {
super.viewDidLoad()

// parse json
if let locationJson = readLocation(){
if let locationArray = locationJson["locations"] as? [[String:Any]]{
for location in locationArray{
locations.append(Location.init(locationInfo: location))
}
print(locations.count)
}
}
// end parse json

nearMeMap.delegate = self

self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.nearMeMap.showsUserLocation = true

// Show annotation
for location in locations {
let annotation = MKPointAnnotation()
annotation.title = location.name
annotation.subtitle = location.type
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
nearMeMap.addAnnotation(annotation)

}

}


// Start test - Custom annotation callout with image
func nearMeMap (_ nearMeMap: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

let identifier = "MyPin"

if annotation.isKind(of: MKUserLocation.self) {
return nil
}

// Reuse the annotation if possible
var annotationView:MKPinAnnotationView? = nearMeMap.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView

if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
}

let leftIconView = UIImageView(frame: CGRect.init(x: 0, y: 0, width: 53, height: 53))
leftIconView.image = UIImage(named: location.image)
annotationView?.leftCalloutAccessoryView = leftIconView

return annotationView
}
// End test

如果有任何帮助,我将不胜感激!我是一名学生,我只是在学习,如果我使用了任何不正确的术语或有菜鸟错误,我深表歉意。

最佳答案

swift 3.0

在上面的代码中,MKMapViewDelegate 应该是

func mapView (_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{...}

而不是

func nearMeMap (_ nearMeMap: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {...}

添加 var currentIndex = 0 作为全局声明以标识当前图像索引。否则,您知道使用 JSON 响应中的 id

即,全局范围变成如下代码,

let locationManager = CLLocationManager()

var locations = [Location]()
//test variable? implemented to resolve issue with images in custom callout
var location:Location!

var currentIndex = 0
override func viewDidLoad() {...}

然后,leftIconView 将变为,

let leftIconView = UIImageView(frame: CGRect.init(x: 0, y: 0, width: 53, height: 53))

var images = [String]()
for location in locations{
images.append(location.image)
}
leftIconView.image = UIImage(named: images[currentIndex])
self.currentIndex = self.currentIndex+1 //0,1,2,3...N.

输出:-

enter image description here

关于ios - 使用图像创建自定义 map 注释标注,从数组访问数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44815487/

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