MapKit Direction to Selected annotation - ios

Trying to get direction to selected annotation on the map from the current location of the User but not sure how to catch selected annotation(when user selecting one of them) and show direction.Checked different topics on the Stack but there is not a lot of fresh information about how catch selected annotation and show direction for user location.Please advise.
import UIKit
import MapKit
import CoreLocation
class NavigationVC: UIViewController {
#IBOutlet weak var mapView: MKMapView!
let manager = CLLocationManager()
let request = MKLocalSearchRequest()
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
}
//MARK: CONFIGURATION OF MAPVIEW
extension NavigationVC: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations
locations: [CLLocation]) {
let location = locations[0]
let span: MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1 )
let userLocation:CLLocationCoordinate2D =
CLLocationCoordinate2DMake(location.coordinate.latitude,
location.coordinate.longitude)
let region: MKCoordinateRegion =
MKCoordinateRegionMake(userLocation, span)
mapView.setRegion(region, animated: true)
self.mapView.showsUserLocation = true
request.naturalLanguageQuery = "Currency exchange"
request.region = mapView.region
let activeSearch = MKLocalSearch(request: request)
activeSearch.start { (response, error) in
guard let response = response else {
return
}
for item in response.mapItems {
let annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
DispatchQueue.main.async {
self.mapView.addAnnotation(annotation)
}
}
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) ->
MKOverlayRenderer {
let polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.blue
polylineRenderer.fillColor = UIColor.red
polylineRenderer.lineWidth = 2
return polylineRenderer
}
}

To get the path on the map from your position to the annotation follow this code:
annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat),longitude: CLLocationDegrees(lon))
self.map?.addAnnotation(annotation)
var route : MKRoute? = nil
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
let directionRequest = MKDirectionsRequest()
directionRequest.source = MKMapItem.forCurrentLocation();
directionRequest.destination = MKMapItem(placemark: MKPlacemark(coordinate:(self?.annotation.coordinate)!))
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
route = response.routes[0]
if let percorso = route{
DispatchQueue.main.async { [weak self] in
self?.map?.add((percorso.polyline), level: MKOverlayLevel.aboveRoads)
}
}
}
}
You have only to select the correct annotation, maybe try to register a touch listener on the annotation.

Use didSelect delegate method.
e.g.
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let annotation = view.annotation else {
return
}
let directionRequest = MKDirectionsRequest()
directionRequest.source = MKMapItem.forCurrentLocation()
directionRequest.destination = MKMapItem(placemark: MKPlacemark(coordinate: annotation.coordinate))
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
if !response.routes.isEmpty {
let route = response.routes[0]
DispatchQueue.main.async { [weak self] in
self?.mapView.add(route.polyline)
}
}
}
}
Don't forget guard to prevent crash.
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) ->
MKOverlayRenderer {
guard overlay is MKPolyline else {
return MKPolylineRenderer()
}
...
}
Good luck!

Related

draw polyline between two latitude and longitude

I pass two lat and long for source and destination but not draw poly line between two point.always get something wrong error .below code I pass my current location in source coordinate and travel location pass in destCoordinates. I want draw poly line between sourceCoordinates to destCoordinates..
if(CLLocationManager.locationServicesEnabled()){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
//let sourceCoordinates = locationManager.location?.coordinate
let sourceCoordinates = CLLocationCoordinate2DMake(19.115950,72.856448)
let destCoordinates = CLLocationCoordinate2DMake(19.119670,72.853616)
let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates)
let destPlacemark = MKPlacemark(coordinate: destCoordinates)
let sourceItem = MKMapItem(placemark: sourcePlacemark)
let destItem = MKMapItem(placemark: destPlacemark)
let directionRequest = MKDirectionsRequest()
directionRequest.source=sourceItem
directionRequest.destination=destItem
directionRequest.transportType = .walking
let directions = MKDirections(request: directionRequest)
directions.calculate(completionHandler: {
response,error in
guard let response = response else{
if let error = error {
print("something wrong")
}
return
}
let route = response.routes[0]
self.upcoming_info_map.add(route.polyline,level: .aboveRoads)
let rekt = route.polyline.boundingMapRect
self.upcoming_info_map.setRegion(MKCoordinateRegionForMapRect(rekt), animated: true)
})
for draw polyline i use this function
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolygonRenderer(overlay:overlay)
renderer.strokeColor = UIColor.blue
renderer.lineWidth = 5.0
return renderer
}
use this method.takes list of locations and draw polyline
func addPolyLineToMap(googlemaplist: [CLLocation?]){
var coordinates = googlemaplist.map({ (location: CLLocation!) -> CLLocationCoordinate2D in
return location.coordinate
})
print("locatios count")
print(googlemaplist.count)
var polyline = MKPolyline(coordinates: &coordinates, count: googlemaplist.count)
DispatchQueue.main.async {
self.MapKit.add(polyline)
}
}

