{"id":403967,"date":"2024-06-29T17:26:00","date_gmt":"2024-06-29T17:26:00","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=403967"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=403967","title":{"rendered":"<span>\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f Yandex MapKit \u0432 SwiftUI \u043f\u0440\u043e\u0435\u043a\u0442. \u0427\u0430\u0441\u0442\u044c 1<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u0445 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e. <\/p>\n<p>\u0421\u043e\u0432\u0441\u0435\u043c \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u043a\u0430\u0440\u0442\u044b \u043e\u0442 Apple \u043d\u0430 \u042f\u043d\u0434\u0435\u043a\u0441 \u043a\u0430\u0440\u0442\u044b. \u041d\u0438\u0436\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043e Yandex MapKit.<\/p>\n<p>Yandex MapKit \u2014 \u044d\u0442\u043e \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u042f\u043d\u0434\u0435\u043a\u0441.\u041a\u0430\u0440\u0442 \u0432 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0434\u043b\u044f iOS \u0438 Android.<\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u043b\u0430\u0441\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 Yandex MapKit \u0441\u043e\u0437\u0434\u0430\u043d\u0430 \u0434\u043b\u044f UIKit, \u043d\u043e \u043d\u0430\u0448\u0430 \u0446\u0435\u043b\u044c \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u044d\u0442\u043e \u0434\u0435\u043b\u043e \u043d\u0430 SwiftUI. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u0431\u0435\u0433\u0430\u0435\u043c \u043a \u0441\u0442\u0430\u0440\u043e\u0439 \u0434\u043e\u0431\u0440\u043e\u0439 \u043f\u0438\u043a\u0447\u0435:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/012\/004\/024\/0120040245555641cdad44431df2c67d.png\" width=\"371\" height=\"439\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/012\/004\/024\/0120040245555641cdad44431df2c67d.png\"\/><\/figure>\n<p>\u0411\u0443\u0434\u0435\u043c \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043a\u043b\u044e\u0447 \u0443 \u042f\u043d\u0434\u0435\u043a\u0441\u0430 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442, \u0434\u043b\u044f \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u043b\u0435\u043d\u0438\u044f \u0441 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u043e\u0439 \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u044f\u044e <a href=\"https:\/\/yandex.ru\/dev\/maps\/mapkit\/doc\/ios-quickstart\/concepts\/ios\/quickstart.html\" rel=\"noopener noreferrer nofollow\">\u0441\u0441\u044b\u043b\u043a\u0443<\/a>. <\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 AppDelegate, \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0432 SwiftUI \u043f\u0440\u043e\u0435\u043a\u0442\u0435 <a href=\"https:\/\/paigeshin1991.medium.com\/how-to-set-up-your-swiftui-project-with-appdelegate-and-scenedelegate-64cf5566e1e7\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u0438\u0434\u0435\u043c \u0432 \u0442\u0430\u0440\u0433\u0435\u0442 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 -> Info \u0438 \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c Privacy<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/753\/417\/3d2\/7534173d21fb30f859ddf86ff5ed173e.png\" width=\"1396\" height=\"50\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/753\/417\/3d2\/7534173d21fb30f859ddf86ff5ed173e.png\"\/><\/figure>\n<p>\u0412 \u043a\u043b\u0430\u0441\u0441\u0435 AppDelegate \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u042f\u043d\u0434\u0435\u043a\u0441\u0430 \u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0432\u0430\u0448 API \u043a\u043b\u044e\u0447 \u0438\u0437 \u043a\u0430\u0431\u0438\u043d\u0435\u0442\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u042f\u043d\u0434\u0435\u043a\u0441\u0430<\/p>\n<pre><code class=\"swift\">func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {     YMKMapKit.setApiKey(\"\u0412\u0430\u0448 API-\u043a\u043b\u044e\u0447\")     YMKMapKit.sharedInstance() }<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 LocationManager, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435: <\/p>\n<pre><code class=\"swift\">import Foundation import CoreLocation import YandexMapsMobile import Combine<\/code><\/pre>\n<p>LocationManager \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u0437\u0430 \u0432\u0441\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 \u043a\u0430\u0440\u0442\u0430\u043c\u0438, \u043e\u0431\u044a\u044f\u0432\u0438\u043c \u0432 \u043d\u0435\u043c \u0441\u0430\u043c\u0443 \u043a\u0430\u0440\u0442\u0443 \u0438 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e manager, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442\u0441\u044f \u043e\u0442 \u043a\u043b\u0430\u0441\u0441\u0430 CLLocationManager, \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/p>\n<pre><code class=\"swift\">class LocationManager: NSObject{      lazy var map : YMKMap = {         return mapView.mapWindow.map     }()      private let manager = CLLocationManager()      let mapView = YMKMapView(frame: CGRect.zero)     override init(){         super.init()     } }<\/code><\/pre>\n<p>\u0423\u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u043e\u0442 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 CLLocationManagerDelegate \u0438 \u0432 init() \u043f\u043e\u0434\u043f\u0438\u0448\u0435\u043c \u0434\u0435\u043b\u0435\u0433\u0430\u0442 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430 \u0438 \u0432\u044b\u0437\u043e\u0432\u0435\u043c \u0443 \u043d\u0435\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0433\u0435\u043e\u043f\u043e\u0437\u0438\u0446\u0438\u0438. <\/p>\n<pre><code class=\"swift\">class LocationManager: NSObject, CLLocationManagerDelegate<\/code><\/pre>\n<pre><code class=\"swift\"> override init(){         super.init()         manager.delegate = self         manager.startUpdatingLocation()     }<\/code><\/pre>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 LocationManager \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u0435\u043b\u0435\u0433\u0430\u0442\u0430 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0441\u0442\u0430\u0442\u0443\u0441\u0430 \u0438 \u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0433\u0435\u043e\u043f\u043e\u0437\u0438\u0446\u0438\u0438<\/p>\n<pre><code class=\"swift\">func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {         if status == .authorizedWhenInUse {             self.manager.startUpdatingLocation()         }     }<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/p>\n<pre><code class=\"swift\">@Published var lastUserLocation: CLLocation? = nil<\/code><\/pre>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u043b\u0443\u0448\u0430\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e: <\/p>\n<pre><code class=\"swift\">  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {       self.lastUserLocation = locations.last  }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e UI \u0447\u0430\u0441\u0442\u0438. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c SwiftUIView \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c MapView \u0438 \u043e\u0431\u0435\u0440\u043d\u0435\u043c \u0432 ZStack. \u041e\u0431\u044a\u044f\u0432\u0438\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 LocationManager<\/p>\n<pre><code class=\"swift\">  @ObservedObject var locationManager = LocationManager()<\/code><\/pre>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a Yandex MapKit \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 UIKit, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 YandexMapView \u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u0441\u044f \u043e\u0442 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 UIViewRepresentable. <\/p>\n<p>\u041e\u0431\u044a\u044f\u0432\u0438\u043c EnviromentObject \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u043e\u0442 \u043a\u043b\u0430\u0441\u0441\u0430 LocationManager \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e makeUIView(), \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u0435\u0440\u043d\u0435\u043c \u043a\u0430\u043a View \u0440\u0430\u043d\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0435 mapView \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 LocationManager<\/p>\n<pre><code class=\"swift\">import SwiftUI import YandexMapsMobile import Combine  struct YandexMapView: UIViewRepresentable {     @EnvironmentObject var locationManager : YaLocationManager     func makeUIView(context: Context) -> YMKMapView {         return locationManager.mapView     }       func updateUIView(_ mapView: YMKMapView, context: Context) {} } <\/code><\/pre>\n<p>\u0412\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u0432 LocationManager \u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e centerMapLocation, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0430 \u0432\u0445\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 target, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u0434\u0430\u0432\u0430\u0442\u044c \u0434\u043e\u043b\u0433\u043e\u0442\u0443 \u0438 \u0448\u0438\u0440\u043e\u0442\u0443, \u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 map, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u042f\u043d\u0434\u0435\u043a\u0441 \u043a\u0430\u0440\u0442\u0443.<\/p>\n<pre><code class=\"swift\">  func centerMapLocation(target location: YMKPoint?, map: YMKMapView) {          guard let location = location else { print(\"Failed to get user location\"); return }          map.mapWindow.map.move(              with: YMKCameraPosition(target: location, zoom: 18, azimuth: 0, tilt: 0),              animationType: YMKAnimation(type: YMKAnimationType.smooth, duration: 0.5)          )      }<\/code><\/pre>\n<p>\u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u043e\u043a\u0430\u0437\u0430 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0433\u0435\u043e\u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043e\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u0431\u0440\u0430\u0442\u044c lastUserLocation \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u043c\u0435\u0442\u043e\u0434 centerMapLocation, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435.<\/p>\n<pre><code class=\"swift\"> func currentUserLocation(){         if let myLocation = lastKnownLocation {              centerMapLocation(target: YMKPoint(latitude: myLocation.coordinate.latitude, longitude: myLocation.coordinate.longitude), map: mapView)       }  }<\/code><\/pre>\n<p>\u041e\u0431\u044a\u044f\u0432\u0438\u043c \u0432 ZStack YandexMapView \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u0435\u0439 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441<\/p>\n<pre><code class=\"swift\"> ZStack{     YandexMapView()     .edgesIgnoringSafeArea(.all)     .environmentObject(locationManager) }<\/code><\/pre>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043b\u0438\u0448\u044c \u0432 OnAppear \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 currentUserLocation<\/p>\n<pre><code class=\"swift\">.onAppear{         locationManager.currentUserLocation() }<\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c, \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0443\u044e \u042f\u043d\u0434\u0435\u043a\u0441 \u041a\u0430\u0440\u0442\u0443 \u0432 \u0432\u0430\u0448\u0435\u043c SwiftUI \u043f\u0440\u043e\u0435\u043a\u0442\u0435<\/p>\n<details class=\"spoiler\">\n<summary>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/summary>\n<div class=\"spoiler__content\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/f9b\/468\/38d\/f9b46838d6564760d53f94cadc9c6c32.jpg\" width=\"591\" height=\"1280\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f9b\/468\/38d\/f9b46838d6564760d53f94cadc9c6c32.jpg\" data-blurred=\"true\"\/><\/figure>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u044f\u044e \u043a\u043e\u0434 \u0432\u0441\u0435\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432: <\/p>\n<details class=\"spoiler\">\n<summary>LocationManager<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"swift\">class LocationManager: NSObject, CLLocationManagerDelegate, ObservableObject{          private let manager = CLLocationManager()     let mapView = YMKMapView(frame: CGRect.zero)     @Published var lastUserLocation: CLLocation? = nil     lazy var map : YMKMap = {         return mapView.mapWindow.map     }()          override init() {         super.init()         self.manager.delegate = self     }          func currentUserLocation(){         if let myLocation = lastUserLocation {             centerMapLocation(target: YMKPoint(latitude: myLocation.coordinate.latitude, longitude: myLocation.coordinate.longitude), map: mapView )         }     }          func centerMapLocation(target location: YMKPoint?, map: YMKMapView) {                  guard let location = location else { print(\"Failed to get user location\"); return }                  map.mapWindow.map.move(             with: YMKCameraPosition(target: location, zoom: 18, azimuth: 0, tilt: 0),             animationType: YMKAnimation(type: YMKAnimationType.smooth, duration: 0.5)         )     }          func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {         if status == .authorizedWhenInUse {             self.manager.startUpdatingLocation()         }     }          func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {         \/\/ Notify listeners that the user has a new location         self.lastUserLocation = locations.last     } }  <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>MapView<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"swift\">struct MapView: View {     @ObservedObject var locationManager = LocationManager()     var body: some View {         ZStack{             YandexMapsView().edgesIgnoringSafeArea(.all).environmentObject(locationManager)         }.onAppear{             locationManager.currentUserLocation()         }     } } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>YandexMapView<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"swift\">struct YandexMapView: UIViewRepresentable {     @EnvironmentObject var locationManager : YaLocationManager     func makeUIView(context: Context) -> YMKMapView {         return locationManager.mapView     }               func updateUIView(_ mapView: YMKMapView, context: Context) {} }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u043f\u043e\u0438\u0441\u043a \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443, \u043f\u043e\u0437\u0438\u0446\u0438\u044f \u043d\u0430 \u043a\u0430\u0440\u0442\u0435, \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u0430\u0434\u0440\u0435\u0441\u0430 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0430\u0434\u0440\u0435\u0441\u0430 \u043f\u043e \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0430\u043c \u0438\u0437 \u0446\u0435\u043d\u0442\u0440\u0430 \u043a\u0430\u043c\u0435\u0440\u044b.<\/p>\n<p>\u0412\u0441\u0435\u043c \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435. <\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/728772\/\"> https:\/\/habr.com\/ru\/articles\/728772\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u0445 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e. <\/p>\n<p>\u0421\u043e\u0432\u0441\u0435\u043c \u043d\u0435\u0434\u0430\u0432\u043d\u043e \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u043a\u0430\u0440\u0442\u044b \u043e\u0442 Apple \u043d\u0430 \u042f\u043d\u0434\u0435\u043a\u0441 \u043a\u0430\u0440\u0442\u044b. \u041d\u0438\u0436\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043e Yandex MapKit.<\/p>\n<p>Yandex MapKit \u2014 \u044d\u0442\u043e \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u042f\u043d\u0434\u0435\u043a\u0441.\u041a\u0430\u0440\u0442 \u0432 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0434\u043b\u044f iOS \u0438 Android.<\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u043b\u0430\u0441\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 Yandex MapKit \u0441\u043e\u0437\u0434\u0430\u043d\u0430 \u0434\u043b\u044f UIKit, \u043d\u043e \u043d\u0430\u0448\u0430 \u0446\u0435\u043b\u044c \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u044d\u0442\u043e \u0434\u0435\u043b\u043e \u043d\u0430 SwiftUI. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u0431\u0435\u0433\u0430\u0435\u043c \u043a \u0441\u0442\u0430\u0440\u043e\u0439 \u0434\u043e\u0431\u0440\u043e\u0439 \u043f\u0438\u043a\u0447\u0435:<\/p>\n<figure class=\"\"><\/figure>\n<p>\u0411\u0443\u0434\u0435\u043c \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043a\u043b\u044e\u0447 \u0443 \u042f\u043d\u0434\u0435\u043a\u0441\u0430 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0432 \u043f\u0440\u043e\u0435\u043a\u0442, \u0434\u043b\u044f \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u043b\u0435\u043d\u0438\u044f \u0441 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u043e\u0439 \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u044f\u044e <a href=\"https:\/\/yandex.ru\/dev\/maps\/mapkit\/doc\/ios-quickstart\/concepts\/ios\/quickstart.html\" rel=\"noopener noreferrer nofollow\">\u0441\u0441\u044b\u043b\u043a\u0443<\/a>. <\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 AppDelegate, \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0432 SwiftUI \u043f\u0440\u043e\u0435\u043a\u0442\u0435 <a href=\"https:\/\/paigeshin1991.medium.com\/how-to-set-up-your-swiftui-project-with-appdelegate-and-scenedelegate-64cf5566e1e7\" rel=\"noopener noreferrer nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u0438\u0434\u0435\u043c \u0432 \u0442\u0430\u0440\u0433\u0435\u0442 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 -> Info \u0438 \u043f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c Privacy<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0412 \u043a\u043b\u0430\u0441\u0441\u0435 AppDelegate \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u042f\u043d\u0434\u0435\u043a\u0441\u0430 \u0438 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0432\u0430\u0448 API \u043a\u043b\u044e\u0447 \u0438\u0437 \u043a\u0430\u0431\u0438\u043d\u0435\u0442\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u042f\u043d\u0434\u0435\u043a\u0441\u0430<\/p>\n<pre><code class=\"swift\">func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {     YMKMapKit.setApiKey(\"\u0412\u0430\u0448 API-\u043a\u043b\u044e\u0447\")     YMKMapKit.sharedInstance() }<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 LocationManager, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435: <\/p>\n<pre><code class=\"swift\">import Foundation import CoreLocation import YandexMapsMobile import Combine<\/code><\/pre>\n<p>LocationManager \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u0437\u0430 \u0432\u0441\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0441 \u043a\u0430\u0440\u0442\u0430\u043c\u0438, \u043e\u0431\u044a\u044f\u0432\u0438\u043c \u0432 \u043d\u0435\u043c \u0441\u0430\u043c\u0443 \u043a\u0430\u0440\u0442\u0443 \u0438 \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e manager, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u0442\u0441\u044f \u043e\u0442 \u043a\u043b\u0430\u0441\u0441\u0430 CLLocationManager, \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/p>\n<pre><code class=\"swift\">class LocationManager: NSObject{      lazy var map : YMKMap = {         return mapView.mapWindow.map     }()      private let manager = CLLocationManager()      let mapView = YMKMapView(frame: CGRect.zero)     override init(){         super.init()     } }<\/code><\/pre>\n<p>\u0423\u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u043e\u0442 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 CLLocationManagerDelegate \u0438 \u0432 init() \u043f\u043e\u0434\u043f\u0438\u0448\u0435\u043c \u0434\u0435\u043b\u0435\u0433\u0430\u0442 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0430 \u0438 \u0432\u044b\u0437\u043e\u0432\u0435\u043c \u0443 \u043d\u0435\u0433\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0433\u0435\u043e\u043f\u043e\u0437\u0438\u0446\u0438\u0438. <\/p>\n<pre><code class=\"swift\">class LocationManager: NSObject, CLLocationManagerDelegate<\/code><\/pre>\n<pre><code class=\"swift\"> override init(){         super.init()         manager.delegate = self         manager.startUpdatingLocation()     }<\/code><\/pre>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 LocationManager \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u0435\u043b\u0435\u0433\u0430\u0442\u0430 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0441\u0442\u0430\u0442\u0443\u0441\u0430 \u0438 \u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0433\u0435\u043e\u043f\u043e\u0437\u0438\u0446\u0438\u0438<\/p>\n<pre><code class=\"swift\">func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {         if status == .authorizedWhenInUse {             self.manager.startUpdatingLocation()         }     }<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/p>\n<pre><code class=\"swift\">@Published var lastUserLocation: CLLocation? = nil<\/code><\/pre>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u043b\u0443\u0448\u0430\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e: <\/p>\n<pre><code class=\"swift\">  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {       self.lastUserLocation = locations.last  }<\/code><\/pre>\n<p>\u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e UI \u0447\u0430\u0441\u0442\u0438. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c SwiftUIView \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c MapView \u0438 \u043e\u0431\u0435\u0440\u043d\u0435\u043c \u0432 ZStack. \u041e\u0431\u044a\u044f\u0432\u0438\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 LocationManager<\/p>\n<pre><code class=\"swift\">  @ObservedObject var locationManager = LocationManager()<\/code><\/pre>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a Yandex MapKit \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 UIKit, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 YandexMapView \u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u0441\u044f \u043e\u0442 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 UIViewRepresentable. <\/p>\n<p>\u041e\u0431\u044a\u044f\u0432\u0438\u043c EnviromentObject \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u043e\u0442 \u043a\u043b\u0430\u0441\u0441\u0430 LocationManager \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e makeUIView(), \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u0435\u0440\u043d\u0435\u043c \u043a\u0430\u043a View \u0440\u0430\u043d\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0435 mapView \u0432 \u043a\u043b\u0430\u0441\u0441\u0435 LocationManager<\/p>\n<pre><code class=\"swift\">import SwiftUI import YandexMapsMobile import Combine  struct YandexMapView: UIViewRepresentable {     @EnvironmentObject var locationManager : YaLocationManager     func makeUIView(context: Context) -> YMKMapView {         return locationManager.mapView     }       func updateUIView(_ mapView: YMKMapView, context: Context) {} } <\/code><\/pre>\n<p>\u0412\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u0432 LocationManager \u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u0438\u0432\u0430\u0442\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e centerMapLocation, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0430 \u0432\u0445\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 target, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u0434\u0430\u0432\u0430\u0442\u044c \u0434\u043e\u043b\u0433\u043e\u0442\u0443 \u0438 \u0448\u0438\u0440\u043e\u0442\u0443, \u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 map, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u042f\u043d\u0434\u0435\u043a\u0441 \u043a\u0430\u0440\u0442\u0443.<\/p>\n<pre><code class=\"swift\">  func centerMapLocation(target location: YMKPoint?, map: YMKMapView) {          guard let location = location else { print(\"Failed to get user location\"); return }          map.mapWindow.map.move(              with: YMKCameraPosition(target: location, zoom: 18, azimuth: 0, tilt: 0),              animationType: YMKAnimation(type: YMKAnimationType.smooth, duration: 0.5)          )      }<\/code><\/pre>\n<p>\u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u043e\u043a\u0430\u0437\u0430 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0433\u0435\u043e\u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043e\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u0431\u0440\u0430\u0442\u044c lastUserLocation \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u043c\u0435\u0442\u043e\u0434 centerMapLocation, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435.<\/p>\n<pre><code class=\"swift\"> func currentUserLocation(){         if let myLocation = lastKnownLocation {              centerMapLocation(target: YMKPoint(latitude: myLocation.coordinate.latitude, longitude: myLocation.coordinate.longitude), map: mapView)       }  }<\/code><\/pre>\n<p>\u041e\u0431\u044a\u044f\u0432\u0438\u043c \u0432 ZStack YandexMapView \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u0435\u0439 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441<\/p>\n<pre><code class=\"swift\"> ZStack{     YandexMapView()     .edgesIgnoringSafeArea(.all)     .environmentObject(locationManager) }<\/code><\/pre>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043b\u0438\u0448\u044c \u0432 OnAppear \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 currentUserLocation<\/p>\n<pre><code class=\"swift\">.onAppear{         locationManager.currentUserLocation() }<\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c, \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0443\u044e \u042f\u043d\u0434\u0435\u043a\u0441 \u041a\u0430\u0440\u0442\u0443 \u0432 \u0432\u0430\u0448\u0435\u043c SwiftUI \u043f\u0440\u043e\u0435\u043a\u0442\u0435<\/p>\n<details class=\"spoiler\">\n<summary>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442<\/summary>\n<div class=\"spoiler__content\">\n<figure class=\"full-width\"><\/figure>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u043a\u0440\u0435\u043f\u043b\u044f\u044e \u043a\u043e\u0434 \u0432\u0441\u0435\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432: <\/p>\n<details class=\"spoiler\">\n<summary>LocationManager<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"swift\">class LocationManager: NSObject, CLLocationManagerDelegate, ObservableObject{          private let manager = CLLocationManager()     let mapView = YMKMapView(frame: CGRect.zero)     @Published var lastUserLocation: CLLocation? = nil     lazy var map : YMKMap = {         return mapView.mapWindow.map     }()          override init() {         super.init()         self.manager.delegate = self     }          func currentUserLocation(){         if let myLocation = lastUserLocation {             centerMapLocation(target: YMKPoint(latitude: myLocation.coordinate.latitude, longitude: myLocation.coordinate.longitude), map: mapView )         }     }          func centerMapLocation(target location: YMKPoint?, map: YMKMapView) {                  guard let location = location else { print(\"Failed to get user location\"); return }                  map.mapWindow.map.move(             with: YMKCameraPosition(target: location, zoom: 18, azimuth: 0, tilt: 0),             animationType: YMKAnimation(type: YMKAnimationType.smooth, duration: 0.5)         )     }          func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {         if status == .authorizedWhenInUse {             self.manager.startUpdatingLocation()         }     }          func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {         \/\/ Notify listeners that the user has a new location         self.lastUserLocation = locations.last     } }  <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>MapView<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"swift\">struct MapView: View {     @ObservedObject var locationManager = LocationManager()     var body: some View {         ZStack{             YandexMapsView().edgesIgnoringSafeArea(.all).environmentObject(locationManager)         }.onAppear{             locationManager.currentUserLocation()         }     } } <\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>YandexMapView<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"swift\">struct YandexMapView: UIViewRepresentable {     @EnvironmentObject var locationManager : YaLocationManager     func makeUIView(context: Context) -> YMKMapView {         return locationManager.mapView     }               func updateUIView(_ mapView: YMKMapView, context: Context) {} }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u043f\u043e\u0438\u0441\u043a \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443, \u043f\u043e\u0437\u0438\u0446\u0438\u044f \u043d\u0430 \u043a\u0430\u0440\u0442\u0435, \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u0430\u0434\u0440\u0435\u0441\u0430 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0430\u0434\u0440\u0435\u0441\u0430 \u043f\u043e \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u0430\u043c \u0438\u0437 \u0446\u0435\u043d\u0442\u0440\u0430 \u043a\u0430\u043c\u0435\u0440\u044b.<\/p>\n<p>\u0412\u0441\u0435\u043c \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435. <\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/728772\/\"> https:\/\/habr.com\/ru\/articles\/728772\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-403967","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/403967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=403967"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/403967\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=403967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=403967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=403967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}