Skip to content

mapKit Polyline2

Hyebin (Helia) edited this page Oct 11, 2023 · 1 revision

μ§€λ„μœ„μ— μ‚¬μš©μž 경둜 λ‚˜νƒ€λ‚΄κΈ°

λͺ©μ°¨

1. mapView μ΄ˆκΈ°ν™” 2. Polyline 그리기 2. Polyline μ»€μŠ€ν…€

mapView μ΄ˆκΈ°ν™”

  • map에 μ‚¬μš©μžμ˜ μœ„μΉ˜λ₯Ό λ‚˜νƒ€λ‚΄κ³ , μ‚¬μš©μžμ˜ μœ„μΉ˜λ₯Ό μΆ”μ ν•˜κΈ° μœ„ν•œ locationManger의 μ„±λŠ₯을 best둜 μ„€μ •ν•©λ‹ˆλ‹€.
    func makeUIView(context: Context) -> MKMapView {
        mapView.delegate = context.coordinator
        mapView.showsUserLocation = true
        
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.delegate = context.coordinator
        return mapView
    }
  • 지도가 μ„±κ³΅μ μœΌλ‘œ λ‘œλ“œκ°€ 되면 ν˜„μž¬ μ‚¬μš©μžμ˜ μœ„μΉ˜κΈ°μ€€μœΌλ‘œ μœ„λ„, 경도 500λ―Έν„° λ²”μœ„λ‘œ ν™•λŒ€ν•΄μ„œ 지도λ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€.
    func mapViewDidFinishLoadingMap(_ mapView: MKMapView) {
        if let userLocation = mapView.userLocation.location {
            let region = MKCoordinateRegion(
                center: userLocation.coordinate,
                latitudinalMeters: 500,
                longitudinalMeters: 500
            )
            mapView.setRegion(region, animated: true)
        }
    }

Polyline 그리기

  • viewμ—μ„œ λ²„νŠΌμ„ ν΄λ¦­ν•˜λ©΄ μ‚¬μš©μžμ˜ μœ„μΉ˜ 좔적을 μ‹œμž‘ν•˜κ³ , λ²„νŠΌμ„ λ‹€μ‹œν•œλ²ˆ ν΄λ¦­ν•˜λ©΄ 좔적을 λ©ˆμΆ”λŠ” λ°©μ‹μœΌλ‘œ κ΅¬ν˜„ν–ˆμŠ΅λ‹ˆλ‹€.
    private var playButton: some View {
        Button {
            isTracking.toggle()
            
            if isTracking {
                locationManager.startUpdatingLocation()
            }else {
                locationManager.stopUpdatingLocation()
            }
        } label: {
            Image(systemName: isTracking ? "pause.circle.fill" : "play.circle.fill")
                .tint(Color.black)
                .font(.system(size: 50))
        }
    }
  • startUpdatingLocation()을 ν•˜λ©΄ μ‚¬μš©μžμ˜ μœ„μΉ˜λ₯Ό λ°›μ•„ userLocations 배열에 μ €μž₯ν•˜κ³ , 지도에 userLocationλ°°μ—΄μ˜ μš”μ†Œλ“€μ„ polyline으둜 κ·Έλ¦½λ‹ˆλ‹€.
    @State private var userLocations: [CLLocationCoordinate2D] = []

    func updateUIView(_ uiView: MKMapView, context: Context) {
        uiView.removeOverlays(uiView.overlays)
        
        if !userLocations.isEmpty {
            let polyline = MKPolyline(coordinates: userLocations, count: userLocations.count)
            uiView.addOverlay(polyline)
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.last?.coordinate {
            parent.userLocations.append(location)
        }
    }

Polyline μ»€μŠ€ν…€

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        if let polyline = overlay as? MKPolyline {
            let renderer = MKPolylineRenderer(polyline: polyline)
            renderer.strokeColor = .orange
            renderer.lineWidth = 7
            return renderer
        }
        return MKOverlayRenderer(overlay: overlay)
    }

Clone this wiki locally