import UIKit
import GoogleMaps
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
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: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}
这是我编写的代码,但我无法获取用户的位置。
我在 info.plist
中添加了 NSLocationWhenInUseUsageDescriptation
。
有人可以帮我解决这个问题吗?我已经尝试这个很久了。
我在这些行中遇到错误。
=================================
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
==================================
有人可以帮我解决这个问题吗?
1“MKMapView”类型的值没有成员“myLocationEnabled” 2 “MKMapView”类型的值没有成员“settings” 3 无法分配“GMSCameraPosition!”类型的值输入“MKMapCamera”
这是我遇到的 3 个错误
我的确切错误是在 @IBOutlet weak var mapView: MKMapView 上! 我是改变这个@IBOutlet 弱变量mapView:GMSMapView! 我收到线程 1 错误@IBOutlet weak var mapView: MKMapView!在这唯一的 map 显示中,但没有固定我的位置
我不会为答案添加评论
最佳答案
您正在声明 MKMapView
的实例(MapKit 框架提供的 mapView)。您应该使用 GMSMapView
的实例(Google map 框架的 map )才能使用这些功能。
关于ios - 如何使用google api获取用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34761552/