使用当前位置更新屏幕中心
update center of screen with current location
我已经成功制作了一个应用程序,它在用户当前所在的位置显示一个闪烁的蓝点。然而,截至目前,当用户移动时,屏幕中心不会跟随蓝点,所以如果用户移动蓝点,蓝点就会退出屏幕,用户必须滚动才能跟上它。那不是用户友好的!我有以下代码:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
let locationManager = CLLocationManager()
var mapp = MKMapView.self
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if (CLLocationManager.locationServicesEnabled())
{
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.map.showsUserLocation = true
}
else
{
print("Location services are not enabled")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
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.002, longitudeDelta: 0.002))
self.map.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
}
我唯一的想法是,我可以摆脱“stopUpdatingLocation()”部分,然后它可能会不断更新每个新位置的新中心区域,但我不确定是否没有 stopUpdatingLocation不好的做法?感谢所有建议!
使用 userTrackningMode 使地图视图将地图以该位置为中心并开始跟踪用户的位置。如果缩小地图,地图视图会自动放大用户的位置,有效地改变当前可见区域。
使用它
mapView.setUserTrackingMode(.follow, animated:true)
我已经成功制作了一个应用程序,它在用户当前所在的位置显示一个闪烁的蓝点。然而,截至目前,当用户移动时,屏幕中心不会跟随蓝点,所以如果用户移动蓝点,蓝点就会退出屏幕,用户必须滚动才能跟上它。那不是用户友好的!我有以下代码:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
let locationManager = CLLocationManager()
var mapp = MKMapView.self
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if (CLLocationManager.locationServicesEnabled())
{
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.map.showsUserLocation = true
}
else
{
print("Location services are not enabled")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
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.002, longitudeDelta: 0.002))
self.map.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
}
我唯一的想法是,我可以摆脱“stopUpdatingLocation()”部分,然后它可能会不断更新每个新位置的新中心区域,但我不确定是否没有 stopUpdatingLocation不好的做法?感谢所有建议!
使用 userTrackningMode 使地图视图将地图以该位置为中心并开始跟踪用户的位置。如果缩小地图,地图视图会自动放大用户的位置,有效地改变当前可见区域。
使用它
mapView.setUserTrackingMode(.follow, animated:true)