我已经在代码中设置了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)
}
}
答案 0 :(得分:0)
尝试将您的功能更改为此功能。
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)
}