谷歌地图中的当前位置与swift

Posted

技术标签:

【中文标题】谷歌地图中的当前位置与swift【英文标题】:Current Location in Google Maps with swift 【发布时间】:2016-04-29 14:45:15 【问题描述】:

我试图在谷歌地图上显示用户的当前位置,但在以下情况下,地图甚至没有显示。我应该改变什么来解决这个问题?

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

【问题讨论】:

试试 self.view.addsubview(mapView) 不,不幸的是,这不起作用@John D 你能把你的代码作为对象 c 给我们吗 【参考方案1】:

你可以试试下面的代码,它工作正常

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

【讨论】:

【参考方案2】:

在infoPlist上做requier设置然后试试这个

@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


【讨论】:

【参考方案3】:

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

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

我以前从未使用过 Swift,所以CGRectMake()的语法可能有点不同

【讨论】:

【参考方案4】:

您创建的地图似乎不在您的viewDidLoad 函数中。您可能想尝试将其移到那里,看看会发生什么。

【讨论】:

【参考方案5】:

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

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

【讨论】:

【参考方案6】:
import UIKit

导入谷歌地图 导入 GooglePlaces 导入核心位置

类 MapsViewController:UIViewController、CLLocationManagerDelegate、GMSMapViewDelegate

var mapView = GMSMapView()
var locationManager = CLLocationManager()
let marker = GMSMarker()

override func viewDidLoad()
    super.viewDidLoad()
    
    mapView.frame = self.view.bounds
    self.view.addSubview(mapView)
    
    locationManager.requestAlwaysAuthorization()
    if CLLocationManager.locationServicesEnabled()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = 10
        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
    let lat = (newLocation?.coordinate.latitude)! // get current location latitude
    let long = (newLocation?.coordinate.longitude)! //get current location longitude
    
    marker.position = CLLocationCoordinate2DMake(lat,long)
    marker.map = mapView
    print("Current Lat Long - " ,lat, long )


func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) 
    mapView.clear()
    DispatchQueue.main.async 
        let position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
        self.marker.position = position
        self.marker.map = mapView
        self.marker.icon = UIImage(named: "default_marker")
        print("New Marker Lat Long - ",coordinate.latitude, coordinate.longitude)
    

【讨论】:

以上是关于谷歌地图中的当前位置与swift的主要内容,如果未能解决你的问题,请参考以下文章

谷歌地图 - 如何找出用户的位置并在 Swift 中显示从该位置到目的地点的路线?

访问谷歌地图中的当前位置

swift - 如何在应用程序运行时顺利加载谷歌地图视图

如何使用 swift 在谷歌地图中更改我的位置按钮的位置

谷歌地图中的当前位置并将接收到的位置存储在 TextView 上

如何在 swift/iOS 中更新谷歌地图标记位置