locationManager Unwrapping Error in Swift 4.1 but not in Swift 4.0

I have code that I wrote in Swift 4.0 allowing an App which I am working on to show a Map with a destination and a user location. Now this app worked perfectly before the the recent update to Swift 4.1.
The error message says that there is an unexpectedly found nil while unwrapping an Optional value. In the xCode output window it is showing the following text:
Could not inset legal attribution from corner 4
(lldb)
I have tried all sorts of things, and looked this up all over the internet and I am not sure wether this is a bug in Swift 4.1 or there is something new I need to do. Like said, this app worked perfectly before.
I also made sure the info.plist has the right Privacy properties applied for Location Manager.
Info.plist
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBAction func centerUserLocation(_ sender: Any) {
mapkitView.setUserTrackingMode(MKUserTrackingMode.follow, animated: true)
}
#IBOutlet weak var mapkitView: MKMapView!
let locationManager = CLLocationManager()
#IBAction func phoneNumber(_ sender: Any) {
let number = "00436641491000"
if let url = URL(string: "tel://\(number)") {
UIApplication.shared.open(url)
}
}
#IBAction func emailAddress(_ sender: Any) {
let email = "salzburg#citybeats.at"
if let url = URL(string: "mailto:\(email)") {
UIApplication.shared.open(url)
}
}
override func viewDidLoad() {
super.viewDidLoad()
mapkitView.delegate = self
mapkitView.showsScale = true
mapkitView.showsPointsOfInterest = true
mapkitView.showsUserLocation = true
mapkitView.userTrackingMode = .follow
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
let sourceCoordinates = locationManager.location?.coordinate
let destCoordinates = CLLocationCoordinate2DMake(47.800715, 13.041061)
let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates!)
let destPlacemark = MKPlacemark(coordinate: destCoordinates)
let annotation = MKPointAnnotation()
annotation.coordinate = destCoordinates
annotation.title = "CityBeats"
annotation.subtitle = "House & RnB"
mapkitView.addAnnotation(annotation)
let sourceItem = MKMapItem(placemark: sourcePlacemark)
let destItem = MKMapItem(placemark: destPlacemark)
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceItem
directionRequest.destination = destItem
directionRequest.transportType = .walking
let directions = MKDirections(request: directionRequest)
directions.calculate(completionHandler: {
response, error in
guard let response = response else {
if let error = error {
print("Something went wrong!")
}
return
}
let route = response.routes[0]
self.mapkitView.add(route.polyline, level: .aboveRoads)
let rekt = route.polyline.boundingMapRect
self.mapkitView.setRegion(MKCoordinateRegionForMapRect(rekt), animated: true)
})
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor.cyan
renderer.alpha = 0.7
renderer.lineWidth = 7.0
return renderer
}
}
You should check if sourceCoordinates is nil.
e.g.
guard let sourceCoordinates = locationManager.location?.coordinate else {
return
}

Navigation from user location to annotation

