I have a mapKit on a view controller. I want the map to show the users current location. it seems to work but the users location is stuck on the right hand side of the screen. when you try scrolling it away from the right it just pops back there. Cannot fathom what I am doing wrong? Any help would be appreciated.
override func viewDidLoad() {
super.viewDidLoad()
clientAddressTextField.text = clientAddress
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
Kind regards
Wayne
Ensure that you have set constraints for your map view, otherwise it will have some default size & location, which may well be offscreen
Use the mapView's delegate method mapView:didUpdateUserLocation:
func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
mapView.setRegion(MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800), animated: true)
}
Related
I've done the research on here to try solve my problem, but I cant seem to find a solution. I am wanting my map to rotate, so the top of my screen is the direction which the user is travelling. The code below works but rotates the screen based on the direct the phone is facing. I only want the map to rotate when the user moves in a different direction.
Has anyone solved this problem?
let manager = CLLocationManager()
#IBOutlet weak var map: MKMapView!
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading){
map.camera.heading = newHeading.magneticHeading
map.setCamera(map.camera, animated: true)
print("updatingHeading is called")
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpan.init(latitudeDelta: 0.01,longitudeDelta: 0.01)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegion.init(center: myLocation, span: span)
map.setRegion(region, animated: true)
self.map.showsUserLocation = true
let mapCamera = MKMapCamera(lookingAtCenter: myLocation, fromDistance: 5000, pitch: 30, heading: 0)
map.setCamera(mapCamera, animated: true)
}
func viewDidLoad() {
manager.startUpdatingLocation()
manager.startUpdatingHeading()
}
Is there anything built into swift to help me?
cheers
I don't believe this question has been asked yet in Swift 3.0 - three goals:
Upon viewDidLoad the Map Centers to the User Location at a certain zoom level that can be set (for example let span: MKCoordinateSpan = MKCoordinateSpanMake(40.0, 40.0))
Once the map loads and centers on the User Location, the User can then move and scroll the map to any other location WITHOUT the map automatically snapping back to the original User Location
Allow the User to ONLY zoom in to a certain level but allow the User to zoom out fully to view the entire global map (no restrictions on the zoom out level)
Here is my code thus far:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span: MKCoordinateSpan = MKCoordinateSpanMake(40.0, 40.0)
let userLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(userLocation, span)
mapView.setRegion(region, animated: true)
self.mapView.showsUserLocation = true
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
}
1. Should work with the code you have now.
2. Add check for subsequent location updates
In the didUpdateLocations method, add a Bool to check whether the region was centered on the user already or not.
var regionHasBeenCentered = false
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
if !regionHasBeenCentered {
let span: MKCoordinateSpan = MKCoordinateSpanMake(40.0, 40.0)
let userLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(userLocation, span)
mapView.setRegion(region, animated: true)
regionHasBeenCentered = true
}
self.mapView.showsUserLocation = true
}
Now the map will no longer center on the user after the first update, until you change regionHasBeenCentered back to false. This will allow the user to scroll and zoom freely.
3. Implement MKMapViewDelegate method to detect map region changes
Implement MKMapViewDelegate on your view controller so that you can check for region changes.
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
…and set the view controller as the delegate:
override func viewDidLoad() {
// other things…
mapView.delegate = self
}
Then implement the following method which will be called right before the region changes. Here you can check to see if the span's dimensions are too small, and set them to a minimum appropriate.
func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
if mapView.region.span.latitudeDelta <= 40 && mapView.region.span.longitudeDelta <= 40 {
let minimumSpan = MKCoordinateSpan(latitudeDelta: 40, longitudeDelta: 40)
let minimumRegion= MKCoordinateRegion(center: mapView.centerCoordinate, span: minimumSpan)
mapView.setRegion(minimumRegion, animated: false)
}
}
Important note: From the MKCoordinateSpan documentation, the longitudeDelta will change as you move toward/away from the equator.
longitudeDelta
The amount of east-to-west distance (measured in degrees) to display for the map region. The number of kilometers spanned by a longitude range varies based on the current latitude. For example, one degree of longitude spans a distance of approximately 111 kilometers (69 miles) at the equator but shrinks to 0 kilometers at the poles.
Furthermore, MKCoordinateSpan's dimensions are measured in degrees, and 40 degrees is quite a bit so you probably want to change these values, otherwise the user will not be able to zoom in much at all.
I have a tab bar, one is used to display the map. When I switch to this tab bar, it will request permission and get the current location automatically.
I finished the code, set the simulated location to Apple, it will show a blue dot in the center of the screen, but when I chose the custom location, specify a location, the blue dot is not displayed in the center of the map.
I need to drag the map, and will find the blue dot near the center location about 1~2km. I tested on the real device and have the same problem.
use the method to show the current location auto, but not show the blue dot.
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager!.delegate = self
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse{
locationManager!.startUpdatingLocation()
locationManager!.desiredAccuracy = kCLLocationAccuracyBestForNavigation
}else{
locationManager!.requestWhenInUseAuthorization()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last!
let coordinateRegion = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 100, 100)
mapView.setRegion(mapView.regionThatFits(coordinateRegion), animated: true)
location = newLocation
locationManager?.stopUpdatingLocation()
locationManager = nil
}
I added a button - when it not show the blue dot, clicking the button will set the blue dot in the center of the map. The two methods have the same function, but they produce different views. Why?
#IBAction func showUser() {
let region = MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 100, 100)
mapView.setRegion(mapView.regionThatFits(region), animated: true)
}
Screenshot
I just use this coding to update to location with center of mapview
Please add this keys into info.plist file
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSLocationAlwaysUsageDescription</key>
<string></string>
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
map.delegate = self
map.showsUserLocation = true
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse{
locationManager.startUpdatingLocation()
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
}else{
locationManager.requestWhenInUseAuthorization()
}
}
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
mapView .setCenter(userLocation.coordinate, animated: true)
}
You can download demo from HERE
I have successfully made an app that shows a blinking blue dot where the user currently is. However as of right now the center of the screen does not follow the blue dot as the user moves, so if the user moves the blue dot just exits the screen and the user would have to scroll to keep up with it. That isnt user friendly! I have the below code:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet weak var map: MKMapView!
let locationManager = CLLocationManager()
var mapp = MKMapView.self
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if (CLLocationManager.locationServicesEnabled())
{
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.map.showsUserLocation = true
}
else
{
print("Location services are not enabled")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.002, longitudeDelta: 0.002))
self.map.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
}
My only thoughts are that I could get rid of the "stopUpdatingLocation() section and maybe then it would continuously update the region with the new center for every new location, however I am not sure if not having the stopUpdatingLocation is bad practice? Thanks for all advice!
Use userTrackningMode which causes the map view to center the map on that location and begin tracking the user’s location. If the map is zoomed out, the map view automatically zooms in on the user’s location, effectively changing the current visible region.
To use it
mapView.setUserTrackingMode(.follow, animated:true)
The code below works great but once you try to scroll around on the map or anywhere away from the user location, it pulls it right back and centers back on the user location. It won't release it.
How would you let it release after it centers in on the user's location after it loads?
import MapKit
import CoreLocation
class MapView : UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var map: MKMapView!
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
if (CLLocationManager.locationServicesEnabled()) {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations.last as CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.map.setRegion(region, animated: true)
}