显示用户位置按钮 - Swift

Posted

技术标签:

【中文标题】显示用户位置按钮 - Swift【英文标题】:Show user location button - Swift 【发布时间】:2017-01-05 14:51:30 【问题描述】:

我已经在代码中设置了一个 MKCordinateSpan。我还实现了一个按钮,可以让我直接回到我在地图上的位置。就像一个“我的位置按钮”。它是一个名为 locationManagerButton() 的函数。如何将跨度设置为与其他函数中的相同。这是我的代码:

import UIKit
import MapKit

class MapController:  UICollectionViewController, UICollectionViewDelegateFlowLayout, CLLocationManagerDelegate, MKMapViewDelegate 

var window: UIWindow?
var mapView: MKMapView?
let locationManager = CLLocationManager()

let distanceSpan: Double = 500


override func viewDidLoad() 
    super.viewDidLoad()

    navigationItem.title = "Current Location"

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.view.backgroundColor = UIColor.whiteColor()

    self.mapView = MKMapView(frame: CGRectMake(0, 20, (self.window?.frame.width)!, (self.window?.frame.height)!))
    self.view.addSubview(self.mapView!)

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
    self.mapView!.showsUserLocation = true


    //Show User Location Button
    let button: UIButton = UIButton(type: UIButtonType.Custom)
    button.setImage(UIImage(named: "MyLocationButton"), forState: UIControlState.Normal)
    button.addTarget(self, action: #selector(locationManagerButton), forControlEvents: UIControlEvents.TouchUpInside)
    button.frame = CGRectMake(0, 0, 30, 30)
    let barButton = UIBarButtonItem(customView: button)
    self.navigationItem.leftBarButtonItem = barButton




func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) 
    if let mapView = self.mapView 
        let region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, self.distanceSpan, self.distanceSpan)
        mapView.setRegion(region, animated: true)
        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.01, longitudeDelta: 0.01))

    self.mapView!.setRegion(region, animated: true)
    self.locationManager.stopUpdatingLocation()

    let locValue:CLLocationCoordinate2D = manager.location!.coordinate
    print("locations = \(locValue.latitude) \(locValue.longitude)")





func locationManagerButton() 
    mapView!.setCenterCoordinate(mapView!.userLocation.coordinate, animated: true)





func locationManager(manager: CLLocationManager, didFailWithError error: NSError)

    print("Errors: " + error.localizedDescription)




【问题讨论】:

【参考方案1】:

尝试将您的功能更改为此。

func locationManagerButton(manager: CLLocationManager, 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.01, longitudeDelta: 0.01))

    mapView!.setRegion(region, animated: true)
    mapView!.setCenterCoordinate(mapView!.userLocation.coordinate, animated: true)



【讨论】:

嗨,当我按下按钮时出现此错误。 'NSInvalidArgumentException',原因:'-[UITouchesEvent copyWithZone:]:无法识别的选择器发送到实例 0x7fbc7b409fb0'

以上是关于显示用户位置按钮 - Swift的主要内容,如果未能解决你的问题,请参考以下文章

按下主页按钮时,Swift CLLocationManager 弹出窗口不会停留

当用户在swift中按下注销按钮时如何停止更新位置

iOS swift将我的位置按钮移动到右上角

如何使用 swift 在谷歌地图中更改我的位置按钮的位置

如何在swiftUI中添加返回用户位置按钮?

每次按下按钮时重置用户默认值(swift 4)