so I want to set up a map with navigation from the users location to a annotation that the user selected. I Have all the annotations loaded from a .Plist . someone help me out I'm just a beginner. this is my code for the map:
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, CLLocationManagerDelegate, UISearchBarDelegate {
#IBOutlet weak var mapView: MKMapView!
#IBAction func searchButton(_ sender: Any) {
let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.delegate = self
present(searchController, animated: true, completion: nil)
}
let locationManager = CLLocationManager()
// Vraag om locatie van gebruiker
func requestLocationAccess()
{
let status = CLLocationManager.authorizationStatus()
switch status
{
case .authorizedAlways, .authorizedWhenInUse:
return
case .denied, .restricted:
print("location access denied")
default:
locationManager.requestWhenInUseAuthorization()
}
}
class func createViewAnnotationForMap(mapView:MKMapView, annotation:MKAnnotation)->MKAnnotationView
{
if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "PinAnnotation"){
return annotationView
}
else
{
let returnedAnnotationView:MKPinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier:"PinAnnotation")
returnedAnnotationView.animatesDrop = false
returnedAnnotationView.canShowCallout = true
return returnedAnnotationView
}
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar)
{
//Ignoring user
UIApplication.shared.beginIgnoringInteractionEvents()
//Activity Indicator
let activityIndicator = UIActivityIndicatorView()
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.startAnimating()
self.view.addSubview(activityIndicator)
//Hide search bar
searchBar.resignFirstResponder()
dismiss(animated: true, completion: nil)
//Create the search request
let searchRequest = MKLocalSearchRequest()
searchRequest.naturalLanguageQuery = searchBar.text
let activeSearch = MKLocalSearch(request: searchRequest)
activeSearch.start { (response, error) in
activityIndicator.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
if response == nil
{
print("ERROR")
}
else
{
//Getting data
let latitude = response?.boundingRegion.center.latitude
let longitude = response?.boundingRegion.center.longitude
//Create annotation
let annotation = MKPointAnnotation()
annotation.title = searchBar.text
annotation.coordinate = CLLocationCoordinate2DMake(latitude!, longitude!)
self.mapView.addAnnotation(annotation)
//Zooming in on annotation
let coordinate:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude!, longitude!)
let span = MKCoordinateSpanMake(0.1, 0.1)
let region = MKCoordinateRegionMake(coordinate, span)
self.mapView.setRegion(region, animated: true)
}
}
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.blue
return renderer
}
override func viewDidLoad()
{
super.viewDidLoad()
requestLocationAccess()
mapView.isZoomEnabled = true
mapView.isScrollEnabled = true
mapView.showsScale = true
mapView.isRotateEnabled = true
mapView.showsUserLocation = true
mapView.showsCompass = true
mapView.delegate = self
//Regio instellen
var newRegion:MKCoordinateRegion = MKCoordinateRegion()
newRegion.center.latitude = -25.106303;
newRegion.center.longitude = 133.587542;
newRegion.span.latitudeDelta = 22;
newRegion.span.longitudeDelta = 22;
self.mapView.setRegion(newRegion, animated: true)
if let cityDetailsPath = Bundle.main.path(forResource: "places", ofType: "plist") {
guard let cityDetails = NSArray(contentsOfFile: cityDetailsPath) as? [[String: String]] else {return}
for city in cityDetails
{
guard let latStr = city["latitude"] else { continue }
guard let lonStr = city["longitude"] else { continue }
guard let titleStr = city["title"] else { continue }
guard let subtitleStr = city["subTitle"] else { continue }
if let lat = Double(latStr) {
if let lon = Double(lonStr) {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2DMake(lat,lon)
annotation.title = titleStr
annotation.subtitle = subtitleStr
mapView.addAnnotation(annotation)
}
}
}
}
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
}
extension MapViewController: MKMapViewDelegate {
func createViewAnnotationForMap(mapView:MKMapView, annotation:MKAnnotation)->MKAnnotationView {
let annoannotationId = "PinAnnotation"
if let annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: annoannotationId) {
return annotationView
}
else
{
let returnedAnnotationView:MKPinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: annoannotationId)
returnedAnnotationView.animatesDrop = false
returnedAnnotationView.canShowCallout = true
return returnedAnnotationView
}
}
}
Use didSelect delegate method.
e.g.
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let annotation = view.annotation else {
return
}
let directionRequest = MKDirectionsRequest()
directionRequest.source = MKMapItem.forCurrentLocation()
directionRequest.destination = MKMapItem(placemark: MKPlacemark(coordinate: annotation.coordinate))
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
if !response.routes.isEmpty {
let route = response.routes[0]
DispatchQueue.main.async { [weak self] in
self?.mapView.add(route.polyline)
}
}
}
}
Don't forget guard to prevent crash.
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) ->
MKOverlayRenderer {
guard overlay is MKPolyline else {
return MKPolylineRenderer()
}
...
}

