我正在使用谷歌地图显示一组引脚的应用,我希望地图的相机从用户的位置开始。我尝试编写以下代码(在Swift 2.0中),但它没有把我放在设备的当前位置,而是它向我显示一个空白的蓝色屏幕(我发现它是地图的中间位置)。这是我所拥有的屏幕截图
我在编写此代码时使用了以下库:
Google Maps 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.
}
}
这是运行应用程序后的控制台输出。请注意,它确实输出用户的当前位置:
1 + 51.50998000,-0.13370000> +/- 5.00m(速度-1.00 mps /航向-1.00)@ 2/3 / 16,8:47:57太平洋标准时间 2016-02-03 08:47:57.545 Hanky Ranky [3379:260848]适用于iOS版的Google Maps SDK:1.11.21919.0
感谢帮助人员!
答案 0 :(得分:1)
也许你在ViewController.swift中缺少两个属性。
var locationManager = CLLocationManager()
var didFindMyLocation = false
locationManager 属性将用于询问用户是否允许跟踪他/她的位置,然后根据授权状态显示他/她的当前位置。将使用 didFindMyLocation 标志,以便我们知道用户的当前位置是否在地图上被发现,并最终避免不必要的位置更新。
此Swift Tutorial for Google Maps可能会有所帮助。
答案 1 :(得分:0)
class ViewController: UIViewController, GMSAutocompleteViewControllerDelegate
{
var placeName: String = "Default"
var userlat: CLLocationDegrees = 0
var userlng: CLLocationDegrees = 0
override func viewDidLoad() {
super.viewDidLoad()
let acController = GMSAutocompleteViewController()
acController.delegate = self
self.presentViewController(acController, animated: true, completion: nil)
// Do any additional setup after loading the view.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let camera = GMSCameraPosition.cameraWithLatitude(userlat,
longitude: userlng, zoom: 6)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
mapView.delegate = self
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
self.view = mapView
}
func viewController(viewController: GMSAutocompleteViewController!, didAutocompleteWithPlace place: GMSPlace!) {
lat = place.coordinate.latitude
longi = place.coordinate.longitude
placeName = place.name
}
}
我希望这会有所帮助......