I am new to Xcode and mobile app. I am doing an app to find the current location. I tested it on the simulator and got this message in the console. "Could not inset legal attribution from corner 4". What does it mean and how can I fix it?
import UIKit
import Alamofire
import AlamofireImage
import MapKit
import CoreLocation
class MapVC: UIViewController
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
let authorizationStatus = CLLocationManager.authorizationStatus()
let regionRadius: Double = 1000
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
locationManager.delegate = self
configureLocationServices()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func centerMapPressed(_ sender: Any) {
if authorizationStatus == .authorizedAlways || authorizationStatus == .authorizedWhenInUse{
centerMapOnUserLocation()
}
}
MKMapViewDelegate:
func centerMapOnUserLocation(){
guard let coordinate = locationManager.location?.coordinate else{return}
let coordinateRegion = MKCoordinateRegionMakeWithDistance(coordinate, regionRadius*2.0, regionRadius * 2.0 )
mapView.setRegion(coordinateRegion, animated: true)
}
CLLocationManagerDelegate:
func configureLocationServices(){
if authorizationStatus == .notDetermined{
locationManager.requestAlwaysAuthorization()
}else{
return}
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
centerMapOnUserLocation()
}
this issue occurs when the required information in the right form is not found. if your device i trying to get some location or some number a
variable, and that a
variable is required for afun
.. the value is not set into a
, but afun
is called that this error occurs..
call your cell's coordinates in viewdidload
without any other function.
Here is the simplest way to get your cell's position. 1. you should have updated privacies of the devices under infor.plist
Add following lines into your plist before closing tag
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Privacy - Location Always and When In Use Usage Description</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Privacy - Location Always Usage Description</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Privacy - Location When In Use Usage Description</string>
after that.. this is the simplest working code .. i'm happily using it with no issue..
import CoreLocation
import MapKit
class AddressVc: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var map: MKMapView!
var locationManager = CLLocationManager()
here is code to move map in center for the required location
let latDelta:Double = 0.5
let lngDelta:Double = 0.5
let latitude:Double = 37.57554038
let longitude:Double = -122.40068475
let locationcoordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let zoomSpan = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lngDelta)
let region = MKCoordinateRegion(center: locationcoordinates, span: zoomSpan)
self.map.setRegion(region, animated: true)
in the above code.. if you put device's lat and lng postions in latitude and lognitude that will move the map to your's devices's location.
now how to get device's location.
here is the code you can put into your viewDidLoad()
function.
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
// device latitude = (locationManager.location?.coordinate.latitude)!
// device longitude = (locationManager.location?.coordinate.longitude)!
}
this is detect the device's latitude and longitude values. you can put them in above mentioned code..
Here is the full code.
import CoreLocation
import MapKit
class AddressVc: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var map: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
devicelatitude = (locationManager.location?.coordinate.latitude)!
devicelongitude = (locationManager.location?.coordinate.longitude)!
let latDelta:Double = 0.5
let lngDelta:Double = 0.5
let latitude:Double = devicelatitude
let longitude:Double = devicelongitude
let locationcoordinates = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let zoomSpan = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lngDelta)
let region = MKCoordinateRegion(center: locationcoordinates, span: zoomSpan)
self.map.setRegion(region, animated: true)
}
I hope this will help you.. if not.. there would be many other who are facing this issue.
==========================
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With