Location services
Show a map with apple maps
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:
Setting the pins in the map for a location:
- Go to the object library and filter - look for 'Map Kit View'.
- Add constraints to it
- Go the the project navigator (left hand side of screen), click on the project name (topmost).
- Click on build phases
- Click on link binary with libraries.
- Click on the + sign.
- Search for map.
- Add Mapkit.framework
Edit the attributes
- Go to the main.storyboard.
- Look for the map view.
- Look at attributes inspector.
- 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.
- Go to the project navigator.
- Look for the info.plist and click on it.
- Click on required device capabilities
- Click on the + sign
- In the blank space, key in gps
- Click on the + sign again, key in location-services
Set permissions for location in info.plist
- Go back to info.plist.
- Click on the + sign next to Information Property list.
- Look for privacy - location. If you have problems looking for it, you might need to scroll down using the down arrow key.
- Always usage vs When in use - choose one (will it be used all the time?).
- Value - give them a description to tell them why they should let you know their location.
Getting and showing your location
- Ask for permissions in viewcontroller.
- Type import Mapkit
- Add the mapkit outlet to your class.
- Next to: class ViewController: UIViewController, add CLLocationManagerDelegate
- Initialise location manager as a property of the class, not as a local variable.
- let locationManager = CLLocationManager()
- In viewdidload, key
- locationManager.delegate = self
- 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))
}
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
Post a Comment