Location services

Show a map with apple maps

  1. Go to the object library and filter - look for 'Map Kit View'.
  2. Add constraints to it
  3. Go the the project navigator (left hand side of screen), click on the project name (topmost).
  4. Click on build phases
  5. Click on link binary with libraries.
  6. Click on the + sign.
  7. Search for map.
  8. Add Mapkit.framework
Edit the attributes
  1. Go to the main.storyboard.
  2. Look for the map view.
  3. Look at attributes inspector. 
  4. Edit accordingly.
Setting Required Device capabilities using Info.plist (see if it has GPS, don't want people to download the app if their device doesn't have it, else they will leave a bad review.
  1. Go to the project navigator.
  2. Look for the info.plist and click on it.
  3. Click on required device capabilities
  4. Click on the + sign
  5. In the blank space, key in gps
  6. Click on the + sign again, key in location-services
Set permissions for location in info.plist
  1. Go back to info.plist.
  2. Click on the + sign next to Information Property list.
  3. Look for privacy - location. If you have problems looking for it, you might need to scroll down using the down arrow key.
  4. Always usage vs When in use - choose one (will it be used all the time?).
  5. Value - give them a description to tell them why they should let you know their location.
Getting and showing your location
  1. Ask for permissions in viewcontroller.
  2. Type import Mapkit
  3. Add the mapkit outlet to your class.
  4. Next to: class ViewController: UIViewController, add CLLocationManagerDelegate
  5. Initialise location manager as a property of the class, not as a local variable.
  6. let locationManager = CLLocationManager()
  7. In viewdidload, key 
  8. locationManager.delegate = self
  9. locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()

Add the following function (scroll down to add parameters):


    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        guard status == .authorizedWhenInUse else {
            print("Location not enabled")
            return
        }
        print("Location allowed")
        mapView.showsUserLocation = true
    }




Get location coordinates
1. In object library, search for navigation bar
2. Drag and drop it into the storyboard and add constraints.
3. Add a button, add an action to it.
4. Inside the function:

let coord = locationManager.location?.coordinate
        if let lat = coord?.latitude
        {
            print("Latitude:" + String(lat))
            
        }
        if let lon = coord?.longitude
        {
            print("Longitude:" + String(lon))
            

        }

Setting the pins in the map for a location:
let annoRem = mapView.annotations.filter{$0 !== mapView.userLocation}
            mapView.removeAnnotations(annoRem)
let ann = MKPointAnnotation()
            ann.coordinate.latitude = Double(stringOfLatitude)!

            ann.coordinate.longitude = Double(stringOfLongitude)!
mapView.addAnnotation(ann)

//mapView is the name of the MapViewKit that was add to the storyboard
ann is the pin. You can add a title or subtitle to it.


Comments

Popular posts from this blog

Setting up a playground

Go to another page