Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Simulator not updating location

Tags:

ios

swift2

The below code worked just one time and now all of the sudden it is not showing a point of interest (POI). I tried it on two different machines and am getting the same behavior. This is what has me puzzled, is it the code or my simulator settings.

I cleaned the project, made sure I had the simulator on custom. I did have the longitude and latitude printing now all of a sudden it is not printing in the console either.

import UIKit import MapKit import CoreLocation

class MapViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet var map: MKMapView!

var manager:CLLocationManager!
var latitude:Double = 0.0
var longitude:Double = 0.0
var location:Double = 0.0

override func viewDidLoad() {
    super.viewDidLoad()
    print("init")
    manager = CLLocationManager()
    manager.delegate = self
    manager.desiredAccuracy - kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
}

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

    print("test test test")

    let userLocation:CLLocation = locations[0]
    self.latitude = userLocation.coordinate.latitude
    self.longitude = userLocation.coordinate.longitude

    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = "Army Recruiting"

    request.region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 1600, 1600)

    MKLocalSearch(request: request).startWithCompletionHandler { (response, error) in
        guard error == nil else { return }
        guard let response = response else { return }
        guard response.mapItems.count > 0 else { return }

        let randomIndex = Int(arc4random_uniform(UInt32(response.mapItems.count)))
        let mapItem = response.mapItems[randomIndex]

        mapItem.openInMapsWithLaunchOptions(nil)
    }

    print("\(self.longitude) \(self.latitude)")

}

----------------------UPDATE 1--------------------

Updating location via simulator

enter image description here

I have noticed the below function is not running:

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

---------------------Update 2--------------------

The app worked once then stopped. Not sure what is going on. Yes I added NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription enter image description here

like image 374
Mike3355 Avatar asked Sep 06 '25 03:09

Mike3355


1 Answers

Couple of issues with your code:

override func viewDidLoad() {
    super.viewDidLoad()
    print("init")
    manager = CLLocationManager()
    manager.delegate = self
    manager.desiredAccuracy - kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
}

Replace with:

override func viewDidLoad() {
    super.viewDidLoad()
    print("init")
    manager = CLLocationManager()
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()
}

ADDITIONALLY (for info)

You can create GPX files for different locations.

You can also simulate routes with in the GPX files - have a delve into the Apple Documentation which shows you hows to set the GPX files to your target build. https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/iOS_Simulator_Guide/CustomizingYourExperienceThroughXcodeSchemes/CustomizingYourExperienceThroughXcodeSchemes.html

There is also a good tutorial here with links for creating GPX files and routes which i have used in the past and is very useful. https://blackpixel.com/writing/2013/05/simulating-locations-with-xcode.html

like image 161
RichAppz Avatar answered Sep 07 '25 21:09

RichAppz