【发布时间】:2017-05-09 17:49:36
【问题描述】:
我有一些使用MKMapView 和MKPointAnnotation 的经验,我曾经在地图上放置一些图钉。
这次我想更进一步,使用MKPinAnnotationView,写一个标签和一些别针。
不幸的是,它并没有像我预期的那样工作。
这是我想做的:
我有一张地图(一个 MKMapView 对象),当我触摸它时,我在触摸点上放了一个大头针,然后执行一些计算,这给了我地图上的第二个点。我在地图上放了第二个图钉(位于第二个点),在最后一个图钉上我想放一个标签,说“Hello Second!”,但是当图钉改变位置时,这个标签需要更新。
以下是相关代码:
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)。
现在标签显示为第一组,但我不知道如何更新它。
-
第二个问题,必须有更好的方法来测试我正在处理的引脚。线路:
//* 我需要更好的测试来检查这不是接触点!
【问题讨论】:
-
如果您不介意,您会分享您的演示吗?我可以理解您的问题并发布答案
-
很好,但是让我知道您需要知道的内容不在我的帖子中。我已经在帖子中分享了我能想到的关于这个主题的所有内容。
-
检查这个答案我认为这会帮助你stackoverflow.com/questions/24467408/…
-
嗯?但我可以添加一个 MKAnnotationView。我的问题是在需要时更新我添加的标签的内容。
-
意味着如果您点击其他地方,那么您必须使用 ? 更新您的标签
标签: ios mkmapview mkpinannotationview mkmapviewdelegate mkpointannotation