【问题标题】:Current location is not shown perfectly in swift 3当前位置在 swift 3 中没有完美显示
【发布时间】:2017-12-11 06:22:33
【问题描述】:

我有一个 GMSMapView,想在GMSMapView 上显示当前位置。我在 info.plist 源代码中添加了以下行 -

<key>NSLocationAlwaysUsageDescription</key>
<string>Result</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Result</string>

我的模拟位置是来自 xcode 的美国加利福尼亚州旧金山。 Simulated location

当我加载GMSMapView 时,我会收到位置警报。 Location Alert

但当前位置始终显示旧金山和我 iPhone 上的所有地图相关应用程序的当前位置更改始终是旧金山,尽管现在我的手机位置应该显示孟加拉国达卡。 解决办法是什么???
我想通过移动 GPS 获取当前位置。

【问题讨论】:

  • 您是在模拟器上还是在实际设备上进行测试?如果模拟器看起来像您将位置添加为 San Fransisco 因此位置指向 San Fransisco 在真实设备上测试它应该没问题
  • 尝试添加一个gpx文件@Ahnaf

标签: ios swift gps gmsmapview currentlocation


【解决方案1】:
Step 1: Import the this into your controller class 

    import CoreLocation

Step 2: Add this delegate to your class file

    class Your_controller: CLLocationManagerDelegate

Step 3: Declare this above for viewed load

    var locationManager = CLLocationManager()

Step 4: Add this code to your `viewdidload` method

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()


    if CLLocationManager.locationServicesEnabled() {
      switch (CLLocationManager.authorizationStatus()) {
        case .notDetermined, .restricted, .denied:
          print("No access")
        case .authorizedAlways, .authorizedWhenInUse:
          print("Access")
      }
    } else {
      print("Location services are not enabled")
    }

Step 5: Add this code below viewdidload method

    func locationManager(_ manager: CLLocationManager,
      didUpdateLocations locations: [CLLocation]) {

      let locationArray = locations as NSArray
      let locationObj = locationArray.lastObject as !CLLocation
      let coord = locationObj.coordinate

      lattitude = coord.latitude
      longitude = coord.longitude
      print(lattitude)
      print(longitude)
    }

Step 6: add to permission your plist file

Key - "Privacy - Location When In Use Usage Description" 

Value - "This app needs access to your location"

Step 7: run your Application on Hardware Device

【讨论】:

  • 谢谢,只是一个小问题 let locationObj = locationArray.lastObject as !CLLocation 应该让 locationObj = locationArray.lastObject as! CL位置
  • 谢谢拉维,+1。它真的很棒:) 只是想在 location 方法中添加一些行来添加显示当前位置的标记。 let center = CLLocationCoordinate2D(latitude: locationObj.coordinate.latitude, longitude: locationObj.coordinate.longitude) let marker = GMSMarker() marker.position = center marker.title = "当前位置" marker.map = mapView let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: lattitude, longitude: longitude, zoom: 15) self.mapView.animate(to: camera) locationManager.stopUpdatingLocation()
【解决方案2】:

在您的 viewDidLoad() 中为位置管理器配置添加以下内容:

override func viewDidLoad() {
    super.viewDidLoad()

    if (CLLocationManager.locationServicesEnabled())
    {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }
}

进一步实现 GMSMapView 的委托:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    let location = locations.last as CLLocation

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)        
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    self.map.setRegion(region, animated: true)
}

【讨论】:

  • 我做到了兄弟,但纬度和经度总是旧金山
  • 是模拟器的吗?如果我使用 apk 安装应用程序,那么它会完美吗?
猜你喜欢
  • 1970-01-01
  • 2014-10-16
  • 2019-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-02
  • 1970-01-01
  • 2018-02-11
相关资源
最近更新 更多