【发布时间】:2016-07-28 03:34:05
【问题描述】:
我正在使用 iOS(Swift) 的 Google maps sdk。
有谁知道如何“在我打开 ViewController 时在谷歌地图上显示我的当前位置”?
其实它就像谷歌地图应用。当您打开谷歌地图时,蓝点会显示您当前的位置。您不需要第一次按“myLocationButton”。
所以这是代码:
import UIKit
import CoreLocation
import GoogleMaps
class GoogleMapsViewer: UIViewController {
@IBOutlet weak var mapView: GMSMapView!
let locationManager = CLLocationManager()
let didFindMyLocation = false
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.cameraWithLatitude(23.931735,longitude: 121.082711, zoom: 7)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
self.view = mapView
// GOOGLE MAPS SDK: BORDER
let mapInsets = UIEdgeInsets(top: 80.0, left: 0.0, bottom: 45.0, right: 0.0)
mapView.padding = mapInsets
locationManager.distanceFilter = 100
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
// GOOGLE MAPS SDK: COMPASS
mapView.settings.compassButton = true
// GOOGLE MAPS SDK: USER'S LOCATION
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
// MARK: - CLLocationManagerDelegate
extension GoogleMapsViewer: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 20, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}
有人帮忙吗?非常感谢!
【问题讨论】:
-
这足以显示当前位置 // GOOGLE MAPS SDK: COMPASS mapView.settings.compassButton = true // GOOGLE MAPS SDK: USER'S LOCATION mapView.myLocationEnabled = true mapView.settings.myLocationButton = true
-
是的,显示当前位置就足够了。但是我可以在当前位置设置摄像头吗?
标签: ios swift google-maps