【发布时间】:2016-10-29 10:18:56
【问题描述】:
我一直在尝试在我的应用程序中实现谷歌地图。我想做的第一步就是简单地得到: 1.获取应用程序请求使用当前位置的权限 2.找到用户当前的位置,并有一个蓝点标记来标记它。 3. 更新地图并以用户当前位置为中心。
我尝试了各种方法来使其正常工作,但总是以错误或无法正常工作而告终。最初,当我模拟下面的代码时,它确实出现了关于请求当前位置许可和一个蓝点的对话框,但在下面的模拟中,这似乎已经消失了,当我在模拟器中更改我的当前位置时(通过编辑方案并更改default location) ,新的当前位置没有更新。我感觉代码在 viewDidLoad 之外无法正常工作。我哪里错了?
请我就如何纠正这些问题并让上述几点发挥作用获得一些建议(以及模拟器始终请求当前位置的许可)。目前代码运行没有错误,但只向我显示了英国的地图。 (我已经将 NSLocationWhenInUse 和 NSLocationAlwaysUsage.... 添加到 info.plist 中。并且我已经禁用了所有断点。我也尝试过重置模拟器)
import UIKit
import GoogleMaps
class FirstViewController: UIViewController,CLLocationManagerDelegate {
@IBOutlet weak var googlemaps: GMSMapView!
//this is the connection from the storyboard to the view controller
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: 51.508742, longitude: -0.087891, zoom: 8.0)
let viewMap = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = viewMap
viewMap.isMyLocationEnabled = true
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
override func loadView() {
super.viewDidLoad()
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
// verification of granted permission while app in use
let locationManager = CLLocationManager()
let mapView = GMSMapView()
if status == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
// if permission granted- starting updating location
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
//this is suppose to be button that is added when tapped centers to users location
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.camera = GMSCameraPosition.camera(withTarget: location.coordinate, zoom: 20)
self.view = mapView
locationManager.stopUpdatingLocation()
}
}
}
}
}
提前致谢!我花了几天时间试图解决这个问题,但没有成功:(
【问题讨论】:
标签: ios iphone xcode google-maps currentlocation