-
Notifications
You must be signed in to change notification settings - Fork 0
mapKit Polyline2
Hyebin (Helia) edited this page Oct 11, 2023
·
1 revision
1. mapView μ΄κΈ°ν 2. Polyline 그리기 2. Polyline 컀μ€ν
- 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)
}
}- 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)
}
} 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)
}