Google地图中的当前位置
我试图在Google地图上显示用户的当前位置,但在下面的情况下,地图甚至不显示。 我应该改变什么来解决这个问题?
var locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. //user location stuff locationManager.delegate = self locationManager.requestWhenInUseAuthorization() locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.startUpdatingLocation() } func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { print("Error" + error.description) } func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let userLocation = locations.last let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude) let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude, zoom: 8) let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera) mapView.myLocationEnabled = true self.view = mapView let marker = GMSMarker() marker.position = center marker.title = "Current Location" marker.snippet = "XXX" marker.map = mapView locationManager.stopUpdatingLocation() }
你可以试试这个波纹pipe的代码工作正常
import UIKit import GoogleMaps import GooglePlaces class SearchMapsViewController: UIViewController, UINavigationBarDelegate, GMSAutocompleteFetcherDelegate, LocateOnTheMap, UISearchBarDelegate, CLLocationManagerDelegate { @IBOutlet var googleMapsContainerView: UIView! var searchResultController: SearchResultsController! var resultsArray = [String]() var googleMapsView:GMSMapView! var gmsFetcher: GMSAutocompleteFetcher! var locationManager = CLLocationManager() override func viewDidAppear(animated: Bool) { locationManager.delegate = self locationManager.requestWhenInUseAuthorization() locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.startUpdatingLocation() self.googleMapsView = GMSMapView (frame: self.googleMapsContainerView.frame) self.googleMapsView.settings.compassButton = true self.googleMapsView.myLocationEnabled = true self.googleMapsView.settings.myLocationButton = true self.view.addSubview(self.googleMapsView) searchResultController = SearchResultsController() searchResultController.delegate = self gmsFetcher = GMSAutocompleteFetcher() gmsFetcher.delegate = self } func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { print("Error" + error.description) } func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let userLocation = locations.last let center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude) let camera = GMSCameraPosition.cameraWithLatitude(userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude, zoom: 15); self.googleMapsView.camera = camera self.googleMapsView.myLocationEnabled = true let marker = GMSMarker(position: center) print("Latitude :- \(userLocation!.coordinate.latitude)") print("Longitude :-\(userLocation!.coordinate.longitude)") marker.map = self.googleMapsView marker.title = "Current Location" locationManager.stopUpdatingLocation() }
问题是你正在设置CGView的框架CGRectZero 。 这导致地图具有零高度和零宽度,难怪它不显示!
尝试将其设置为CGRectMake(0,0,200,200) ,例如,这会给你一个在屏幕左上angular的地图,大小为200 x 200。
我从来没有用过Swift,所以CGRectMake()
的语法可能有点不同
看来你的地图的创build不在你的viewDidLoad
函数中。 你可能想尝试移动那里看看会发生什么。
将相应的属性添加到info.plist
。
您应该确保您具有locationalwaysusagedescription
的NS属性以及wheninuseusagedescription
在信息属性列表中使用描述。 这允许询问当前位置的许可。