2016-01-13 32 views
0
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。 有人可以帮助我吗?我从很长一段时间一直在尝试。如何使用google api获取用户的位置

我在这些行中收到错误。

================================= 
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

可以请你,你收到的错误编辑你的问题? – lorenzoliveto

+0

我记得,你不需要在info.plist中添加一些东西。 – LKM

回答

0

你正在申报的MKMapView实例(由MapKit框架提供的MapView)发表评论。您应该使用Google地图框架地图GMSMapView的实例来使用这些功能。

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

您已经有一个名称为“mapView”的变量 - 您是否包含GoogleMaps的pod? – derdida

+0

当您尝试将MKMapView(Apple)和GMSMapView(Google) – derdida

+0

的内容组合起来时,您应该小心,我已添加@IBOutlet弱变量mapView:GMSMapView! –

相关问题