私はプログラミングの初心者です。 私は以下のコードを見つけましたが、中心の場所を除いてすべてを行います(ズームイン、マップ、青い点すべての作業)。 シミュレータで(都市を走って)走ると、青い点がページから飛びます。"マップ上の中心位置"をどのようにして作れますか?
import UIKit
import MapKit
import CoreLocation
import Foundation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
var locationManager: CLLocationManager?
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager!.delegate = self
map.showsUserLocation = true
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
locationManager!.startUpdatingLocation()
} else {
locationManager!.requestWhenInUseAuthorization()
}
}
private func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .notDetermined:
print("NotDetermined")
case .restricted:
print("Restricted")
case .denied:
print("Denied")
case .authorizedAlways:
print("AuthorizedAlways")
case .authorizedWhenInUse:
print("AuthorizedWhenInUse")
locationManager!.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.first!
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500)
map.setRegion(coordinateRegion, animated: true)
locationManager?.stopUpdatingLocation()
locationManager = nil
}
}
マップビューで 'userTrackingMode'プロパティを使用するだけではどうですか? – xoudini
ありがとうございますXoudini私はプログラミングの初心者で、userTrackingModeについて何も知らないので、どのように使用するかわかりません。 – Doug
'yourMapView.userTrackingMode = .follow'は、位置情報サービスがオンになっているときにユーザーを自動的にセンタリングします。[documentation](https://developer.apple.com/reference/mapkit/mkmapview/1616208-usertrackingmode)を参照してください。 – xoudini