![](/img/trans.png)
[英]How to find out that whether a point on a map is within a radius from user's present location on iOS?
[英]Google maps - how to find out user's location and show the route from that location to the destination point in Swift?
我正在制作帶有餐廳、酒吧、自助餐廳的場所應用程序。 我希望該應用程序可以找到用戶當前位置,並在手機瀏覽器的谷歌地圖中顯示從該位置到所需地點的路線。
#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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.