尝试向 Google 地图添加折线时遇到
EXC_BAD_ACCESS (code=1, address=0x8)
错误。
如果我注释掉该行
polyline.map = mapView
,一切都会正常工作。
鉴于错误的模糊性,我不确定根本原因,或者是否只是因为我运行应用程序的模拟器太慢。我还无法在真实设备上进行测试。
let path = GMSMutablePath()
for coordinate in route.polyline.geoJsonLinestring.coordinates {
path.add(CLLocationCoordinate2D(latitude: coordinate[1], longitude: coordinate[0]))
print(coordinate)
}
let bounds = GMSCoordinateBounds(path: path)
let cameraUpdate = GMSCameraUpdate.fit(bounds, withPadding: 16)
DispatchQueue.main.async { [self] in
let polyline = GMSPolyline(path: path)
polyline.strokeColor = UIColor(named: "AccentColor")!
polyline.strokeWidth = 5
polyline.map = mapView
mapView?.moveCamera(cameraUpdate)
}
崩溃不一定会立即发生 - 当我在添加折线后缩小地图时,这似乎是最常崩溃的时候。
更新:
我尝试更改
GPU Frame Capture
中的 Edit Scheme
设置,但没有任何影响。
我还创建了一个带有地图和硬编码的 2 坐标折线的简单视图,但它仍然崩溃。这就是字面上的意思:
override func viewDidLoad() {
super.viewDidLoad()
var coordinates = [
[-2.9287110999999997, 54.8929171],
[-2.8932368, 54.8968314],
]
let path = GMSMutablePath()
for coordinate in coordinates {
path.add(CLLocationCoordinate2D(latitude: coordinate[1], longitude: coordinate[0]))
}
let bounds = GMSCoordinateBounds(path: path)
let cameraUpdate = GMSCameraUpdate.fit(bounds, withPadding: 16)
let polyline = GMSPolyline(path: path)
polyline.strokeColor = UIColor(named: "AccentColor")!
polyline.strokeWidth = 10
polyline.map = mapView
mapView?.moveCamera(cameraUpdate)
}
地图完全加载后崩溃,大约需要一分钟。
谷歌地图在模拟器中的表现总体来说很糟糕。地图视图似乎总是需要 >1 分钟才能完全加载(带有 Google 徽标的灰色视图,然后图块最终出现)。
这似乎是一个错误,因为我可以使用
maps-sdk-for-ios-samples
演示应用程序重现它。我已在这里提交了错误报告:https://issuetracker.google.com/issues/323404687
您只是将折线设置在错误的位置。如果您在
viewDidLoad
处设置一条线,您的地图将不会出现,并且应用程序将会崩溃。您应该将其设置为 viewDidAppear
。
import UIKit
import GoogleMaps
import MapKit
class HomeViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
// MARK: - Variables
private let locationManager = CLLocationManager()
// MARK: - IBOutlets
@IBOutlet weak var mapView: GMSMapView!
// MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
MakeButtonStyle.defaultButtonStyle(button: createButton, title: "Create")
MakeButtonStyle.defaultButtonStyle(button: editButton, title: "Edit")
MakeButtonStyle.defaultButtonStyle(button: deleteButton, title: "Delete")
/* mapView and locationManager */
mapView.delegate = self
mapView.isMyLocationEnabled = true
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let coordinates = [
[-2.9287110999999997, 54.8929171],
[-2.8932368, 54.8968314],
]
let path = GMSMutablePath()
for coordinate in coordinates {
path.add(CLLocationCoordinate2D(latitude: coordinate[1], longitude: coordinate[0]))
}
let bounds = GMSCoordinateBounds(path: path)
let cameraUpdate = GMSCameraUpdate.fit(bounds, withPadding: 16)
let polyline = GMSPolyline(path: path)
polyline.strokeColor = UIColor.red
polyline.strokeWidth = 10
polyline.map = mapView
mapView?.moveCamera(cameraUpdate)
}
// MARK: - locationManager delegate
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
guard status == .authorizedWhenInUse else {
return
}
locationManager.startUpdatingLocation()
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first else {
return
}
let cameraPosition = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
mapView.camera = cameraPosition
locationManager.stopUpdatingLocation()
reverseGeocodeCoordinate(cameraPosition.target)
}
}
以下是我的 iPhone sim 的屏幕截图。