Swift MkMapView로 지도 애플리케이션(10) 만들기 - 긴 핀 위치에 클립을 세워 거리를 표시한다.

기사 목록


Swift MkMapView로 지도 애플리케이션을 만들었습니다(기사 일람).

MkMapView addannotation으로 머리핀을 세워주세요.


MkMapView의 addannotation 함수를 사용하여 맵에 마우스를 배치합니다.

  • 샘플: MkMapView addannotation 함수
    var mapView: MKMapView!
    var pointAno: MKPointAnnotation = MKPointAnnotation()
    
    // ピンを定義する
    pointAno.coordinate = center // 座標(CLLocationCoordinate2D)
    pointAno.title = "タイトル"
    pointAno.subtitle = "サブタイトル(ピンをタップすると表示される)"
    
    // MapViewにピンを立てる
    mapView.addAnnotation(pointAno)
    
  • MkMapView의 removeAnnotation 함수를 사용하여 스레드를 제거합니다.

  • 샘플: MkMapView의 removeAnnotation 함수
    mapView.removeAnnotation(pointAno)
    
  • 긴 헤드셋이 끝날 때, 핀을 헤드셋 위치에 세운다


  • ViewController.swift에서 MKPoint Annotation 변수 정의하기
    ViewController.swift: MKPoint Annotation 변수 정의
        class ViewController:   UIViewController,
                                CLLocationManagerDelegate,
                                UIGestureRecognizerDelegate {
    
            @IBOutlet var mapView: MKMapView!
            var locManager: CLLocationManager!
            @IBOutlet var longPressGesRec: UILongPressGestureRecognizer!
            var pointAno: MKPointAnnotation = MKPointAnnotation()
    

  • 긴 헤드셋이 끝날 때, 핀을 헤드셋 위치에 세운다
    긴 헤드 위치를 얻는 방법 참조Swift MkMapView로 지도 응용(08) 제작 - 긴 헤드 위치의 위도 경도를 얻다.
    ViewController.swift: 긴 헤드셋이 끝났을 때 머리핀을 헤드셋 위치에 세운다
    // UILongPressGestureRecognizerのdelegate:ロングタップを検出する
    @IBAction func mapViewDidLongPress(_ sender: UILongPressGestureRecognizer) {
        // ロングタップ開始
        if sender.state == .began {
        }
        // ロングタップ終了(手を離した)
        else if sender.state == .ended {
            // タップした位置(CGPoint)を指定してMkMapView上の緯度経度を取得する
            let tapPoint = sender.location(in: view)
            let center = mapView.convert(tapPoint, toCoordinateFrom: mapView)
    
            let lonStr = center.longitude.description
            let latStr = center.latitude.description
            print("lon : " + lonStr)
            print("lat : " + latStr)
    
            // 現在位置とタッウプした位置の距離(m)を算出する
            let distance = calcDistance(mapView.userLocation.coordinate, center)
            print("distance : " + distance.description)
    
            // ロングタップを検出した位置にピンを立てる
            pointAno.coordinate = center
            mapView.addAnnotation(pointAno)
        }
    }
    
    이 처리는 다음과 같다.
            // ロングタップを検出した位置にピンを立てる
            pointAno.coordinate = center
            mapView.addAnnotation(pointAno)
    

  • 핀에 현재 위치의 거리 표시하기
    바늘과 현재 위치의 거리를 계산하는 방법은 참조Swift MkMapView로 지도 응용프로그램 제작(09) - 클릭 위치와 현재 위치의 거리를 길게 구하기.
    1m=1.09361yard이기 때문에 yard를 원한다면 1.09361을 곱할 수 있습니다.
    ViewController.swift: 현재 위치와의 거리를 트랙에 표시합니다
    
            // ピンに設定する文字列を生成する
            var str:String = Int(distance).description
            str = str + " m"
    
            // yard
            let yardStr = Int(distance * 1.09361)
            str = str + " / " + yardStr.description + " yard"
    
            if pointAno.title != str {
                // ピンまでの距離に変化があればtiteを更新する
                pointAno.title = str
                mapView.addAnnotation(pointAno)
            }
    
    ▶PC 시뮬레이터를 켜서 긴 핀까지의 거리를 확인할 준비를 하세요.

  • 긴 헤더 시작 시 낡은 트랙 삭제


  • 긴 헤더 시작 시 낡은 트랙 삭제
    ViewController.swift: 긴 헤더 시작 시 오래된 트랙 삭제
    // UILongPressGestureRecognizerのdelegate:ロングタップを検出する
    @IBAction func mapViewDidLongPress(_ sender: UILongPressGestureRecognizer) {
        // ロングタップ開始
        if sender.state == .began {
            // ロングタップ開始時に古いピンを削除する
            mapView.removeAnnotation(pointAno)
        }
    
  • 현재 위치 업데이트 시 거리 재업데이트


  • 현재 위치 업데이트 시 거리 재업데이트
    ViewController.swift: 현재 위치 업데이트 시 거리 업데이트
    // CLLocationManagerのdelegate:現在位置取得
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]) {
    
        // 現在位置とタッウプした位置の距離(m)を算出する
        let distance = calcDistance(mapView.userLocation.coordinate, pointAno.coordinate)
    
        if (0 != distance) {
            // ピンに設定する文字列を生成する
            var str:String = Int(distance).description
            str = str + " m"
    
            // yard
            let yardStr = Int(distance * 1.09361)
            str = str + " / " + yardStr.description + " yard"
    
            if pointAno.title != str {
                // ピンまでの距離に変化があればtitleを更新する
                pointAno.title = str
                mapView.addAnnotation(pointAno)
            }
        }
    }
    
    만약 수직 직매가 없다면distance는 0이 될 것이다.
  • 기사 목록


    Swift MkMapView로 지도 애플리케이션을 만들었습니다(기사 일람).

    좋은 웹페이지 즐겨찾기