Put latitude and longitude on a mapView - ios

I tried to show the place that I want in mapView by insering latitude and longitude, but I failed to do that and the map show me always my place where I am and not the place that I want to get
these is the code that I used
class MapViewController: UIViewController, CLLocationManagerDelegate{
#IBOutlet weak var map: MKMapView!
let manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
map.showsPointsOfInterest = true
map.showsUserLocation = true
manager.requestAlwaysAuthorization()
manager.requestWhenInUseAuthorization()
//user location stuff
if CLLocationManager.locationServicesEnabled() {
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.startUpdatingLocation()
}
}
func locationManager(_manager: CLLocationManager, didUpdateLocations locations:[CLLocation]) {
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let location = CLLocationCoordinate2D(latitude: 36.1070, longitude: -112.1130)
let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
map.setRegion(region, animated: true)
self.map.showsUserLocation = true
}
}

Comment this
map.showsUserLocation = true
And put the code of didUpdateLocations inside viewDidLoad
class MapViewController: UIViewController, CLLocationManagerDelegate{
#IBOutlet weak var map: MKMapView!
let manager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
map.showsPointsOfInterest = true
//map.showsUserLocation = true
manager.requestAlwaysAuthorization()
manager.requestWhenInUseAuthorization()
//user location stuff
if CLLocationManager.locationServicesEnabled() {
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.startUpdatingLocation()
}
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let location = CLLocationCoordinate2D(latitude: 36.1070, longitude: -112.1130)
let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
map.setRegion(region, animated: true)
}
func locationManager(_manager: CLLocationManager, didUpdateLocations locations:[CLLocation]) {
///
}
}

To have a more reliable and efficient result,
I recommend using .plist (property list) file to store the latitude and longitude dynamically it would be much easier and would cost you less time.
plist contains the pins annotation directions which are basically an XML text file that holds the essential configuration information for bundle execution.
here is how to attach it to your project:
In your main view controller write the function:
func fetchAllData(){
if let path = Bundle.main.path(forResource: "NAME OF YOUR PLIST FILE", ofType: "plist") {
////If your plist contain root as Dictionary
if let dic = NSDictionary(contentsOfFile: path) as? [String: Any] {
let keys=dic.keys
for dataOfKey in keys {
if let object=dic[dataOfKey] as? [[String:Any]]{
locationsArray.append(contentsOf: object)
}
}
}
for location in self.locationsArray{
let newPin = MKPointAnnotation()
newPin.coordinate = CLLocationCoordinate2D.init(latitude: Double(location["latitude"] as! String)!, longitude: Double(location["longitude"] as! String)!)
self.mapView.addAnnotation(newPin)
}
}
}

Related

How to use current location with Yelp API

I am creating a yelp API app, but I am having trouble with my current location code. It seems the problem is my call to the API is happening before my values of Longitude and latitude are set, but I don't know how to rearrange my code to fix this issue. I have attached my code below lmk if you see anything I can do.
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var venuesTableView: UITableView!
let locationManager = CLLocationManager()
var long: Double = 0.0
var lat: Double = 0.0
/// Central Park, NYC coordinates
let CPLatitude: Double = 40.782483
let CPLongitude: Double = -73.963540
// Menlo Park, California Coordinates
let MPLatitude: Double = 37.4539910200113
let MPLongitude: Double = -122.19097843112596
var venues: [Venue] = []
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:[CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else{return}
long = locValue.longitude
lat = locValue.latitude
print(long)
print(lat)
}
override func viewDidLoad() {
super.viewDidLoad()
venuesTableView.delegate = self
venuesTableView.dataSource = self
venuesTableView.register(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: "customCell")
venuesTableView.separatorStyle = .none
retrieveVenues(latitude: lat, longitude: long, category: "eventservices",
limit: 20, sortBy: "distance", locale: "en_US") { (response, error) in
if let response = response {
self.venues = response
DispatchQueue.main.async {
self.venuesTableView.reloadData()
}
}
print("Does this work?")
print(self.long)
print(self.lat)
}
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
func userCurrentLocation(){
}
}
Create a separate function for the api call.
private func getVenues() {
retrieveVenues(latitude: lat, longitude: long, category: "eventservices",
limit: 20, sortBy: "distance", locale: "en_US") { (response, error) in
if let response = response {
self.venues = response
DispatchQueue.main.async {
self.venuesTableView.reloadData()
}
}
print("Does this work?")
print(self.long)
print(self.lat)
}
}
Then call this from the delegate method.
func locationManager(_ manager: CLLocationManager, didUpdateLocations
locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else{return}
long = locValue.longitude
lat = locValue.latitude
getVenues()
}

