我正在用餐厅,酒吧,自助餐厅制作Places应用程序。 我希望该应用找到用户的当前位置,并在Google地图的手机上的浏览器中显示从该位置到所需位置的路线。
答案 0 :(得分:0)
#1导入库
import CoreLocation
import GoogleMaps
CLLocationManagerDelegate
var locationManager = CLLocationManager()
var placePicker: GMSPlacePicker!
var lat: Double!
var long: Double!
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方法中调用
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()
}
****Dont Forget to Requesting authorization to use location services info.plist****
@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
}