【问题标题】:how to get user's location using google api如何使用google api获取用户的位置
【发布时间】:2016-04-18 02:58:43
【问题描述】:
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”类型的值没有成员“设置” 3 无法分配类型“GMSCameraPosition!”的值输入“MKMapCamera”

这是我遇到的第三个错误

我的确切错误在@IBOutlet weak var mapView: MKMapView! 我是否更改了此@IBOutlet 弱var mapView:GMSMapView! 我得到线程 1 错误 @IBOutlet weak var mapView: MKMapView!在这个唯一的地图显示中,但没有固定我的位置

我不会为答案添加评论

【问题讨论】:

  • 您能否用您收到的错误编辑您的问题?
  • 我记得,你不需要在 info.plist 中添加一些东西。

标签: ios swift google-maps-sdk-ios


【解决方案1】:

您正在声明MKMapView 的实例(由 MapKit 框架提供的 mapView)。为了使用这些功能,您应该使用 GMSMapView 的实例,即 Google Maps Framework 的地图。

【讨论】:

    【解决方案2】:

    我已经这样做了,它就像一个魅力:

    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");
    }
    

    【讨论】:

    • 您已经有一个名为“mapView”的变量 - 您是否包含了 GoogleMaps 的 pod?
    • 当你尝试结合来自 MKMapView (Apple) 和 GMSMapView (Google) 的东西时你应该小心
    • 我添加了@IBOutlet weak var mapView: GMSMapView!
    • 您的项目中包含了适用于 iOS 的 GoogleMaps?有没有地图?
    • 因此您已将 GoogleMaps 包含到您的项目中,并且您可以编译(可能在清理之后) - 但随后收到错误 - 在哪里?哪一行代码?有什么细节吗?
    猜你喜欢
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 2015-03-23
    • 2021-12-07
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2014-01-07
    相关资源
    最近更新 更多