Mapbox iOS8 Swift mapView.showUsersLocation [英] Mapbox iOS8 Swift mapView.showUsersLocation

查看:93
本文介绍了Mapbox iOS8 Swift mapView.showUsersLocation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Mapbox iOS8 Swift可可插件用于Mapbox,并试图在mapView上显示用户位置时遇到问题.我的代码如下

Im trying using Mapbox iOS8 Swift cocoa plugin for mapbox and facing an issue when trying to show the users location on a mapView. My code is the following

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进行了必要的更改,并在我第一次启动我的应用程序时得到了适当的消息.问题是,它会打印以下内容:

I've made the necessary changes to info.pList and get the appropriate message the first time i fire up my app. The issue is, it prints the following:

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

谁能给我一个例子,说明我如何在地图上显示用户的位置(蓝点).

Can anyone provide me an example of how I can show the users location (blue dot) on the map.

推荐答案

要使地图自动居中于用户的位置,请设置mapView.userTrackingMode = .Follow( MGLUserTrackingModeFollow ).

To have the map automatically center on the user's location, set mapView.userTrackingMode = .Follow (MGLUserTrackingModeFollow in Objective C).

要仅显示用户的位置(而不是移至该位置),请设置

To merely show the user's location (but not move to it), set mapView.showsUserLocation = true.

看到mapView.userLocation的虚假数字的原因是,viewDidLoad中通常尚不提供用户的位置.使用 mapView:didUpdateUserLocation: 委托方法,当用户的位置可用和更新时会得到通知.

The reason why you're seeing bogus numbers for mapView.userLocation is that the user's location typically isn't available yet in viewDidLoad. Use the mapView:didUpdateUserLocation: delegate method to be notified when the user's location becomes available and when it updates.

这篇关于Mapbox iOS8 Swift mapView.showUsersLocation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