9

ユーザーの位置の緯度と経度を見つけようとしているので、viewdidload でマップをユーザーの中心に置くことができます。

正しいコードと思われるものを実装しましたが、userLat (緯度) と userLon (経度) の値がかなりずれています。

注意他の誰かが私と同じ問題を抱えていましたが、彼の答えは決して解決されませんでした: Mapbox iOS8 Swift mapView.showUsersLocation

import Mapbox

class ViewController: UIViewController, MGLMapViewDelegate, CLLocationManagerDelegate {

//    let locationManager = CLLocationManager()

    @IBOutlet weak var mapView: MGLMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Initalise map's center coordinate as vancouver
        mapView.setCenterCoordinate(CLLocationCoordinate2D(latitude: 49.283382,
            longitude: -123.117394),
            zoomLevel: 15, animated: false)
        view.addSubview(mapView)

        // Set the delegate property of our map view to self after instantiating it.
        mapView.delegate = self

        // User location
        mapView.showsUserLocation = true

        let userLoc = mapView.userLocation!
        userLoc.title = "Hello"
        userLoc.subtitle = "I am here!"

        let userLat = userLoc.coordinate.latitude
        let userLon = userLoc.coordinate.longitude

        print(userLat, userLon)

        /*
        self.locationManager.requestAlwaysAuthorization()
        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()
        }*/

    }

    func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
        return true
    }

}

結果の印刷:

3.40282346638529e+38 3.40282346638529e+38

奇妙なことに、注釈は正常に機能し、場所をクリックするとタイトルとサブタイトルが表示されます。

4

2 に答える 2