地图按钮刷新位置 [英] Map button refresh location
本文介绍了地图按钮刷新位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前我有这个代码:
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(纬度: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)
}
覆盖func viewDidLoad(){
super.viewDidLoad()
self.locationManager.delegate = self
self。 locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
}
覆盖func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理可以重新创建的任何资源。
}
//位置委托方法
func locationManager(manager:CLLocationManager,didUpdateLocations locations:[CLLocation]){
let location = locations.last
let center = CLLocationCoordinate2D(纬度: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.localizedDescription )
}
}
< >如何获得刷新按钮来做到这一点?我真的需要一些帮助,因为我是新的Swift / xcode:)
谢谢
解决方案
由于@Orkhan说你可以这样做。
如果你想做这个动作,你只需简单的Ctrl Ctrl拖动到viewController选择动作。
之后,您可以添加代码到处理程序。
var location:CLLocation!
@IBAction func refreshLocation(sender:AnyObject){
let center = CLLocationCoordinate2D(纬度: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
}
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
解决方案
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".
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
}
这篇关于地图按钮刷新位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文