Swift 3 - UIView loads every time when adding a location coordinate

I am using firebase as my backend. In this application I have used mapkit and firebase. I want to track the location of user. But when I am doing the location of user is sucessfully uploaded to firebase but in my application the view is reloaded.
my code:-
#IBOutlet weak var map:MKMapView!
let locationManager = CLLocationManager()
var lat = [Double]()
var lon = [Double]()
var ref:DatabaseReference!
var uid = ""
override func viewDidLoad() {
super.viewDidLoad()
map.delegate = self
locationManager.delegate = self
ref = Database.database().reference().child("users").child(uid)
ref.child("locations").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in
print("func snapshot \(snapshot)")
let dict = snapshot.value as! [String:Double]
let lat = dict["latitude"]
let lon = dict["longitude"]
print("latidue = \(lat!)")
print("longitude = \(lon!)")
self.lat.append(lat!)
self.lon.append(lon!)
})
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let location = CLLocationCoordinate2D(latitude: lat.first!,longitude: lon.first!)
let destinationLocation = CLLocationCoordinate2D(latitude: lat.last!, longitude:lon.last!)
let sourcePlacemark = MKPlacemark(coordinate: location, addressDictionary: nil)
let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
let destinationMapItem = MKMapItem(placemark: destinationPlacemark)
let sourceAnnotation = MKPointAnnotation()
if let location = sourcePlacemark.location {
sourceAnnotation.coordinate = location.coordinate
}
let destinationAnnotation = MKPointAnnotation()
if let location = destinationPlacemark.location {
destinationAnnotation.coordinate = location.coordinate
}
let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationMapItem
directionRequest.transportType = .automobile
let directions = MKDirections(request: directionRequest)
directions.calculate {
(response, error) -> Void in
guard let response = response else {
if let error = error {
print("Error: \(error)")
}
return
}
let route = response.routes[0]
self.map.add((route.polyline), level: MKOverlayLevel.aboveRoads)
let rect = route.polyline.boundingMapRect
self.map.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
}
let span = MKCoordinateSpanMake(0.05, 0.05)
let region = MKCoordinateRegion(center: location, span: span)
map.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
map.addAnnotation(annotation)
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor.red
renderer.lineWidth = 4.0
return renderer
}
I don't want to reload my whole view just want to render my mapview whenever new cordinate is uploaded.
Remove code from viewDidAppear and put it inside the observe childAdded callback

How to display different color in annotation point in map?

