gpt4 book ai didi

ios - Swift:谷歌地图代码抛出零异常

转载 作者:搜寻专家 更新时间:2023-11-01 07:33:42 25 4
gpt4 key购买 nike

enter image description here我有以下代码,它基本上应该检测位置(如果位置服务打开),然后返回到上一页并显示坐标(单击按钮时)。

@IBOutlet var mapView:GMSMapView!//在 Storyboard 上添加 Google map 检查 var locationManager: CLLocationManager! 变种 View Controller : View Controller !//保存前一个 Controller 的对象以将位置发回

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)

可能是什么原因?我可以在 map 上看到图钉,尽管 map 上除了下面的谷歌 Logo 外没有显示任何图形

最佳答案

如果您在 View (GMSMapView 的子类)中看不到 map 图形,这是因为 Google map API key ,可能您尚未在您的应用委托(delegate)中设置它,或者它设置不正确!

确保您已使用您的应用程序包 ID 和您在 google 中的帐户设置了正确的 google API key 。您必须在您的应用删除中设置以下代码,执行 didFinishLaunchingWithOptions 功能:

import GoogleMaps

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
GMSServices.provideAPIKey("your API Key")
}

关于ios - Swift:谷歌地图代码抛出零异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31146392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com