当前位置不工作

Current Location not working

我对 iOS 开发和使用 google 地图 sdk 都很陌生。我正在为我当前的项目使用 Xcode。我在适当的地方添加了我的 API 键。

到目前为止我有这个:

导入 UIKit 导入谷歌地图 导入 GooglePlaces

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func loadView() {
    let camera = GMSCameraPosition.cameraWithLatitude(36.2108, longitude: -81.6774, zoom: 15.0)
    let mapView = GMSMapView.mapWithFrame(.zero, camera: camera)
    mapView.myLocationEnabled = true
    if let mylocation = mapView.myLocation {
        print("User's location: \(mylocation)")
    } else {
        print("User's location is unknown")
    }
    self.view = mapView
    mapView.mapType = kGMSTypeTerrain
    mapView.indoorEnabled = true
    let mapInsets = UIEdgeInsets(top: 100.0, left: 0.0, bottom: 0.0, right: 0.0)
    mapView.padding = mapInsets

    //Creates a marker in the center of the map.
    //let marker = GMSMarker()
    //marker.position = CLLocationCoordinate2D(latitude: 36.2108, longitude: -81.6774)
    //marker.title = "ASU"
    //marker.snippet = "Boone, NC"
    //marker.map = mapView
    }

}

我的问题是,当我 运行 使用模拟器的应用程序时,它不会在任何地方显示我当前的 gps 位置。它确实成功地将我带到了指定的坐标,它正确缩放,并将地图显示为排版地图。

所以我的问题:

我做错了什么导致我的当前位置不显示? (它从不要求获得许可)

如何让我的地图出现在背景中?就像我在顶部添加了填充,这样我就可以通过故事板添加一个按钮,但是按钮没有显示出来。

谢谢大家!

模拟器上的位置并不总是有效,您可以:

  1. 尝试通过 Simulator > Reset content and settings 重置模拟器的内容
  2. 更换模拟器
  3. 确保您已在模拟器模拟器 > 调试 > 位置 > 自定义位置中添加了自定义纬度和经度

mapView 中没有填充的问题是因为您将 mapView 设置为显示为主视图 (self.view = mapView) 并且由于该地图覆盖了整个区域屏幕。

尝试从主视图添加一个带填充的子视图,然后将地图视图分配给您的子视图。像这样-

let subView = UIView(frame: CGRect(x: 0, y: 100, width: self.view.frame.width, height: self.view.frame.height-100))

subView = mapView

self.view.addSubview(subView)

另外,对于位置问题,请尝试为您的 xcode 设置一个默认位置。有关如何在运行时设置位置的详细信息,请参阅此内容。

https://developer.apple.com/library/mac/recipes/xcode_help-scheme_editor/Articles/simulating_location_on_run.html