Can't display color of annotation point in map , how it could be solved here's my code .i declared even function of color in MKPointAnnotation even i can't change the color , is there i have missed any codes ??
class NearbyViewController: UIViewController,MKMapViewDelegate,CLLocationManagerDelegate {
#IBOutlet weak var mapview: MKMapView!
#IBOutlet weak var name: UILabel!
var locationManager : CLLocationManager!
#IBOutlet weak var menu: UIBarButtonItem!
var schoolMap : [schools] = []
var collegeMap : [Colleges] = []
var universityMap : [University] = []
class MyPointAnnotation : MKPointAnnotation {
var pinTintColor: UIColor?
}
override func viewDidLoad() {
super.viewDidLoad()
getschoolMapJson()
getcollegeMapJson()
getuniversityMapJson()
menu.target = self.revealViewController()
menu.action = #selector(SWRevealViewController.revealToggle(_:))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.distanceFilter = 1.00
locationManager.startUpdatingLocation()
}
override func viewWillAppear(_ animated: Bool) {
mapview.reloadInputViews()
}
override func viewDidAppear(_ animated: Bool) {
mapview.reloadInputViews()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations[0]
let span = MKCoordinateSpanMake(0.05, 0.05)
let mylocation = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region = MKCoordinateRegionMake(mylocation, span)
mapview.setRegion(region, animated: true)
self.mapview.showsUserLocation = true
}
func getschoolMapJson(){
if (schoolMap.count > 0){
return
}
let url = NSURL(string: "http://www.myeducationhunt.com/api/v1/schools")
var request = URLRequest(url: url! as URL)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(request).responseJSON(){ response in
switch response.result{
case.success(let data):
print("success api",data)
let myresponse = JSON(data)
/*for i in 0..<self.myresponse.count{
let schools_data = schools(schoolJson:self.myresponse[i])
self.schooldata.append(schools_data)
}*/
for school in myresponse.array!{
let schoolsObj = schools(schoolJson: school)
//Add Pin
let location = CLLocationCoordinate2DMake((schoolsObj.latitude) , (schoolsObj.longitude))
let span = MKCoordinateSpanMake(10.0, 10.0)
let region = MKCoordinateRegionMake(location, span)
self.mapview.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = schoolsObj.name
let pincolor = MyPointAnnotation()
pincolor.pinTintColor = .blue
// MKPinAnnotationColor.green
// let pinC = MKPinAnnotationView.greenPinColor()
// self.mapview.addAnnotation(pinC as! MKAnnotation)
self.mapview.addAnnotation(annotation)
self.schoolMap.append(schoolsObj)
}
// self.mapview.reloadInputViews()
case.failure(let error):
print("Not Success",error)
}
}
}
func getcollegeMapJson(){
if (collegeMap.count > 0){
return
}
let url = NSURL(string: "http://www.myeducationhunt.com/api/v1/colleges")
var request = URLRequest(url: url! as URL)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(request).responseJSON(){ response in
switch response.result{
case.success(let data):
print("success api",data)
let myresponse = JSON(data)
/*for i in 0..<self.myresponse.count{
let schools_data = schools(schoolJson:self.myresponse[i])
self.schooldata.append(schools_data)
}*/
for college in myresponse.array!{
let collegeObj = Colleges(collegeJson: college)
//Add Pin
let location = CLLocationCoordinate2DMake((collegeObj.latitude) , (collegeObj.longitude))
let span = MKCoordinateSpanMake(10.0, 10.0)
let region = MKCoordinateRegionMake(location, span)
self.mapview.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = collegeObj.name
self.mapview.addAnnotation(annotation)
self.collegeMap.append(collegeObj)
}
case.failure(let error):
print("Not Success",error)
}
}
}
func getuniversityMapJson(){
if (universityMap.count > 0){
return
}
let url = NSURL(string: "http://www.myeducationhunt.com/api/v1/universities")
var request = URLRequest(url: url! as URL)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Alamofire.request(request).responseJSON(){ response in
switch response.result{
case.success(let data):
print("success api",data)
let myresponse = JSON(data)
for university in myresponse.array!{
let universityObj = University(universityJson: university)
//Add Pin
let location = CLLocationCoordinate2DMake((universityObj.latitude) , (universityObj.longitude))
let span = MKCoordinateSpanMake(10.0, 10.0)
let region = MKCoordinateRegionMake(location, span)
self.mapview.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = universityObj.name
self.mapview.addAnnotation(annotation)
self.universityMap.append(universityObj)
}
case.failure(let error):
print("Not Success",error)
}
}
}
}
I am confused to display annotation point color i tried in many ways
but can't do it .
You have to add the following function. I hope this will solve your issue.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "myAnnotation") as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myAnnotation")
} else {
annotationView?.annotation = annotation
}
if let annotation = annotation as? MyPointAnnotation {
annotationView?.pinTintColor = annotation.pinTintColor
}
return annotationView
}

Resources