extension AddPresenter: AddPresentationLogic {
// ...
func openMap(_ delegate: YandexMapSetPointViewControllerProtocol?) {
router.routeTo(target: .addPlace(delegate))
}
// ...
}
protocol YandexMapSetPointViewControllerProtocol {
func didSelectPoint(_ place: Place)
}
@IBAction func selectButtonWasPressed(_ sender: Any) {
let place = Place()
place.name = "Указанная геопозиция"
place.point.latitude = "\(String(describing: selectedPoint!.latitude))"
place.point.longitude = "\(String(describing: selectedPoint!.longitude))"
place.addressText = selectedPointGeocoderedAddress
delegate?.didSelectPoint(place)
navigationController?.popViewController(animated: true)
}
@IBOutlet weak var mapView: YMKMapView! // и YMKUserLocationObjectListener - слушатель, получающий сведения
func onObjectAdded(with view: YMKUserLocationView) {}
func onObjectRemoved(with view: YMKUserLocationView) {}
func onObjectUpdated(with view: YMKUserLocationView, event: YMKObjectEvent) {}
userLocation = YMKPoint(latitude: view.pin.geometry.latitude, longitude: view.pin.geometry.longitude)
// MARK: -
// Params
var userLocation: YMKPoint? {
didSet {
guard userLocation != nil && userLocation?.latitude != 0 && userLocation?.longitude != 0 else { return }
if isItFirstSelection {
isItFirstSelection = false
selectedPoint = userLocation
mapView.mapWindow.map.move(
with: YMKCameraPosition.init(target: userLocation!, zoom: 16, azimuth: 0, tilt: 0),
animationType: YMKAnimation(type: YMKAnimationType.smooth, duration: 1),
cameraCallback: nil)
}
activityIndicator.stopAnimating()
}
}
// MARK: -
// Some like didLoad
setupLocationManager()
// MARK: -
// Setup
private func setupLocationManager() {
locationManager = YMKMapKit.sharedInstance()!.createLocationManager()
locationManager.subscribeForLocationUpdates(withDesiredAccuracy: 0, minTime: 10, minDistance: 0, allowUseInBackground: true, filteringMode: .on, locationListener: self)
}
// MARK: -
// MARK: YMKLocationDelegate
extension YandexMapSetPointViewController: YMKLocationDelegate {
func onLocationUpdated(with location: YMKLocation) {
userLocation = YMKPoint(latitude: location.position.latitude, longitude: location.position.longitude)
}
func onLocationStatusUpdated(with status: YMKLocationStatus) {}
}
// Params
private var locationManager: YMKLocationManager!
private var nativeLocationManager = CLLocationManager()
// MARK: -
// Some like didLoad
setupNativeLocationManager()
// MARK: -
// Setup
private func setupNativeLocationManager() {
if CLLocationManager.locationServicesEnabled() {
nativeLocationManager.delegate = self
nativeLocationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
nativeLocationManager.startUpdatingLocation()
}
}
// MARK: -
// CLLocationManagerDelegate
extension YandexMapSetPointViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
userLocation = YMKPoint(latitude: locations.last!.coordinate.latitude, longitude: locations.last!.coordinate.longitude)
}
}
// Params
private let geocoderManager = GeocoderManager.shared
// Не стал шерстить импорт на избыточность. Вроде бы все нужно
import Foundation
import Alamofire
import Alamofire_SwiftyJSON
import SwiftyJSON
import PromiseKit
import UIKit
// MARK: -
// MARK: GeocoderManager
class GeocoderManager {
static let shared = GeocoderManager()
private init() {}
func getAddressBy(latitude: String, longitude: String, completion: @escaping (Bool, String?, Error?)->()) {
GeocoderAPI().request(latitude: latitude, longitude: longitude).done { (response) in
completion(true, response.getAddress(), nil)
print("success")
}.catch { (error) in
completion(false, nil, error)
}
}
}
//
import Foundation
import Alamofire
import PromiseKit
// MARK: -
// MARK: Request
enum GeocoderRequest {
case addressRequest(String, String)
}
// MARK: -
// MARK: GeocoderRequest
extension GeocoderRequest: DefaultRequest {
var path: String {
switch self {
case .addressRequest:
return "1.x/"
}
}
var method: HTTPMethod {
switch self {
case .addressRequest:
return .get
}
}
var headers: HTTPHeaders {
return [:]
}
var parameters: [String: Any]? {
switch self {
case .addressRequest(let latitude, let longitude):
return [
"apikey" : Consts.APIKeys.yandexGeocoderKey,
"format" : "json",
"results" : 1,
"spn" : "3.552069,2.400552",
"geocode" : "\(longitude),\(latitude)"
]
}
}
func asURLRequest() throws -> URLRequest {
let url = try GlobalConsts.Links.geocoderBaseURL.asURL()// not good, need new idea for this
var urlRequest = URLRequest(url: url.appendingPathComponent(path))
urlRequest.httpMethod = method.rawValue
urlRequest.allHTTPHeaderFields = headers
switch method {
case .get:
urlRequest = try URLEncoding.default.encode(urlRequest, with: parameters)
case .post:
urlRequest = try JSONEncoding.default.encode(urlRequest, with: parameters)
case .put:
urlRequest = try JSONEncoding.default.encode(urlRequest, with: parameters)
case .patch:
urlRequest = try JSONEncoding.default.encode(urlRequest, with: parameters)
case .delete:
urlRequest = try JSONEncoding.default.encode(urlRequest, with: parameters)
default:
break
}
return urlRequest
}
}
К сожалению, не доступен сервер mySQL