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()
}
}
}
这是我编写的代码,但我无法获得用户的位置。
我在NSLocationWhenInUseUsageDescriptation
中添加了info.plist
。
有人可以帮我吗?我很长一段时间都在尝试这个。
我在这些方面遇到错误。
=================================
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
==================================
有人可以帮我解决这个问题吗?
1类型' MKMapView'没有会员' myLocationEnabled' 2类型的价值' MKMapView'没有会员的设置' 3无法指定类型' GMSCameraPosition的值!'输入' MKMapCamera'
这是我得到的3个错误
我的确切错误是在@IBOutlet弱var mapView:MKMapView! 我改变了这个@IBOutlet弱var mapView:GMSMapView! 我得到线程1错误@IBOutlet弱var mapView:MKMapView!在这个唯一的地图展示中,但没有固定我的位置
并没有为答案添加评论
答案 0 :(得分:0)
您正在声明MKMapView
的实例(MapKit Framework提供的mapView)。您应该使用GMSMapView
的实例(Google Maps Framework的地图)来使用这些功能。
答案 1 :(得分:0)
我已经这样做了,它就像一个魅力:
let locationManager = CLLocationManager()
var mapView:GMSMapView!
override func viewDidLoad() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: { (placemarks, error) -> Void in
if(error != nil) {
print("Error:" + error!.localizedDescription )
return
}
if(placemarks!.count > 0) {
if let pm = placemarks?.first {
self.displayLocationInfo(pm)
}
} else {
print("Error with data")
}
});
}
func displayLocationInfo(placemark: CLPlacemark) {
self.locationManager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error with Server");
}