Show Current Location and Update Location in Mkmapview in Swift

Show Current Location and Update Location in MKMapView in Swift

You have to override CLLocationManager.didUpdateLocations (part of CLLocationManagerDelegate) to get notified when the location manager retrieves the current location:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last{
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)
}
}

NOTE: If your target is iOS 8 or above, you must include the NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in your Info.plist to get the location services to work.

Swift MKMapView Drop a Pin Annotation to Current Location

If you want to add pin to user location you can do that in didUpdateLocations delegate method like this

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
mapView.removeAnnotation(newPin)

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))

//set region on the map
map.setRegion(region, animated: true)

newPin.coordinate = location.coordinate
map.addAnnotation(newPin)

}

Create a global variable for your pin

let newPin = MKPointAnnotation()

So whenever user will move to a new location the previous pin will be removed and a new pin will be added to updated location.

how to show current location on MKMapView

In the simulator, the user's current location is always in Cupertino, California.

If you're using Interface Builder to add your map view, simply check the "Shows User Location" check box in the Attributes Inspector for the map view. (Select the map view and type command-1 to display the attributes inspector.)

If you're adding or manipulating the map view programmatically, set the showsUserLocation property of the map view to YES.


Update: It turns out that this is possible, just not using the built in map view functionality, and it doesn't always work.

Recent versions of the SDK (which have to run on Snow Leopard) can get the location of the machine the simulator is running on using CLLocationManager. You can then use this location to create an annotation to display on the map view. It won't behave like the built in "user's location indicator" (at least not without some work), but it will show the user's current location.

See this post for details of when this technique won't work.

See the "Related sample code" section of the CLLocationManager documentation for sample code that uses CLLocationManager and CLLocationManagerDelegate and then displays the user's location on a map view.

Swift MapView Stuck Around User's Current Location

Your code has several problems:

You declare a variable newPin at global scope and in mapLongPress(...) you declare a new variable let newPin = ... locally so the global newPin isn't used.

In didUpdateLocations() you first remove the (global) newPin annotation (why??) and set it again at the end of the function. Because the global newPin was never set to anything useful this will never get the desired result.

Furthermore, in didUpdateLocations() you set the map's region and center point to the current location. This is done on every location update, giving weird results when trying to pan the map.

To set center and region when the view appears, try something like that:

class MapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
let locationManager = CLLocationManager()

// class variable for the current location
var lastLocation: CLLocation?

override func viewDidLoad() {
// ...
}

override func viewDidAppear(_ animated: Bool) {
if self.lastLocation != nil {
// set center and region to current location
let center = CLLocationCoordinate2D(latitude: self.lastLocation.coordinate.latitude, longitude: self.lastLocation.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

//set region on the map
map.setRegion(region, animated: true)
}
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.lastLocation = locations.last
}

}

MapKit zoom to user current location

I faced similar issue and wasted 4 days thinking whats going wrong. Finally resolved with creating these lines of code in viewDidLoad Method :

    //Zoom to user location
let noLocation = CLLocationCoordinate2D()
let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 200, 200)
mapView.setRegion(viewRegion, animated: false)
mapView.showsUserLocation = true

In ViewDidLoad Method add these new changes code :

override func viewDidLoad() {

super.viewDidLoad()

let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest

// Check for Location Services
if (CLLocationManager.locationServicesEnabled()) {
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
}

//Zoom to user location
if let userLocation = locationManager.location?.coordinate {
let viewRegion = MKCoordinateRegionMakeWithDistance(userLocation, 200, 200)
mapView.setRegion(viewRegion, animated: false)
}

self.locationManager = locationManager

DispatchQueue.main.async {
self.locationManager.startUpdatingLocation()
}

}

Hope this helps to resolve your issue. Feel free to post comment if any further issue. Thanks

current location in mapkit

you can get current location coordinates by CLLocationManager as :

let sourcelocation = self.locationManager.location?.coordinate



Related Topics



Leave a reply



Submit