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

Posted

技术标签:

【中文标题】谷歌地图 - 如何找出用户的位置并在 Swift 中显示从该位置到目的地点的路线?【英文标题】:Google maps - how to find out user's location and show the route from that location to the destination point in Swift? 【发布时间】:2018-07-13 14:45:24 【问题描述】:

我正在制作带有餐厅、酒吧、自助餐厅的场所应用程序。 我希望该应用找到用户当前位置,并在谷歌地图的手机浏览器中显示从该位置到所需位置的路线。

【问题讨论】:

请展示您的尝试。 【参考方案1】:

#1 导入库

import CoreLocation
import GoogleMaps 

2 设置委托

CLLocationManagerDelegate

3 个变量

var locationManager = CLLocationManager()
var placePicker: GMSPlacePicker!
var lat: Double!
var long: Double!

4 为 CLLoactionManager 定义函数

func setUpCLLocationManager() 
            // cllocation mananger
        self.locationManager = CLLocationManager()

        self.locationManager.delegate = self

        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

        self.locationManager.requestWhenInUseAuthorization()

        self.locationManager.startUpdatingLocation()

        self.locationManager.distanceFilter = 1000

        if (CLLocationManager.locationServicesEnabled())

        

            self.locationManager.delegate = self

            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

            if ((UIDevice.current.systemVersion as NSString).floatValue >= 8)

            

                self.locationManager.requestWhenInUseAuthorization()

            



            self.locationManager.startUpdatingLocation()

        

        else

        
            self.showalertview(messagestring: "Location services are not enabled")
            #if debug

                println("Location services are not enabled");

            #endif

        
    

并在 ViewDidLoad 方法中调用它

5 调用委托方法

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
    let locValue:CLLocationCoordinate2D = manager.location!.coordinate
    long = (locValue.longitude)
    lat = (locValue.latitude)

    print (long , lat)

let coordinates = CLLocationCoordinate2DMake(self.latitude, self.longitude)
    let marker = GMSMarker(position: coordinates)
    marker.title = "I am here"
    marker.map = self.googleMapView
    self.googleMapView.animateToLocation(coordinates)







func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) 
    if status == .authorizedWhenInUse 
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
        let locValue: CLLocationCoordinate2D = (manager.location?.coordinate)!
        print("locations = \(locValue.latitude) \(locValue.longitude)")
        long = Float(locValue.longitude)
        lat = Float(locValue.latitude)
    //if authorized
//lo






func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) 
    print(error.localizedDescription)
    self.locationManager.stopUpdatingLocation()

6

****Dont Forget to Requesting authorization to use location services info.plist****

7 GMS 地点选择器

    @IBAction func pickPlace(sender: UIButton) 
    let northEast = CLLocationCoordinate2DMake(VIEWPORT_LATLNG.latitude + VIEWPORT_DELTA, VIEWPORT_LATLNG.longitude + VIEWPORT_DELTA)
    let southWest = CLLocationCoordinate2DMake(VIEWPORT_LATLNG.latitude - VIEWPORT_DELTA, VIEWPORT_LATLNG.longitude - VIEWPORT_DELTA)
    let viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
    let config = GMSPlacePickerConfig(viewport: viewport)
    placePicker = GMSPlacePicker(config: config)

    placePicker?.pickPlaceWithCallback( (place: GMSPlace?,error: NSError?) -> Void in


      if error != nil 
        let error = error?.localizedDescription
       print(error)
        return
      

      if place != nil 
        let coordinates = CLLocationCoordinate2DMake(place.coordinate.latitude, place.coordinate.longitude)
                let marker = GMSMarker(position: coordinates)
                marker.title = place.name
                marker.map = self.googleMapView
                self.googleMapView.animateToLocation(coordinates)
       else 
        print("No place selected")
      
    )
  

#8 create path/route

func drawRectange()  
   /* create the path */     
let path = GMSMutablePath()     
path.add(CLLocationCoordinate2D(latitude: lat, longitude: long)     path.add(CLLocationCoordinate2D(coordinates) 
/* show what you have drawn */     
let rectangle = GMSPolyline(path: path)     
rectangle.map = mapView 

【讨论】:

以上是关于谷歌地图 - 如何找出用户的位置并在 Swift 中显示从该位置到目的地点的路线?的主要内容,如果未能解决你的问题,请参考以下文章

如何保存多个谷歌地图标记,它们在 Firebase 实时数据库中的位置,并在打开应用程序时再次显示它们

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

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

获取用户国家和城市名称并在谷歌地图上定位

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

用户添加放置图钉,谷歌地图