在xcode应用程序上,我显示了数据库中的许多图钉以及我的位置,但是地图并未以我的位置为中心
覆盖func viewDidLoad(){ super.viewDidLoad()
// Do any additional setup after loading the view.
// Create coordinates from location lat/long
var poiCoodinates = CLLocationCoordinate2D()
tab = selectedEtablissement as! [EtablissementModel]
for item in 0..<tab.count{
poiCoodinates.latitude = tab[item].lonetablissement!
poiCoodinates.longitude = tab[item].lngetablissement!
// Zoom to region
let viewRegion: MKCoordinateRegion = MKCoordinateRegion.init(center: poiCoodinates, latitudinalMeters: 750, longitudinalMeters: 750)
self.mapB******.setRegion(viewRegion, animated: true)
// Plot pin
let pin: MKPointAnnotation = MKPointAnnotation()
pin.coordinate = poiCoodinates
self.mapB*******.addAnnotation(pin)
//add title to the pin
pin.title = tab[item].nometablissement
}
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.startUpdatingLocation()
mapBieres.showsUserLocation = true
self.locationManager.stopUpdatingLocation()
}
在许多城市显示图钉,并使用此代码显示我的位置,但地图并未以我的位置为中心。我想要地图上的所有图钉,但地图以我的位置为中心。谢谢
答案 0 :(得分:-1)
`import MapKit
import CoreLocation
class ViewController: UIViewController {
var locationManager = CLLocationManager()
@IBOutlet weak var mapView: MKMapView!
func setLocation() { // Seeting up current location
locationManager.delegate = self as CLLocationManagerDelegate
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
// Setting the current location to the ashton building, & the zoom of the map
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locationOfUser = locations[0]
let lat = locationOfUser.coordinate.latitude
let long = locationOfUser.coordinate.longitude
let latDelta: CLLocationDegrees = 0.003 // Distance from the map
let lonDelta: CLLocationDegrees = 0.003
let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)
let location = CLLocationCoordinate2D(latitude: lat, longitude: long)
let region = MKCoordinateRegion(center: location, span: span)
mapView.setRegion(region, animated: true)
}
}`
以下代码应帮助您设置当前位置以及地图的距离缩放。 (如果我没有记错的话,请确保在设置中打开了当前位置)。 希望这无论如何都能有所帮助,必须仔细阅读我以前的Swift作业:)