Skip to content

Commit

Permalink
#79 [Feat] 유저 이동경로 그리기 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
soletree committed Aug 15, 2024
1 parent d008d30 commit 96372a8
Showing 1 changed file with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,58 @@
// Created by sole on 8/14/24.
//

import ComposableArchitecture
import NMapsMap
import UIKit
import SwiftUI
import Combine

final class MapViewController: UIViewController {
private let store: StoreOf<MapFeature> = .init(initialState: .init(), reducer: { MapFeature() })
private var cancellable: Set<AnyCancellable> = .init()

private var naverMapView: NMFNaverMapView!
private var locationManager: LocationManager!

private let pathOverlay: NMFPath = {
let pathOverlay: NMFPath = .init()
pathOverlay.color = UIColor(.brandColor(color: .main))
pathOverlay.outlineWidth = 0
return pathOverlay
}()

init() {
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()
self.setUpLocationManager()
self.naverMapView = .init(frame: view.frame)
self.setUpMapView()
self.setUpCamera()
self.setUpMapMarker()
view.addSubview(naverMapView)

store.publisher
.map{ $0.locations }
.sink{ self.overlayMapPath(locations: $0) }
.store(in: &cancellable)
}

/// locationManager 관련 설정을 세팅합니다.
func setUpLocationManager() {
self.locationManager = .init()
self.locationManager = .init(store: store)
self.locationManager.requestAuthorization()
self.locationManager.startUpdatingLocation()
}

/// 지도 interaction 관련 설정을 세팅합니다.
/// 지도 관련 설정을 세팅합니다.
func setUpMapView() {
self.naverMapView = .init(frame: view.frame)
self.naverMapView.showCompass = false
self.naverMapView.showZoomControls = false
self.naverMapView.mapView.isScrollGestureEnabled = false
Expand All @@ -55,8 +82,43 @@ final class MapViewController: UIViewController {
self.naverMapView.mapView.moveCamera(cameraUpdate)
}

func requestLocations() -> [CLLocationCoordinate2D] {
self.locationManager.locations.map{ $0.coordinate }
// FIXME: UIImage로 Rendering이 제대로 되지 않는 문제 해결 해야 함.
func setUpMapMarker() {
let locationOverlay = self.naverMapView.mapView.locationOverlay
let markerVC = SCMapMarker(isFocused: true).asUIViewController()
// let markerUIImage: UIImage = markerVC.view.asUIImage(bounds: self.view.bounds)
let markerUIImage: UIImage = .apple
let overlayImage: NMFOverlayImage = .init(image: markerUIImage)
locationOverlay.icon = overlayImage
}

/// 유저가 지나온 경로를 그립니다.
func overlayMapPath(locations: [NMGLatLng]) {
// point가 2개 이상인 경우에만 실행됩니다.
guard locations.count > 1
else { return }
self.pathOverlay.path = .init(points: locations)
self.pathOverlay.mapView = self.naverMapView.mapView
}

deinit {
cancellable.forEach{ $0.cancel() }
}
}

extension UIView {
func asUIImage(bounds: CGRect) -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: bounds)
let uiImage = renderer.image { context in
self.layer.render(in: context.cgContext)
}
return uiImage
}
}


extension View {
func asUIViewController() -> UIViewController {
UIHostingController(rootView: self)
}
}

0 comments on commit 96372a8

Please sign in to comment.