Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Swift Storyboard iOS I'm trying to display a map annotation in mapkit when a user clicks a button. Right now with the current code the

Swift Storyboard iOS

I\'m trying to display a map annotation in mapkit when a user clicks a button. Right now with the current code the annotation (the pin) display from the get go and I would like to embed in the button so it shows in the map after the user presses the button. Here\'s the code snippet related to the annotation:

let pin = MKPointAnnotation()

pin.coordinate = coordinate

mapView.addAnnotation(pin)

}

Full code:

import CoreLocation

import MapKit

import UIKit

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet var mapView: MKMapView!

let manager = CLLocationManager()

override func viewDidLoad() {

super.viewDidLoad()

}

override func viewDidAppear(_ animated: Bool) {

super.viewDidAppear(animated)

manager.desiredAccuracy = kCLLocationAccuracyBest

manager.delegate = self

manager.requestWhenInUseAuthorization()

manager.startUpdatingLocation()

}

func locationManager( _ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){

if let location = locations.first{

manager.stopUpdatingLocation()

render(location: location)

}

}

func render(location: CLLocation){

let coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)

let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)

let region = MKCoordinateRegion(center: coordinate, span: span)

mapView.setRegion(region, animated: true)

let pin = MKPointAnnotation()

pin.coordinate = coordinate

mapView.addAnnotation(pin)

}

}

Sol49:

To display the map annotation only when the user clicks a button, you can modify the render function to create the MKPointAnnotation object and set its properties outside of the function, but add it to the mapView only when the button is pressed. Here\'s an example of how you can modify the code:

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet var mapView: MKMapView!

let manager = CLLocationManager()

var pin: MKPointAnnotation?

override func viewDidAppear(_ animated: Bool) {

super.viewDidAppear(animated)

manager.desiredAccuracy = kCLLocationAccuracyBest

manager.delegate = self

manager.requestWhenInUseAuthorization()

manager.startUpdatingLocation()

}

func locationManager( _ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){

if let location = locations.first{

manager.stopUpdatingLocation()

render(location: location)

}

}

func render(location: CLLocation){

let coordinate = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)

let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)

let region = MKCoordinateRegion(center: coordinate, span: span)

mapView.setRegion(region, animated: true)

// Create the MKPointAnnotation object

pin = MKPointAnnotation()

pin?.coordinate = coordinate

}

@IBAction func addButtonPressed(_ sender: Any) {

// Add the MKPointAnnotation object to the mapView

if let pin = pin {

mapView.addAnnotation(pin)

}

}

}

In this modified code, we create the MKPointAnnotation object in the render function, but we don\'t add it to the mapView yet. Instead, we assign the object to the pin property of the view controller, which can be accessed by the addButtonPressed function. When the button is pressed, we check if pin is not nil, and add it to the mapView if it exists.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

Do not say anything that will place further blame on the reader.

Answered: 1 week ago