Data from GeoJSON URL end point not appearing on Map using MKAnnotations

I am creating my first IOS app and am not a developer and am really stuck with Map Annotations.
I am trying to get Fire data from a GeoJSON URL end point and display the fires as Annotations on a Map using URLSession and a custom MKAnnotationView and custom fire Pins.
The problem is the Annotations with the GeoJSON Fire data from the URL end point are not appearing on the Map, although data is being returned by the URL session. However, if I manually create a single Fire annotation it is appearing correctly on the map with the custom pin.
Any help would be immensely appreciated, I have spent days trying to figure this out :(
Here is the ViewController.Swift file
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
var locationManager:CLLocationManager!
var lat = Double()
var lon = Double()
var fires: [Fire] = []
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.register(FireMarkerView.self,forAnnotationViewWithReuseIdentifier:
MKMapViewDefaultAnnotationViewReuseIdentifier)
if let url = URL(string: "https://services3.arcgis.com/T4QMspbfLg3qTGWY/arcgis/rest/services/Active_Fires/FeatureServer/0/query?outFields=*&where=1%3D1&f=geojson") {
URLSession.shared.dataTask(with: url) {data, response, error in
if let data = data {
do {
let features = try MKGeoJSONDecoder().decode(data)
.compactMap { $0 as? MKGeoJSONFeature }
let validWorks = features.compactMap(Fire.init)
self.fires.append(contentsOf: validWorks)
print([self.fires])
}
catch let error {
print(error)
}
}
}.resume()
}
//This code works an annotation appears correctly on map
/* let fire = Fire(
title: "Ford Fire",
incidentShortDescription: "Hwy 35",
incidentTypeCategory: "WF",
coordinate: CLLocationCoordinate2D(latitude: 37.7993, longitude: -122.1947))
mapView.addAnnotation(fire)*/
mapView.addAnnotations(fires)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
determineMyCurrentLocation()
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
switch manager.authorizationStatus {
case .authorizedAlways , .authorizedWhenInUse:
mapView.showsUserLocation = true
followUserLocation()
locationManager.startUpdatingLocation()
break
case .notDetermined , .denied , .restricted:
locationManager.requestWhenInUseAuthorization()
break
default:
break
}
switch manager.accuracyAuthorization {
case .fullAccuracy:
break
case .reducedAccuracy:
break
default:
break
}
}
func followUserLocation() {
if let location = locationManager.location?.coordinate {
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: 4000, longitudinalMeters: 4000)
mapView.setRegion(region, animated: true)
}
}
func determineMyCurrentLocation() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
//locationManager.startUpdatingHeading()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
let userLocation = locations.first! as CLLocation
lat = userLocation.coordinate.latitude
lon = userLocation.coordinate.longitude
let region = MKCoordinateRegion.init(center: location.coordinate, latitudinalMeters: 400000, longitudinalMeters: 400000)
self.mapView.setRegion(region, animated: true)
// Call stopUpdatingLocation() to stop listening for location updates,
// other wise this function will be called every time when user location changes.
// Need a solution for this.
manager.stopUpdatingLocation()
print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
print("Error \(error)")
}
}
Here is the Model Class, Fire.swift
import Foundation
import MapKit
class Fire: NSObject, MKAnnotation {
let title: String?
let incidentShortDescription: String?
let incidentTypeCategory: String?
let coordinate: CLLocationCoordinate2D
init(
title: String?,
incidentShortDescription: String?,
incidentTypeCategory: String?,
coordinate: CLLocationCoordinate2D
) {
self.title = title
self.incidentShortDescription = incidentShortDescription
self.incidentTypeCategory = incidentTypeCategory
self.coordinate = coordinate
super.init()
}
init?(feature: MKGeoJSONFeature) {
// 1
guard
let point = feature.geometry.first as? MKPointAnnotation,
let propertiesData = feature.properties,
let json = try? JSONSerialization.jsonObject(with: propertiesData),
let properties = json as? [String: Any]
else {
return nil
}
// 3
title = properties ["IncidentName"] as? String
incidentShortDescription = properties["IncidentShortDescription"] as? String
incidentTypeCategory = properties["IncidentTypeCategory"] as? String
coordinate = point.coordinate
super.init()
}
var subtitle: String? {
return (incidentTypeCategory)
}
var image: UIImage {
guard let name = incidentTypeCategory else {
return #imageLiteral(resourceName: "RedFlame")
}
switch name {
case "RX":
return #imageLiteral(resourceName: "YellowFlame")
default:
return #imageLiteral(resourceName: "RedFlame")
}
}
Here is the custom MKAnnotation Class: FileMarkerView.swift
import Foundation
import MapKit
class FireMarkerView: MKAnnotationView {
override var annotation: MKAnnotation? {
willSet {
guard let fire = newValue as? Fire else {
return
}
canShowCallout = true
calloutOffset = CGPoint(x: -5, y: 5)
rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
image = fire.image
}
}
}
URLSession.shared.dataTask is an asynchronous task, meaning it calls its callback function at some indeterminate time in the future. Code executed outside of its callback (the { }) will end up getting called before the data task has actually completed. Right now, you're setting the annotations outside of that callback.
To solve this, you need to set the annotations inside of that callback function. So, where you have print([self.fires]), you can do:
DispatchQueue.main.async {
self.mapView.addAnnotations(self.fires)
}
The DispatchQueue.main.async is to make sure that an update to the UI gets called on the main thread (the URL task may return on a different thread).

ios11 MKAnnotation not showing both annotations

When I add the annotations to the map they sometimes show and sometimes not depending on how close they are to each other. If they are in the same house lets say one won't show. How do I make both of them show? Do I need to make a custom annotation class? I heard ios11 has a clumping feature, do I need to use that? Here is the code(abridged):
import UIKit
import MapKit
import Firebase
class GameViewController: UIViewController {
#IBOutlet weak var mapView: MKMapView!
fileprivate var locations = [CLLocation]()
fileprivate var userLocations = [(loc: CLLocation, name: String, team: String)]()
fileprivate var userAnnotations = [MKAnnotation]()
fileprivate var hasBeenUP = false
var ref: FIRDatabaseReference!
let uid = FIRAuth.auth()!.currentUser!.uid
var timer = Timer()
var timeLeft = 0.0
var firstTimer = Timer()
var name = ""
var team = ""
override func viewDidLoad() {
super.viewDidLoad()
let center = CLLocationCoordinate2D(latitude: 47.786769, longitude: -20.413634)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(region, animated: true)
mapView.mapType = .hybrid
locationManager.startUpdatingLocation()
ref = FIRDatabase.database().reference()
setupULSending()
getMetaInfo()
ref.child("realtimeLocations").observe(FIRDataEventType.value, with: { (snapshot) in
self.userLocations = []
for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {
guard let snapshotValue = snapshot.value as? NSDictionary, let snapVal = snapshotValue[rest.key] as? NSDictionary else {
break
}
let name = snapVal["name"] as! String
let team = snapVal["team"] as? String ?? ""
if let lat = snapVal["lat"] as? Double,
let long = snapVal["long"] as? Double {
let location = CLLocation(latitude: lat, longitude: long)
self.userLocations.append((loc: location, name: name, team: team))
}else {
}
}
DispatchQueue.main.async {
self.updateUserLocation()
}
})
}
private lazy var locationManager: CLLocationManager = {
let manager = CLLocationManager()
manager.allowsBackgroundLocationUpdates = true
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.delegate = self
manager.requestAlwaysAuthorization()
return manager
}()
func updateUserLocation() {
for an in self.mapView.annotations {
mapView.removeAnnotation(an)
}
for loc in userLocations {
let annotation = MKPointAnnotation()
annotation.coordinate = loc.loc.coordinate
annotation.title = loc.name
annotation.subtitle = "local"
mapView.addAnnotation(annotation)
}
}
}
// MARK: - CLLocationManagerDelegate
extension GameViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations
locations: [CLLocation]) {
let location = locations.last as! CLLocation
self.locations.append(location)
}
}
On the MKAnnotationView, you have to set the MKFeatureDisplayPriority to 'required'. You can modify the annotation views by implementing MKMapViewDelegate and mapView(MKMapView, viewFor: MKAnnotation). Something like this:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }
var view = mapView.dequeueReusableAnnotationView(withIdentifier: "yourIdentifier")
if view == nil {
view = MKMarkerAnnotationView(annotation: nil, reuseIdentifier: "yourIdentifier")
}
view?.displayPriority = .required
return view
}
More options for this are explained in the WWDC 2017 video 237 "What's New in MapKit"

