使用swift在谷歌地图中的当前位置

问题描述 投票:0回答:5

我正在尝试在谷歌地图上显示用户的当前位置,但在下面的情况下,地图甚至不会显示。我该怎么改变来解决这个问题?

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()
}
ios swift google-maps google-maps-sdk-ios currentlocation
5个回答
3
投票

你可以尝试这个波纹管代码工作正常

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()
}

1
投票

确实要求在信息Plist上设置,然后尝试这个

@IBOutlet weak var your "name of view which show map": GMSMapView!

override func viewDidLoad(){
        super.viewDidLoad()

  placesClient = GMSPlacesClient.shared()
  locationManager.requestAlwaysAuthorization()
     if CLLocationManager.locationServicesEnabled(){     
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
        locationManager.distanceFilter = 500
        locationManager.requestWhenInUseAuthorization()
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()  
  }   
    mapView.settings.myLocationButton = true
    mapView.settings.zoomGestures = true
    mapView.animate(toViewingAngle: 45)
    mapView.delegate = self  }

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
     let newLocation = locations.last // find your device location
     mapView.camera = GMSCameraPosition.camera(withTarget: newLocation!.coordinate, zoom: 14.0) // show your device location on map
     mapView.settings.myLocationButton = true // show current location button
     var lat = (newLocation?.coordinate.latitude)! // get current location latitude
     var long = (newLocation?.coordinate.longitude)! //get current location longitude

}

0
投票

问题是您正在将mapView的框架设置为CGRectZero。这会导致地图的高度为零,零宽度,难怪它不显示!

尝试将其设置为CGRectMake(0,0,200,200),例如,这将为您提供屏幕左上角的地图,大小为200 x 200。

我之前从未使用过Swift,因此CGRectMake()的语法可能略有不同


0
投票

看来你的地图创建不在你的viewDidLoad函数中。您可能想尝试移动那里,看看会发生什么。


0
投票

将适当的属性添加到info.plist中。

您应该确保在信息属性列表中具有locationalwaysusagedescriptionwheninuseusagedescription的NS属性。这允许询问当前位置的权限。

© www.soinside.com 2019 - 2024. All rights reserved.