我的当前位置显示不同的位置,谷歌地图 - Xcode

时间:2020-12-28 11:41:10

标签: ios swift xcode google-maps swiftui

我已经实现了 Google Maps,但我的 Current Location 始终是 USA。我正在 REAL device 上对其进行测试,并且在 location permissions 中添加了 info.plistGoogle API key 也在 didFinishLaunchingWithOptionsAppDelegate 方法中实现。

<key>NSLocationWhenInUseUsageDescription</key>
    <string>By accessing your location, this app can help you navigate between different locations.</string>

这是我的代码。我正在关注 raywenderlich.com 的本教程。 https://www.raywenderlich.com/7363101-google-maps-ios-sdk-tutorial-getting-started 我已经下载了本教程的材料,使用了我的 API 密钥,但我当前的位置仍然是美国。在多台真实设备上测试。

import UIKit
import GoogleMaps
import CoreLocation

class MapViewController: UIViewController {
@IBOutlet weak var mapView : GMSMapView!
let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    if CLLocationManager.locationServicesEnabled() {
      locationManager.requestWhenInUseAuthorization()
      locationManager.desiredAccuracy = kCLLocationAccuracyBest
      locationManager.startUpdatingLocation()
      locationManager.delegate = self
      self.mapView.delegate = self
    } else {
      locationManager.requestWhenInUseAuthorization()
    }
  }
}

CLLocationManagerDelegate:-

extension MapViewController: CLLocationManagerDelegate {
  func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    guard status == .authorizedWhenInUse else { return }
    locationManager.startUpdatingLocation()
    self.mapView.isMyLocationEnabled = true
    self.mapView.settings.myLocationButton = true
  }
  
  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location = locations.first else { return }
   
  // the coordinates i get here are always from some city in USA.
 mapView.camera = GMSCameraPosition(
      target: location.coordinate,
      zoom: 15,
      bearing: 0,
      viewingAngle: 0)
    }
  
  func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    print("Error: \(error)")
  }
  
}

1 个答案:

答案 0 :(得分:1)

请检查下面的设置作为屏幕截图:

enter image description here