How to create multiple MapKit annotations by using data from a server?

I have been having a real tough time making multiple annotations on a MapView. So far I have been able to create the relevant classes to download, parse and store the data into an array that can be used. Yet I am still struggling to use the said data and make the annotations required.
HomeModel Class - download and parse the required information from the server
import UIKit
import Foundation
protocol HomeModelProtocol: class {
func itemsDownloaded(items: NSArray)
}
class HomeModel: NSObject, URLSessionDataDelegate {
weak var delegate: HomeModelProtocol!
var data = Data()
let urlPath: String = "https://FAKEDATABASEURL.XYZ"
func downloadItems() {
let url: URL = URL(string: urlPath)!
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
let task = defaultSession.dataTask(with: url) { (data, response, error) in
if error != nil {
print("Failed to download data")
} else {
print("Data downloaded")
self.parseJSON(data!)
}
}
task.resume()
}
func parseJSON(_ data:Data) {
var jsonResult = NSArray()
do {
jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! NSArray
} catch let error as NSError {
print(error)
}
var jsonElement = NSDictionary()
let locations = NSMutableArray()
for i in 0 ..< jsonResult.count {
jsonElement = jsonResult[i] as! NSDictionary
let location = LocationModel()
if let name = jsonElement["Name"] as? String,
let address = jsonElement["Address"] as? String,
let latitude = jsonElement["Latitude"] as? String,
let longitude = jsonElement["Longitude"] as? String {
location.name = name
location.address = address
location.latitude = latitude
location.longitude = longitude
}
locations.add(location)
}
DispatchQueue.main.async(execute: { ()-> Void in
self.delegate.itemsDownloaded(items: locations)
})
}
}
LocalModel Class - To store the data into an array to be used by the app
import UIKit
import Foundation
class LocationModel: NSObject {
// Properties
var name: String?
var address: String?
var latitude: String?
var longitude: String?
// Empty constructor
override init() { }
// Construct with #name, #address, #latitude and #longitude.
init(name: String, address: String, latitude: String, longitude: String) {
self.name = name
self.address = address
self.latitude = latitude
self.longitude = longitude
}
// Print the object's current state
override var description: String {
return "Name: \(String(describing: name)), Address:\(String(describing: address)), Latitude: \(String(describing: latitude)), Longitude: \(String(describing: longitude))"
}
}
Map View Controller - Controls the map for the application
import UIKit
import MapKit
import CoreLocation
class HotPlacesViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
#IBOutlet weak var mapView: MKMapView!
var isFirstTime = true
var locationManager = CLLocationManager()
let newPin = MKPointAnnotation()
var selectedLocation:LocationModel?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Setup the location services delegate in this class.
locationManager.delegate = self
// This little method requests the users permission for location services whilst in this view controller.
if CLLocationManager.authorizationStatus() == .notDetermined {
self.locationManager.requestAlwaysAuthorization()
let alert = UIAlertController(title: "You can change this option in the Settings App", message: "So keep calm your selection is not permanent. 🙂",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
// Drops the pin on the users current location.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
mapView.removeAnnotation(newPin)
let location = locations.last! as CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
if(self.isFirstTime) {
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
// Set the region on the map.
mapView.setRegion(region, animated: true)
self.isFirstTime = false
}
newPin.coordinate = location.coordinate
mapView.addAnnotation(newPin)
}
}
Make your let locations = NSMutableArray() array accessible by other classes so that you can use this array in your HotPlacesViewController class.
Then in your HotPlacesViewController class declare a property for holding locations data. And load the data inside viewDidLoad() like:
class HotPlacesViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
// declare a property which will hold the locations data
override func viewDidLoad() {
...
...
locations = // load your data here
}
}
Then, for multiple annotations follow this logic:
for location in locations {
let annotation = MKPointAnnotation()
annotation.title = location.name
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
mapView.addAnnotation(annotation)
}

