Google 地图 API 没有显示我的位置
Google Maps API not showing my location
我正在尝试在 GMSMapView
中显示 myLocation
。这是我目前的代码:
class ViewController: UIViewController {
// MARK: Properties
@IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
let stdZoom: Float = 12
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestWhenInUseAuthorization()
mapView.myLocationEnabled = true
if let myLocation = mapView.myLocation {
print("my location enabled");
let update = GMSCameraUpdate.setTarget(myLocation.coordinate, zoom: stdZoom)
mapView.moveCamera(update)
} else {
print("my location could not be enabled")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我按照问题 here 的建议将 CLLocationManager
设为 class 变量,并在我的 [=25= 中将 NSLocationWhenInUseUsageDescription
设置为字符串] 文件中 Xcode,但模拟器中从未显示允许定位服务的提示,我得到以下控制台输出:
2015-11-14 19:17:14.752 ios-demo[34759:569776] Simulator user has requested new graphics quality: 100
my location could not be enabled
2015-11-14 19:17:15.838 ios-demo[34759:569776] Google Maps SDK for iOS version: 1.10.21020.0
有人能指出我遗漏了什么吗?
我遵循了教程 here,特别是标题为 我的位置 的部分。我忽略了关于 func locationManager
的部分,而是将对 viewMap.myLocationEnabled = true
的调用放在 viewDidLoad
中。本教程对 observeValueForKeyPath
的覆盖似乎已经过时,需要进行一些调整,但这是我的完整代码:
class ViewController: UIViewController, CLLocationManagerDelegate {
// MARK: Properties
@IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
let stdZoom: Float = 12
var didFindMyLocation = false
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.requestWhenInUseAuthorization()
self.mapView.myLocationEnabled = true
self.mapView.settings.myLocationButton = true
self.mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if !didFindMyLocation {
let myLocation: CLLocation = change![NSKeyValueChangeNewKey] as! CLLocation
self.mapView.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 10.0)
self.mapView.settings.myLocationButton = true
didFindMyLocation = true
}
}
}
我正在尝试在 GMSMapView
中显示 myLocation
。这是我目前的代码:
class ViewController: UIViewController {
// MARK: Properties
@IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
let stdZoom: Float = 12
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestWhenInUseAuthorization()
mapView.myLocationEnabled = true
if let myLocation = mapView.myLocation {
print("my location enabled");
let update = GMSCameraUpdate.setTarget(myLocation.coordinate, zoom: stdZoom)
mapView.moveCamera(update)
} else {
print("my location could not be enabled")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我按照问题 here 的建议将 CLLocationManager
设为 class 变量,并在我的 [=25= 中将 NSLocationWhenInUseUsageDescription
设置为字符串] 文件中 Xcode,但模拟器中从未显示允许定位服务的提示,我得到以下控制台输出:
2015-11-14 19:17:14.752 ios-demo[34759:569776] Simulator user has requested new graphics quality: 100
my location could not be enabled
2015-11-14 19:17:15.838 ios-demo[34759:569776] Google Maps SDK for iOS version: 1.10.21020.0
有人能指出我遗漏了什么吗?
我遵循了教程 here,特别是标题为 我的位置 的部分。我忽略了关于 func locationManager
的部分,而是将对 viewMap.myLocationEnabled = true
的调用放在 viewDidLoad
中。本教程对 observeValueForKeyPath
的覆盖似乎已经过时,需要进行一些调整,但这是我的完整代码:
class ViewController: UIViewController, CLLocationManagerDelegate {
// MARK: Properties
@IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
let stdZoom: Float = 12
var didFindMyLocation = false
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.requestWhenInUseAuthorization()
self.mapView.myLocationEnabled = true
self.mapView.settings.myLocationButton = true
self.mapView.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if !didFindMyLocation {
let myLocation: CLLocation = change![NSKeyValueChangeNewKey] as! CLLocation
self.mapView.camera = GMSCameraPosition.cameraWithTarget(myLocation.coordinate, zoom: 10.0)
self.mapView.settings.myLocationButton = true
didFindMyLocation = true
}
}
}