Skip to content

Instantly share code, notes, and snippets.

@ravisharmaa
Created November 3, 2020 16:01
Show Gist options
  • Select an option

  • Save ravisharmaa/69d37418d509953d3609fe5f3c7df415 to your computer and use it in GitHub Desktop.

Select an option

Save ravisharmaa/69d37418d509953d3609fe5f3c7df415 to your computer and use it in GitHub Desktop.
import UIKit
import Combine
import MapKit
class VehiclesBrowserViewController: UIViewController {
// MARK:- Properties
// subscription to hold on to the publisher.
var subscription: Set<AnyCancellable> = []
// map view
fileprivate lazy var mapView: MKMapView = {
let mapView = MKMapView()
mapView.translatesAutoresizingMaskIntoConstraints = false
mapView.mapType = .standard
let location = CLLocationCoordinate2D(latitude: 53.394655, longitude: 9.757589)
let span = MKCoordinateSpan(latitudeDelta: 5, longitudeDelta: 5)
let region = MKCoordinateRegion(center: location, span: span)
mapView.setRegion(mapView.regionThatFits(region), animated: true)
mapView.delegate = self
return mapView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
configureMapView()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationController?.isNavigationBarHidden = true
}
func configureMapView() {
view.addSubview(mapView)
NSLayoutConstraint.activate([
mapView.topAnchor.constraint(equalTo: view.topAnchor),
mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
mapView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])
}
fileprivate func addAnnotaionFromResponse(response: PoiList) {
let location = CLLocationCoordinate2D(latitude: response.latitude!, longitude: response.longitude!)
let regionOne = MKCoordinateRegion(center: location, latitudinalMeters: CLLocationDistance(exactly: 10)!, longitudinalMeters: CLLocationDistance(exactly: 4500)!)
mapView.setRegion(mapView.regionThatFits(regionOne), animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = response.type
mapView.addAnnotation(annotation)
}
fileprivate func prepareRequest(longtitude: CLLocationDegrees, latitidue: CLLocationDegrees) {
showLoadingView()
NetworkService.shared.sendRequest(to: "poi/v1", method: .get, model:PoiListResponse.self , queryItems: [
"p2Lat":latitidue.description,
"p1Lon":longtitude.description,
"p1Lat": 53.694865.description,
"p2Lon":10.099891.description,
])
.receive(on: RunLoop.main)
.sink { (_) in
//
} receiveValue: { [self](response) in
for data in response.poiList {
addAnnotaionFromResponse(response: data)
}
dismissLoadingView()
}.store(in: &subscription)
}
}
extension VehiclesBrowserViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "id")
annotationView.canShowCallout = true
return annotationView
}
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
if !animated {
mapView.removeAnnotations(self.mapView.annotations)
prepareRequest(longtitude: mapView.region.center.longitude, latitidue: mapView.region.center.latitude)
}
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let region = MKCoordinateRegion(center: view.annotation!.coordinate, span: mapView.region.span)
mapView.setRegion(region, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment