MKMapView
과 MKPointAnnotation
을 사용하여 이전에 몇 가지 경험을 쌓았습니다. 일부 핀을지도에 넣었습니다. 이번에는 한 단계 더 나아가서 MKPinAnnotationView
을 사용하여 일부 핀과 함께 라벨을 작성하려고합니다.MKPinAnnotationView의 제목이 표시되지 않습니다.
불행히도, 예상대로 모든 것이 작동하지는 않습니다. 여기
는 내가하고 싶은 것입니다 :을 내가 (AN 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!"
}
} 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: MKPinAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
as? MKPinAnnotationView {
dequeuedView.annotation = annotation
view = dequeuedView
} else {
view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.canShowCallout = true
view.calloutOffset = CGPoint(x: -5, y: 0)
}
return view
}
}
예상대로 핀 배치, 무슨 일이지만, 나는 두 번째에있는 라벨을 볼 수 없습니다. 두 번째 핀 (두 번째 핀이 같은 위치에 머물러있는 경우)을 두드리면 레이블이 나타납니다 (항상 수행해야하는 것처럼). 다시 (너무 가깝지 않은) 다시 탭하면 라벨이 다시 사라집니다 (그래야 안됩니다).
내 코드 (위)에 잘못된 부분이 있습니까? 관련 팁을 제공해드립니다.
음, 다른 SDK (Google Maps API)를 사용하지 않아도됩니다. 나는 내가 올바른 일을한다면 그것이 내가 원하는대로 일할 것이라고 생각한다. – Michel
나는 단지 거친 아이디어를주고있다. 나는 구글 맵을 사용할 것을 요구하지 않고있다. – hussain