Created
May 11, 2017 21:08
-
-
Save DonMag/8cdcdcffba56f990c597d319cc22469e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // MapSampleViewController.swift | |
| // verytmp | |
| // | |
| // Created by Don Mag on 5/11/17. | |
| // Copyright © 2017 DonMag. All rights reserved. | |
| // | |
| import UIKit | |
| import MapKit | |
| class MapSampleViewController: UIViewController { | |
| var panView: UIView! | |
| var mapView: MKMapView! | |
| var regionSize = 1000.0 | |
| var startPoint = CGPoint.zero | |
| var startCoord = CLLocationCoordinate2D(latitude: 48.8582, longitude: 2.2945) | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| let pv = UIView() | |
| let mv = MKMapView() | |
| pv.translatesAutoresizingMaskIntoConstraints = false | |
| mv.translatesAutoresizingMaskIntoConstraints = false | |
| self.view.addSubview(pv) | |
| self.view.addSubview(mv) | |
| mv.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 8.0).isActive = true | |
| mv.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -8.0).isActive = true | |
| mv.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -8.0).isActive = true | |
| mv.heightAnchor.constraint(equalTo: mv.widthAnchor).isActive = true | |
| pv.widthAnchor.constraint(equalTo: mv.widthAnchor).isActive = true | |
| pv.centerXAnchor.constraint(equalTo: mv.centerXAnchor).isActive = true | |
| pv.bottomAnchor.constraint(equalTo: mv.topAnchor, constant: -16).isActive = true | |
| pv.heightAnchor.constraint(equalToConstant: 60.0).isActive = true | |
| pv.backgroundColor = .blue | |
| let pan = UIPanGestureRecognizer(target: self, action: #selector(self.didPan(_:))) | |
| pv.addGestureRecognizer(pan) | |
| panView = pv | |
| mapView = mv | |
| let region = MKCoordinateRegionMakeWithDistance(startCoord, regionSize, regionSize) | |
| mapView.setRegion(region, animated: false) | |
| } | |
| func didPan(_ sender: Any) { | |
| if let recognizer = sender as? UIPanGestureRecognizer { | |
| let trans = recognizer.translation(in: recognizer.view) | |
| switch recognizer.state { | |
| case .began: | |
| startPoint = trans | |
| startCoord = mapView.centerCoordinate | |
| break | |
| case .changed: | |
| let delta = startPoint.x - trans.x | |
| let newCoord = CLLocationCoordinate2D(latitude: startCoord.latitude + Double(delta / 10000), longitude: startCoord.longitude) | |
| let region = MKCoordinateRegionMakeWithDistance(newCoord, regionSize, regionSize) | |
| self.mapView.setRegion(region, animated: false) | |
| break | |
| default: | |
| break | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment