【问题标题】:Mapbox iOS8 Swift mapView.showUsersLocationMapbox iOS8 Swift mapView.showUsersLocation
【发布时间】:2015-06-18 11:43:34
【问题描述】:

我正在尝试使用 Mapbox iOS8 Swift cocoa 插件用于 mapbox,并在尝试在 mapView 上显示用户位置时遇到问题。我的代码如下

func mapView(mapView: MGLMapView!, symbolNameForAnnotation annotation: MGLAnnotation!) -> String! {
    return "secondary_marker"
}

let manager = CLLocationManager()

override func viewDidLoad() {

    super.viewDidLoad()

    let mapView = MGLMapView(frame: view.bounds, accessToken: "pk.privateMapboxAccessTokenGoesHere")
    mapView.autoresizingMask = .FlexibleWidth | .FlexibleHeight

    if CLLocationManager.authorizationStatus() == .NotDetermined {
        manager.requestAlwaysAuthorization()
    }
    mapView.showsUserLocation = true

   let x:MGLUserLocation = mapView.userLocation
   println(mapView.userLocation)
   println(x.coordinate.latitude)
   println(x.coordinate.longitude)

   ... more code here to show map works.

}

我已经对 info.pList 进行了必要的更改,并在我第一次启动我的应用程序时收到了相应的消息。问题是,它会打印以下内容:

<MGLUserLocation: 0x7fd8aa6c9a00>
3.40282346638529e+38
3.40282346638529e+38

谁能提供我如何在地图上显示用户位置(蓝点)的示例。

【问题讨论】:

    标签: ios swift cllocationmanager mapbox


    【解决方案1】:

    要让地图自动以用户位置为中心,请设置mapView.userTrackingMode = .Follow(在Objective C 中为MGLUserTrackingModeFollow)。

    要仅显示用户的位置(但不移动到该位置),请设置mapView.showsUserLocation = true

    您看到mapView.userLocation 的虚假数字的原因是用户的位置通常在viewDidLoad 中不可用。使用mapView:didUpdateUserLocation: 委托方法在用户位置可用和更新时收到通知。

    【讨论】:

      【解决方案2】:

      感谢您给我一个起点。

      我使用了您的代码并将 userLocation 添加到我的地图中,它正在为我显示。

          if let userLocation = mapView.userLocation {
              userLocation.title = "USER LOCATION MARKER"
              mapView.addAnnotation(userLocation)
          }
      

      请注意,我必须插入我的 iPhone 并从那里运行它。不知道它是否应该在模拟器中工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多