Current location title? Mapkit

I'm making an application with MapKit. Here's an image of how it looks:
And I want to change (Current location) title of the symbol current location from that pin.
Here's code:
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
#IBOutlet weak var theMap: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad()
{
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
let location = self.locationManager.location
var latitude: Double = location.coordinate.latitude
var longitude: Double = location.coordinate.longitude
println("GPS Súradnice :: \(latitude), \(longitude)")
theMap.delegate = self
theMap.mapType = MKMapType.Standard
theMap.showsUserLocation = true
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
//--- Find Address of Current Location ---//
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
{
//--- CLGeocode to get address of current location ---//
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in
if (error != nil)
{
println("Reverse geocoder failed with error" + error.localizedDescription)
return
}
if placemarks.count > 0
{
let pm = placemarks[0] as! CLPlacemark
self.displayLocationInfo(pm)
}
else
{
println("Problem with the data received from geocoder")
}
})
}
func displayLocationInfo(placemark: CLPlacemark?)
{
if let Placemark = placemark
{
//Stop updating kvôli vydrži baterke
locationManager.stopUpdatingLocation()
let adresa = (Placemark.thoroughfare != nil) ? Placemark.thoroughfare : "Ulica: "
let cislo = (Placemark.subThoroughfare != nil) ? Placemark.subThoroughfare : "Číslo ulice:"
let mesto = (Placemark.locality != nil) ? Placemark.locality : "Mesto: "
let stat = (Placemark.country != nil) ? Placemark.country : "Štát: "
var coordinates:CLLocationCoordinate2D = placemark!.location.coordinate
var pointAnnotation:MKPointAnnotation = MKPointAnnotation()
pointAnnotation.coordinate = coordinates
pointAnnotation.title = "\(adresa) \(cislo)"
pointAnnotation.subtitle = "\(adresa) \(cislo), \(mesto), \(stat)"
self.theMap.addAnnotation(pointAnnotation)
self.theMap.centerCoordinate = coordinates
self.theMap.selectAnnotation(pointAnnotation, animated: true)
println(mesto)
println(adresa)
println(cislo)
println(stat)
}
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
{
println("Chyba pri aktualizovaní lokácie " + error.localizedDescription)
}
}
If I get it right. You want to change blue dot. Try this.
let theLocation: MKUserLocation = theMap.userLocation
theLocation.title = "I'm here!"
//swift 3
let annotation = MKPointAnnotation()
annotation.coordinate = center
annotation.title = "title"
Annotation.subtitle = “Subtitle”
mapView.addAnnotation(annotation)
I think,http://swift3devlopment.blogspot.in/ here you get more details of MapKit
For Show blue circle mapkit in swift 2.0:
override func viewDidLoad() {
super.viewDidLoad()
//this line show blue circle location user
Mapa.showsUserLocation = true
locationManager.requestWhenInUseAuthorization();
if CLLocationManager.locationServicesEnabled(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
else{
print("Location service disabled");
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
let region = MKCoordinateRegion(center: locValue, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
print("locations = \(locValue.latitude) \(locValue.longitude)")
Mapa.setRegion(region, animated: true)
}

Resources