Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map button refresh location

I am working on my first swift application for the iPad. Thus far I have a basic mapview with a button in the bottom toolbar, which I would like to refresh and focus onto the users location once clicked.

Currently I have this code:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

var location: CLLocation!
let locationManager = CLLocationManager()

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var refresh: UIBarButtonItem!

@IBAction func refresh(sender: AnyObject) {

    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.mapView.setRegion(region, animated: true)
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.locationManager.delegate = self

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

    self.locationManager.requestWhenInUseAuthorization()

    self.locationManager.startUpdatingLocation()

    self.mapView.showsUserLocation = true

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// location delegate methods

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: 1, longitudeDelta: 1))

    self.mapView.setRegion(region, animated: true)

    self.locationManager.stopUpdatingLocation()

}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
    print("Error code: " + error.localizedDescription)
}

}

How can I get the refresh button to do this? I really need some help as I am new to Swift / xcode :)

Thank you

like image 441
Oscar Avatar asked Oct 17 '15 13:10

Oscar


3 Answers

As @Orkhan said you can do it in this way.

If you want to do the action you just simple ctrl-drag to viewController and select "Action".

enter image description here

After that you can add code to the handler.

    var location: CLLocation!
    @IBAction func refreshLocation(sender: AnyObject) {

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

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    self.location = locations.last as CLLocation
}
like image 191
razor118 Avatar answered Oct 31 '22 22:10

razor118


Try the following 3 line code:

@IBAction func refresh(sender: AnyObject) {
    mapView.setCenterCoordinate(mapView.userLocation.coordinate, animated: true)
}

EDIT

Check the following:

Can you see the dot on the left side of the method?

enter image description here

I want see your connections inspector. Refresh button and refresh method are connected correctly?

enter image description here

like image 22
Kosuke Ogawa Avatar answered Oct 31 '22 22:10

Kosuke Ogawa


You can update your location with this function:

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

If your target is iOS 8, you need to add the NSLocationAlwaysUsageDescription key in your Info.plist.

like image 1
Orkhan Alizade Avatar answered Oct 31 '22 20:10

Orkhan Alizade