Reputation: 99
I'm trying to replicate what I had in another project where I used storyboards using only code. The view controller I'm struggling with is the map view controller.
I'm trying to create a map which shows the user location, zooms to a suitable level around that location on launching and then allow the user to drop a pin on the map which will have a circle overlay around the location of the pin.
I have managed to create the map and show user location, but am struggling setting the region to zoom on the location when the map launches. I have been struggling with this for a couple of days.
I know I need to use CLlocation2D somewhere, but am not sure where/how to use this at the moment.
I also tried to setup my map using functions, code so far is below (it's a botch job from different sources trying to make it fit my goal, so apologies if it's messy)
> import UIKit
> import MapKit
> import CoreLocation
>
> class MapViewController: UIViewController, MKMapViewDelegate,
> CLLocationManagerDelegate {
> var mapView: MKMapView!
> var locationManager = CLLocationManager()
>
> override func viewDidLoad() {
> super.viewDidLoad()
>
> setupMapView()
>
> }
>
> override func viewWillAppear(_ animated: Bool) {
> super.viewWillAppear(animated)
>
> }
>
> func setupMapView() {
> let mapView = MKMapView()
>
> let leftMargin:CGFloat = 0
> let topMargin:CGFloat = 0
> let mapWidth:CGFloat = view.frame.size.width
> let mapHeight:CGFloat = view.frame.size.height
>
> mapView.frame = CGRect(x: leftMargin, y: topMargin, width: mapWidth, height: mapHeight)
>
> mapView.mapType = MKMapType.standard
> mapView.isZoomEnabled = true
> mapView.isScrollEnabled = true
> mapView.showsCompass = true
> mapView.showsScale = true
> mapView.showsUserLocation = true
>
> let noLocation = CLLocationCoordinate2D()
> let span:MKCoordinateSpan = MKCoordinateSpanMake(0.05, 0.05)
> let viewLocation = MKCoordinateRegionMake(noLocation, span)
>
>
> mapView.setRegion(viewLocation, animated: true)
> print(viewLocation)
>
> view.addSubview(mapView)
> }
>
> //add function to create circle overlay
>
> // override func didReceiveMemoryWarning() { //
> super.didReceiveMemoryWarning() // // Dispose of any resources
> that can be recreated. // }
>
>
> /*
> // MARK: - Navigation
>
> // In a storyboard-based application, you will often want to do a little preparation before navigation
> override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
> // Get the new view controller using segue.destinationViewController.
> // Pass the selected object to the new view controller.
> }
> */
>
> }
For the pin, in storyboard I used
@IBAction func pinDrop(_ sender: UILongPressGestureRecognizer)
but am not sure how to translate this to programatic code.
Any help would be great!
Upvotes: 0
Views: 1570
Reputation: 100503
Add long press like this
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.addAnnotation(_:)))
self.mapView.addGestureRecognizer(longPress)
//
get coordinates of press
@objc func addAnnotation(_ gestureRecognizer:UIGestureRecognizer)
{
if gestureRecognizer.state != UIGestureRecognizerState.began
{
return
}
let touchPoint = gestureRecognizer.location(in: self.mapView)
let newCoordinates = self.mapView.convert(touchPoint, toCoordinateFrom: self.mapView)
}
//
let noLocation = self.mapView.userLocation.coordinate
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.05, 0.05)
let viewLocation = MKCoordinateRegionMake(noLocation, span)
mapView.setRegion(viewLocation, animated: true)
Upvotes: 0