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

时间:2018-07-13 14:45:25

标签: ios swift google-maps geolocation

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

1 个答案:

答案 0 :(得分:0)

#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 
}