加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 地图按钮刷新位置

发布时间:2020-12-14 05:48:26 所属栏目:百科 来源:网络整理
导读:我正在开发我的第一个快速iPad应用程序.到目前为止,我有一个基本的mapview,底部工具栏中有一个按钮,我想刷新一次,并在点击后关注用户位置. 目前我有这个代码: import UIKitimport MapKitimport CoreLocationclass ViewController: UIViewController,MKMapVi
我正在开发我的第一个快速iPad应用程序.到目前为止,我有一个基本的mapview,底部工具栏中有一个按钮,我想刷新一次,并在点击后关注用户位置.

目前我有这个代码:

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

}

如何获取刷新按钮?我真的需要一些帮助,因为我是Swift / xcode的新手:)

谢谢

正如@Orkhan所说,你可以这样做.

如果你想做这个动作你只需简单地按住Ctrl键拖动到viewController并选择“Action”.

之后,您可以将代码添加到处理程序.

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

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude,longitude: location.coordinate.longitude)
    let region = MKCoordinateRegion(center: center,longitudeDelta: 0.01))

    self.map.setRegion(region,animated: true)
}

func locationManager(manager: CLLocationManager!,didUpdateLocations locations: [AnyObject]!) {
    self.location = locations.last as CLLocation
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读