【问题标题】:It does not show the map (google maps) in my controller.它没有在我的控制器中显示地图(谷歌地图)。
【发布时间】:2018-01-16 15:10:04
【问题描述】:
import UIKit
import CoreLocation
import GoogleMaps

class CourseClass2: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate, CLLocationManagerDelegate {


@IBOutlet weak var containView: UIView!

let locManager=CLLocationManager()



 override func viewDidLoad() {
        super.viewDidLoad()


        self.locManager.delegate = self
        self.locManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locManager.requestWhenInUseAuthorization()
        self.locManager.startUpdatingLocation()
}



    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        self.showCurrentLocationOnMap()
        self.locManager.stopUpdatingLocation()
    }

    func showCurrentLocationOnMap() {

        let camera = GMSCameraPosition.camera(withLatitude: (self.locManager.location?.coordinate.latitude)!, longitude: (self.locManager.location?.coordinate.latitude)!, zoom: 140)

        let mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
        mapView.isMyLocationEnabled = true

        let marker = GMSMarker()
        marker.position = camera.target
        marker.snippet = "Current location"
        marker.appearAnimation = GMSMarkerAnimation.pop
        marker.map = mapView
        self.containView.addSubview(mapView)

    }

我在我的控制器中添加了这段代码,因为我试图在我当前的位置上显示带有标记的地图(谷歌地图)但没有工作(地图没有显示),我在我的代码中做错了什么?我该如何解决?

【问题讨论】:

  • 是否正在调用 locationManager 函数?你在模拟器上测试吗?
  • 我正在设备上测试
  • locationManager 被调用了吗?例如在函数上添加断点或更改函数以接受纬度和经度并从 viewdidload 调用它
  • FTF,当你第一次运行你的应用程序时,它肯定不会正常运行。您依次拥有self.locManager.requestWhenInUseAuthorization()self.locManager.startUpdatingLocation()。但是self.locManager.requestWhenInUseAuthorization()是一个异步函数,所以第一次用户还没有授权定位,所以self.locManager.startUpdatingLocation()不会执行。关于你的地图,我在你的代码上看不到任何地图参考,你是在 IB 上插入的吗?
  • @GIJOW 是的,也许这就是问题所在,我该如何修复它以执行 self.locManager.startUpdatingLocation() ?现在关于您的问题,我的代码就像在克劳迪奥的答案上一样,但它仍然不起作用

标签: ios swift google-maps swift4


【解决方案1】:

我建议你在界面生成器上设置containView 类型,像这样,还将名称更改为mapView(要清楚),注意GMSMapView 类型并将其链接到你的@987654327 @控制器:

(我不是Mac所以我用了这张图。图片来源:http://vikrambahl.com/google-maps-ios-xcode-storyboards/

并尝试使用以下代码,至少应该出现地图(没有标记)。

import UIKit
import CoreLocation
import GoogleMaps

class CourseClass2: UIViewController, UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mapView: GMSMapView!

let locManager=CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    self.locManager.delegate = self
    self.locManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locManager.requestWhenInUseAuthorization()
    self.locManager.startUpdatingLocation()

    mapView.isMyLocationEnabled = true
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    self.showCurrentLocationOnMap()
    self.locManager.stopUpdatingLocation()
}

func showCurrentLocationOnMap() {
    let camera = GMSCameraPosition.camera(withLatitude: (self.locManager.location?.coordinate.latitude)!, longitude: (self.locManager.location?.coordinate.latitude)!, zoom: 140)

    let marker = GMSMarker()
    marker.position = camera.target
    marker.snippet = "Current location"
    marker.appearAnimation = GMSMarkerAnimation.pop
    marker.map = mapView
}

还要小心强制解包选项(self.locManager.location?.coordinate.latitude)!,因为它可能会导致致命错误

【讨论】:

  • 这仅显示地图但所有代码仍然不起作用:(
  • @fisherM 这可能是因为func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 没有被系统调用/触发,这意味着标记不会显示
  • 知道如何修复它吗?
  • @fisherM 你在 info.plist 中添加权限了吗?
  • @KhawarIslam 是的,我添加了
【解决方案2】:

Xcode 9.2 和 Swift 4

转到您的 info.plist 文件并根据您的代码添加以下键。

Privacy - Location When In Use Usage Description

移动到您的视图控制器并用此代码替换 showCurrentLocationOnMap() 函数。

 func showCurrentLocationOnMap() {

        let camera = GMSCameraPosition.camera(withLatitude: (self.locManager.location?.coordinate.latitude)!, longitude: (self.locManager.location?.coordinate.latitude)!, zoom: 10.0)

        let mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
        mapView.isMyLocationEnabled = true

        let marker = GMSMarker()
        marker.position = camera.target
        marker.snippet = "Current location"
        marker.appearAnimation = GMSMarkerAnimation.pop
        marker.map = mapView
        self.view = mapView

    }

在模拟器中运行代码Debug->Location->Apple选择或Apple Device。我的模拟器图像和你的代码。

【讨论】:

    猜你喜欢
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2014-12-27
    • 1970-01-01
    • 2020-04-03
    相关资源
    最近更新 更多