【问题标题】:Swift, take places near user positionSwift,发生在用户位置附近
【发布时间】:2017-09-25 10:04:41
【问题描述】:

我是开发的初学者,我在我的应用程序视图上添加谷歌地图,按照教程我使用了这个代码

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            let location: CLLocation = locations.last!
            print("Location: \(location)")

            let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude,
                                                  longitude: location.coordinate.longitude,
                                                  zoom: zoomlevel)


            let marker = GMSMarker()
            marker.title = "My Position"

            marker.map = mapView
            if mapView.isHidden {
                mapView.isHidden = false
                mapView.camera = camera
            } else {
                mapView.animate(to: camera)
            }


        }


        func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
            switch status {
            case .restricted:
                print("Location access was restricted.")
            case .denied:
                print("User denied access to location.")

                mapView.isHidden = false
            case .notDetermined:
                print("Location status not determined.")
            case .authorizedAlways: fallthrough
            case .authorizedWhenInUse:
                print("Location status is OK.")
            }
        }


        func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
            locationManager.stopUpdatingLocation()
            print("Error: \(error)")
        }

要查找用户位置,现在我想在地图上查看距离用户位置 X 距离的地点(谷歌地点),我该怎么做?我在 google 地方文档上搜索但没有找到有效的答案

【问题讨论】:

标签: ios swift xcode google-maps


【解决方案1】:

在 mapView 上添加另一个标记 onTap 并使其可拖动并使用 CLLocationDistance 测量 2 个位置之间的距离。

let distanceInMeters = self.locationManager.location?.distance(from: newMarkerLocation!)

【讨论】:

    猜你喜欢
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 2020-11-13
    • 2021-10-01
    • 1970-01-01
    • 2019-10-30
    相关资源
    最近更新 更多