我无法弄清楚如何让地图从viewDidLoad放大用户位置。我尝试设置一个区域,但这并没有起作用。这是我的代码,任何提示?
@IBOutlet弱var mapView:MKMapView! var MapViewLocationManager:CLLocationManager! = CLLocationManager() var currentLocation:PFGeoPoint! = PFGeoPoint()
override func viewDidLoad() {
super.viewDidLoad()
self.mapView.showsUserLocation = true
mapView.delegate = self
MapViewLocationManager.delegate = self
mapView.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true)
}
我已经查找了这个问题的答案但是还没有找到Swift的答案或者确实有效。谢谢!!
答案 0 :(得分:15)
我会尝试这样的事情:
let latitude:CLLocationDegrees = //insert latitutde
let longitude:CLLocationDegrees = //insert longitude
let latDelta:CLLocationDegrees = 0.05
let lonDelta:CLLocationDegrees = 0.05
let span = MKCoordinateSpanMake(latDelta, lonDelta)
let location = CLLocationCoordinate2DMake(latitude, longitude)
let region = MKCoordinateRegionMake(location, span)
mapView.setRegion(region, animated: false)
我看到你说你试过设置一个区域。也许尝试这样做。
答案 1 :(得分:6)
在大多数应用中,当前位置'按钮已实现。一个简单的方法就是这样:
@IBAction func myLocationButtonTapped(_ sender: Any) {
mapView.showsUserLocation = true
mapView.setUserTrackingMode(.follow, animated: true)
}
用户平移或缩放地图后,跟踪将停止。所以对我来说这已经足够了。
答案 2 :(得分:6)
你必须试试这个。 它会缩放地图。
let span = MKCoordinateSpanMake(0.050, 0.050)
let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 23.0225, longitude: 72.5714), span: span)
mapView.setRegion(region, animated: true)`
答案 3 :(得分:0)
@IBOutlet weak var conscienceMap: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
conscienceMap.showsUserLocation = true
conscienceMap.setUserTrackingMode(MKUserTrackingMode.follow, animated: true)
}