更新:它与语言有关系吗?因为我的应用使用英语和阿拉伯语。
大家好,我几天以来一直在为此苦苦挣扎,我已经尝试了一切,但找不到任何解决方案,问题是,当我通过xCode运行应用程序时,一切正常,如以下屏幕截图所示。
然后,当我拔下设备并重新打开应用程序后,它停止工作,该应用程序运行良好,但是我没有看到任何地图,只有标记如以下屏幕截图所示。
任何帮助将不胜感激。
AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GMSServices.provideAPIKey(googleApiKey)
GMSPlacesClient.provideAPIKey(googleApiKey)
return true
}
ViewController
//Step 1
@IBOutlet weak var mapview: GMSMapView!
//Step 2
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.mapview.settings.scrollGestures = true
self.mapview.settings.zoomGestures = true
self.mapview.isMyLocationEnabled = true
self.mapview.settings.myLocationButton = true
self.mapview.delegate = self
self.mapview.layoutIfNeeded()
}
}
//Step 3 After we have the current location items are loaded from server by user location and the markers are added in GetBusinesses function
extension ItemsList: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
guard status == .authorizedWhenInUse else {
return
}
self.locationManager.startUpdatingLocation()
self.mapview.camera = GMSCameraPosition(target: (self.locationManager.location?.coordinate)!, zoom: 14, bearing: 0, viewingAngle: 0)
self.GetBusinesses(data: "&isOpen=2&cat=\(Prefrences.getSelectedCategoryId())")
}
}
答案 0 :(得分:1)
要在自定义视图中显示地图,您必须这样做
let camera = GMSCameraPosition.camera(withLatitude: lat , longitude: long, zoom: 15.0)
let mapView = GMSMapView.map(withFrame: CGRect(x: 2, y: 2, width: self.MapView.frame.width - 4, height: self.MapView.frame.height - 2), camera: camera)
self.MapView.addSubview(mapView)
self.MapView.clipsToBounds = true
self.MapView.contentMode = .center
您需要在情节提要框架上使用创建的mapView创建一个mapView并将其作为子视图添加到您创建的主MapView中
答案 1 :(得分:0)
好了,因此,在挣扎了许多天并尝试了所有愚蠢的方法来解决它之后,我终于找到了解决方案。主要问题是,如果您的应用程序支持多种语言,则Google Map将显示这种行为。以下是修复它的步骤。
步骤1: 如果您的viewController中有GMSMapView视图,请断开GMSMapView的出口,因为它将动态创建 。
步骤2: 根据用户设置设置应用程序语言。
第3步: 创建自定义GMSMapView对象集委托方法和其他必要的设置,并将其添加到其容器中。
就是这样,您很好。据我了解,您必须在创建GMSMapView之前先设置应用程序语言(我也可能是错的)。如果有人知道为什么这种方法有效,请解释一下。
//Our Custom MapView Var
var mapview: GMSMapView!
//View viewDidLoad() function
override func viewDidLoad() {
super.viewDidLoad()
/*
TODO:
Get selected language from preferences and set accordingly.
*/
UserDefaults.standard.set(["en"], forKey: "AppleLanguages")
UserDefaults.standard.synchronize()
let camera = GMSCameraPosition.camera(withLatitude: 55.277397,
longitude: 25.199514,
zoom:12)
self.mapview = GMSMapView.map(withFrame: self.mMapContainer.bounds, camera: camera)
self.mapview.delegate = self
self.mapview.isMyLocationEnabled = true
self.mapview.settings.myLocationButton = true
self.mapview.isMyLocationEnabled = true
self.mapview.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mMapContainer.addSubview(self.mapview)
}