- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我过去有一些使用 MKMapView
和 MKPointAnnotation
的经验,我曾用它们在 map 上放置一些图钉。这次我想更进一步,使用 MKPinAnnotationView
来写一个标签和一些引脚。
不幸的是,它并没有像我预期的那样工作。
这是我想做的:
我有一张 map (一个 MKMapView 对象),当我触摸它时,我在触摸点放置了一个图钉,然后执行了一些计算,这在 map 上给出了第二个点。我在 map 上放了第二个图钉(位于第二个点),在最后一个图钉上我想放一个标签,说“你好,第二个!”,但是当图钉改变位置时需要更新这个标签。
相关代码如下:
class ViewController: UIViewController, MKMapViewDelegate {
var mapView:MKMapView!, touchPoint,secondPoint:MKPointAnnotation!
override func viewDidLoad() {
super.viewDidLoad()
mapView = MKMapView()
...........
let mapTap = UITapGestureRecognizer(target: self,
action: #selector(ViewController.mapTouchHandler))
mapView.addGestureRecognizer(mapTap)
}
func mapTouchHandler(gesture:UITapGestureRecognizer) {
...........
// Compute map coordinates for the touch point (tapGeoPoint).
if touchPoint == nil {
touchPoint = MKPointAnnotation()
mapView.addAnnotation(touchPoint);
}
touchPoint.coordinate = CLLocationCoordinate2D(latitude: tapGeoPoint.latitude,
longitude: tapGeoPoint.longitude)
...........
computeSecondPoint(url: someComputedURL)
}
func computeSecondPoint(url searchURL:String) {
let reqURL = NSURL(string: searchURL)!, session = URLSession.shared,
task = session.dataTask(with: reqURL as URL) {
(data: Data?, response: URLResponse?, error: Error?) in
if error == nil {
do {let allData = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSArray
.................
// Compute map coordinates for the second point (secondPointCoord).
if self.secondPoint == nil {
self.secondPoint = MKPointAnnotation()
self.mapView.addAnnotation(self.secondPoint)
}
DispatchQueue.main.async {
() -> Void in
self.secondPoint.coordinate = CLLocationCoordinate2D(latitude: secondPointCoord.latitude,
longitude: secondPointCoord.longitude)
self.secondPoint.title = "Hello Second -TITLE!"
//* I want to update the label for this pin (attached to the secondPoint) too.
}
} catch let error as NSError {print(error.localizedDescription)}
} else {
print("Error inside \(#function):\n\(error)")
}
}
task.resume()
}
func mapView(_ mapView: MKMapView,
viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "pin"
var view: MyPinAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
as? MyPinAnnotationView {
dequeuedView.annotation = annotation
view = dequeuedView
} else {
view = MyPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
if ((annotation.coordinate.latitude != touchPoint.coordinate.latitude) ||
(annotation.coordinate.longitude != touchPoint.coordinate.longitude)) {//* I need a better test to check that this not touchPoint!
view.pinTintColor = UIColor.blue
view.canShowCallout = true
view.calloutOffset = CGPoint(x: -7, y: 0)
view.setInfo(title: "Hi Start Label!")
} else {
view.pinTintColor = UIColor.red
view.canShowCallout = false
}
}
return view
}
}
这是 MyPinAnnotationView 类:
import UIKit
import MapKit
class MyPinAnnotationView: MKPinAnnotationView {
let information:UILabel = UILabel(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 70.0, height: 30.0)))
func setInfo(title : String)
{
information.text = title
information.textAlignment = .center
self.addSubview(information)
}
func hideInfo() {
information.removeFromSuperview()
}
}
注释标记为//* 的行显示我需要帮助的地方。
第一个问题,我想更新secondPoint上的label,但是不知道用什么代码。线路:
//* 我也想更新此 pin 的标签(附加到 secondPoint)。
现在标签显示为第一组,但我不知道如何更新它。
第二个问题,必须有更好的方法来测试我正在处理的引脚。线路:
//* 我需要一个更好的测试来检查这不是 touchPoint!
最佳答案
如果您希望注释 View 显示一些随着注释更改而更新的文本,请在注释上使用 KVO。因此,首先,创建一个模型对象,即注释,其中包含要观察的新属性:
class MyAnnotation: NSObject, MKAnnotation {
dynamic var title: String?
dynamic var subtitle: String?
dynamic var coordinate: CLLocationCoordinate2D
dynamic var information: String?
init(title: String? = nil, subtitle: String? = nil, coordinate: CLLocationCoordinate2D, information: String? = nil) {
self.title = title
self.subtitle = subtitle
self.coordinate = coordinate
self.information = information
}
}
我将此新属性命名为information
,但您可能想出一个更好的名称来捕捉此属性的功能意图。但希望它能说明这个想法。这里的关键要点是,如果此属性稍后可能会发生变化,我们希望将其设为动态
,以便我们可以使用 KVO 来观察这些变化。
class MyPinAnnotationView: MKPinAnnotationView {
private let informationLabel = UILabel(frame: CGRect(origin: .zero, size: CGSize(width: 70.0, height: 30.0)))
private var observerContext = 0
override var annotation: MKAnnotation? {
willSet {
removeObserverIfAny()
}
didSet {
if let annotation = annotation as? MyAnnotation {
annotation.addObserver(self, forKeyPath: #keyPath(MyAnnotation.information), context: &observerContext)
informationLabel.text = annotation.information
}
}
}
deinit {
removeObserverIfAny()
}
private func removeObserverIfAny() {
if let oldAnnotation = annotation as? MyAnnotation {
oldAnnotation.removeObserver(self, forKeyPath: #keyPath(MyAnnotation.information))
}
}
func showInformation() {
addSubview(informationLabel)
}
func hideInformation() {
informationLabel.removeFromSuperview()
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard context == &observerContext else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
return
}
if let annotation = annotation as? MyAnnotation, let information = annotation.information {
informationLabel.text = information
}
}
}
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MyPinAnnotationView
if annotationView == nil {
annotationView = MyPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = true
} else {
annotationView?.annotation = annotation
}
annotationView?.showInformation()
return annotationView
}
}
我已经将标签的名称更改为 informationLabel
以使其更明确一点,它是一个 View ,不要与模型属性 information
混淆>,我们新的 MyAnnotation
类。此外,我建议使用比 MyAnnotation
和 MyPinAnnotationView
更有意义的类名,也许使用一些能更好地捕捉这两个类的功能意图的名称。
无论如何,如您所见,当您为此注释 View 设置 annotation
时,它会更新标签 text
。但它还通过 KVO 观察注释的新 information
属性,因此如果此属性稍后发生更改, View 将相应更新。
关于ios - 如何更新 MKPinAnnotationView 的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41311327/
我正在开发一个 OSX 应用程序。 我对 MKAnnotation 进行了子类化: import Foundation import MapKit class LocationAnnotation:
我想设置自定义图像而不是 map 上的普通图钉。我还希望它能够打开并为每个添加放置动画。 有什么建议吗? 编辑:这是我所拥有的一切 - (MKAnnotationView *)mapView:(MKM
根据 Apple 文档,MKPinAnnotationView 的图钉颜色有红色、绿色和紫色。还有什么办法可以得到其他颜色吗?我在文档中没有找到任何内容。 最佳答案 更多;) 还有原来的: 代码: -
我不明白为什么(理论上)与 MKPointAnnotation 关联的 MKPinAnnotationView 没有出现在 map 上。事实上,图钉出现了,但它不是应有的紫色... 这是代码: MKP
我正在使用 iOS Mapkit 来放置代表包的图钉。初始负载时一切正常。 我需要做的是,当用户位置在最近包裹的100m半径内时,我需要将引脚更改为“解锁”状态(目前可能只是更改颜色)。我已经检测到最
我有以下代码可以在我的 map 中删除并添加新图钉: - (IBAction)setLocation:(id)sender{ NSMutableArray *toRemove = [NSMut
我过去有过使用 MKMapView 和 MKPointAnnotation 的经验,我用它们在 map 上放置了一些图钉。这次我尝试更进一步,使用 MKPinAnnotationView 来编写标签和
编辑:将标题从:“Double tap ..”更改为“Double selection touch..” 我需要在我的应用程序中至少检测到对 MKPinAnnotationView 的第二次触摸。目前
我必须为不同的引脚设置自定义图像。使用我的代码,我可以为它们中的每一个设置自定义颜色,但是当我尝试设置自定义图像而不是颜色时,它不起作用。 - (MKAnnotationView *)mapView:
我想要一个 MKMapView 显示带有公开按钮的注释,这会导致像 the Golden Gate Bridge annotation in this Apple sample app 这样的 Vie
为 MKPinAnnotationView 上传自定义图像后,我注意到图钉偏离了中心。引脚应该位于路线折线上的一个点上,并且位于 mkcircle 的中心;但是,图钉似乎位于多段线的右侧和中心偏北的位
有谁知道为什么用于 MKPinAnnotationView 的图像应该稍微偏移(向上和向右),我使用的代码是: pinView = [[MKPinAnnotationView alloc] initW
MKPinAnnotationView pin 的宽度和高度是多少像素? 编辑:更具体地说,是 iPhone 屏幕上包含图钉的假想矩形的宽度和高度。 最佳答案 根据下面的代码,图钉图像和 MKPinA
我继承了一个引发此警告的项目 Incompatible pointer types assigning to 'MKPinAnnotationView *' from 'MKAnnotationVie
我正在尝试在 MKPinAnnotationView 后面添加图像。看起来在这里执行此操作应该相当容易: - (void)mapView:(MKMapView *)mapView didAddAnno
我添加了 MKPinAnnotationView 和 setDragAble。我的代码在这里 - (MKAnnotationView *)mapView:(MKMapView *)mapView vi
我正在使用 MKPinAnnotationView在我的应用程序中。 我正在设置 MapView对象作为委托(delegate),并使用此代码自定义我的 AnnotationView func
我遇到了一个奇怪的问题,即初始的一组图钉未显示自定义图像。 在我的 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotati
我正在尝试根据某些条件为 MKPinAnnotationView 创建三个自定义图钉图像注释。在这种情况下,我将遍历自行车站对象的 for 循环,检查可用自行车的数量是否为 0,如果是,则设置一个 p
我过去有一些使用 MKMapView 和 MKPointAnnotation 的经验,我曾用它们在 map 上放置一些图钉。这次我想更进一步,使用 MKPinAnnotationView 来写一个标签
我是一名优秀的程序员,十分优秀!