Google 映射奇怪的行为 swift 4 ios
Google Maps strange behavior swift 4 ios
更新:跟语言有关系吗?因为我的应用支持英语和阿拉伯语。
大家好,我几天以来一直在为此苦苦挣扎,我已经尝试了所有方法,但找不到任何解决方案,问题是当我 运行 通过 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())")
}
}
要在自定义视图中显示地图,您必须这样做,
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 中
好吧,在苦苦挣扎了很多天并尝试了所有可能的愚蠢方法来修复它之后,我终于找到了解决方法。主要问题是如果您的应用程序支持多种语言,那么 google 地图将显示这种行为。以下是修复它的步骤。
第 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)
}
更新:跟语言有关系吗?因为我的应用支持英语和阿拉伯语。
大家好,我几天以来一直在为此苦苦挣扎,我已经尝试了所有方法,但找不到任何解决方案,问题是当我 运行 通过 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())")
}
}
要在自定义视图中显示地图,您必须这样做,
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 中
好吧,在苦苦挣扎了很多天并尝试了所有可能的愚蠢方法来修复它之后,我终于找到了解决方法。主要问题是如果您的应用程序支持多种语言,那么 google 地图将显示这种行为。以下是修复它的步骤。
第 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)
}