Swift 谷歌地图不显示当前位置或定位按钮
我将我的google地图视图嵌入到一个选项卡栏控制器中,它是选项卡中的第二项。视图加载地图,但不显示当前位置或“定位”按钮。我已经编辑了方案,以包含要使用的构建位置Swift 谷歌地图不显示当前位置或定位按钮,swift,google-maps-sdk-ios,Swift,Google Maps Sdk Ios,我将我的google地图视图嵌入到一个选项卡栏控制器中,它是选项卡中的第二项。视图加载地图,但不显示当前位置或“定位”按钮。我已经编辑了方案,以包含要使用的构建位置 var locationManager = CLLocationManager() var didFindMyLocation = false override func viewDidLoad() { super.viewDidLoad() var camera = GM
var locationManager = CLLocationManager()
var didFindMyLocation = false
override func viewDidLoad() {
super.viewDidLoad()
var camera = GMSCameraPosition.cameraWithLatitude(33.7,
longitude: -77.4, zoom: 8.0)
mapView.camera = camera
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
// Do any additional setup after loading the view.
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.AuthorizedWhenInUse {
mapView.myLocationEnabled = true
}
}
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if !didFindMyLocation {
let myLocation: CLLocation = change[NSKeyValueChangeNewKey] as! CLLocation
mapView.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 10.0)
mapView.settings.myLocationButton = true
didFindMyLocation = true
}
}
var locationManager=CLLocationManager()
var didFindMyLocation=false
重写func viewDidLoad(){
super.viewDidLoad()
var camera=GMSCameraPosition.cameraWithLatitude(33.7,
经度:-77.4,缩放:8.0)
mapView.camera=camera
locationManager.delegate=self
locationManager.RequestWhenUseAuthorization()
addObserver(self,forKeyPath:“myLocation”,选项:NSKeyValueObservingOptions.New,上下文:nil)
//加载视图后执行任何其他设置。
}
func locationManager(经理:CLLocationManager!,didChangeAuthorizationStatus:CLAuthorizationStatus){
如果状态==CLAuthorizationStatus.AuthorizedWhenInUse{
mapView.myLocationEnabled=true
}
}
重写func observeValueForKeyPath(键路径:字符串,对象对象:AnyObject,更改:[NSObject:AnyObject],上下文:UnsafeMutablePointer){
如果!发现了错配{
让myLocation:CLLocation=将[NSKeyValueChangeNewKey]更改为!CLLocation
mapView.camera=GMSCameraPosition.cameraWithTarget(myLocation.coordinate,zoom:10.0)
mapView.settings.myLocationButton=true
didFindMyLocation=true
}
}
如果以前设置了位置权限func locationManager(manager:CLLocationManager!,didChangeAuthorizationStatus:CLAuthorizationStatus)
可能没有被调用,因为授权状态没有更改
您也可以手动执行授权检查,然后启用地图上的位置
if CLLocationManager.locationServicesEnabled()
&& CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse {
mapView.myLocationEnabled = true
}
如何从该点获取用户位置?我在viewdidload中实现了上面的功能,并添加了:mapView.camera=GMSCameraPosition.cameraWithTarget(mapView.myLocation.coordinate,zoom:10.0)在if语句块中,但程序崩溃,因为mapView.myLocation.coordinate为nil。@wxcoder映射视图从核心位置异步检索当前位置(如果可用),因此它不会立即可用。根据原始示例中的内容,将检索位置以更新observeValueForKeyPath中的相机,一切都应该正常。