gpt4 book ai didi

iOS Google Maps SDK 不显示用户位置

转载 作者:行者123 更新时间:2023-11-29 01:15:57 25 4
gpt4 key购买 nike

我正在开发一个使用谷歌地图显示一组图钉的应用程序,我希望 map 的相机在用户的位置上启动。我尝试编写以下代码(在 Swift 2.0 中),但它不会将我置于设备的当前位置,而是显示一个空白的蓝色屏幕(我发现它位于 map 的正中间)。这是我所拥有的屏幕截图

Here's a screenshot of what I have

我在编写此代码时使用了以下库:

Google map SDK

OneShotLocationManager

这是我的代码(在 ViewController.swift 中)

import UIKit
import GoogleMaps

class ViewController: UIViewController, GMSMapViewDelegate {

var manager: OneShotLocationManager?

var userlat: Double = 0.0
var userlng: Double = 0.0

override func viewDidLoad() {
manager = OneShotLocationManager()
manager!.fetchWithCompletion {location, error in

// fetch location or an error
if let loc = location {
print(loc)
self.userlat = loc.coordinate.latitude
} else if let err = error {
print(err.localizedDescription)
}
self.manager = nil
}


super.viewDidLoad()

let camera = GMSCameraPosition.cameraWithLatitude(userlat,
longitude: userlng, zoom: 6)

let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
mapView.delegate = self
self.view = mapView

let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
}

func mapView(mapView: GMSMapView!, markerInfoWindow marker: GMSMarker!) -> UIView! {
let infoWindow = NSBundle.mainBundle().loadNibNamed("CustomInfoWindow", owner: self, options: nil).first! as! CustomInfoWindow
infoWindow.label.text = "\(marker.position.latitude) \(marker.position.longitude)"
return infoWindow
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

这是运行应用程序后的控制台输出。请注意,它确实输出用户的当前位置:

<+51.50998000,-0.13370000> +/- 5.00 米(速度 -1.00 英里/航向 -1.00)@ 2/3/16,太平洋标准时间上午 8:47:572016-02-03 08:47:57.545 Hanky Ranky[3379:260848] 适用于 iOS 版本的 Google Maps SDK:1.11.21919.0

感谢各位的帮助!

最佳答案

也许您在 ViewController.swift 中缺少两个属性。

var locationManager = CLLocationManager()

var didFindMyLocation = false

locationManager 属性将用于请求用户允许跟踪他/她的位置,然后根据授权状态显示或不显示他/她的当前位置。将使用 didFindMyLocation 标志,以便我们知道用户的当前位置是否在 map 上被发现,并最终避免不必要的位置更新。

Swift Tutorial for Google Maps可能会有帮助。

关于iOS Google Maps SDK 不显示用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35183213/

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