【发布时间】:2015-09-17 17:30:07
【问题描述】:
我有以下代码,它应该基本上检测位置(如果位置服务已打开),然后返回上一页并显示坐标(单击按钮时)。
@IBOutlet var mapView:GMSMapView!// 在 Storyboard 上添加谷歌地图检查 var locationManager:CLLocationManager! var viewController : 视图控制器! //保存前一个控制器的对象以返回位置
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
locationManager = CLLocationManager() // Intialize the location manager
locationManager.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Methos to change the MApType
@IBAction func changeMaptoEarthView()
{
self.mapView.mapType = kGMSTypeSatellite
}
@IBAction func changeMaptoMapView()
{
self.mapView.mapType = kGMSTypeNormal
}
//Method to get UserLocation
@IBAction func sendLocationtoPreviousView()
{
viewController.locationRecieved(locationManager.location)
self.performSegueWithIdentifier("backSegue", sender: nil)
}
@IBAction func addUserLocation() //Detect location click event
{
if(CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse) // If already have location access
{
locationManager.startUpdatingLocation()
}
else if (CLLocationManager.authorizationStatus() == .Denied)// If location access is denied in setting
{
UIAlertView(title: "Permission", message: "Please allow location services in setting", delegate: nil, cancelButtonTitle: "ok").show()
}
else
{
locationManager.requestWhenInUseAuthorization() // Ask user permission if permission not given
}
}
//CLLocation Manager Delegate methoda
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let location = locations.first as? CLLocation {
self.mapView.clear() // clear all overlay on Map
self.mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
// Adding pin on Current location
var marker = GMSMarker()
marker.position = location.coordinate
marker.snippet = "My Location"
marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = mapView
locationManager.stopUpdatingLocation()
}
}
应用程序在模拟器中运行,直到我说返回,然后它在这一行抛出 nil 错误消息:
viewController.locationRecieved(locationManager.location)
可能是什么原因?我可以在地图上看到图钉,虽然地图没有显示任何图形,除了下面的谷歌徽标
【问题讨论】:
标签: ios swift google-maps