【发布时间】:2018-01-19 09:13:44
【问题描述】:
我正在构建一个 ios 应用程序,它使用谷歌地图来显示服务器中返回的地点的位置。我设法在地图的初始加载中显示结果,但是当我想搜索新的地方并重新加载地图中的标记时。不幸的是,该应用程序崩溃了。
希望得到您的帮助。
谢谢。
这是我在 ViewController 中最初显示标记的代码:
func setUpMap() {
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 10.0)
self.mapView.camera = camera
let map_view = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
for data in arrData {
let long = data["LONGITUDE"].string!
let lat = data["LATITUDE"].string!
if long != "" || lat != "" {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat)!, longitude: CLLocationDegrees(long)!)
marker.title = ""
marker.snippet = "Hey, this is sample"
marker.icon = UIImage(named: "locator")
marker.map = self.mapView
bounds = bounds.includingCoordinate(marker.position)
}
}
// fit to bounds
mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))
self.mapView = map_view
}
这是我重新加载/重置地图中标记的代码。
func reloadMarkers() {
self.mapView.clear()
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 10.0)
self.mapView.camera = camera
let map_view = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
var _bounds = GMSCoordinateBounds()
for data in arrData {
let long = data["LONGITUDE"].string!
let lat = data["LATITUDE"].string!
if long != "" || lat != "" {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat)!, longitude: CLLocationDegrees(long)!)
marker.title = ""
marker.snippet = "Hey, this is sample"
marker.icon = UIImage(named: "locator")
marker.map = self.mapView
_bounds = _bounds.includingCoordinate(marker.position)
}
}
// fit to bounds
mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))
self.mapView = map_view
}
感谢您的帮助。 干杯!
【问题讨论】:
-
你能发布你的崩溃日志吗?
-
当我在我的 reloadMarkers 方法中设置 GMSCameraPosition 时,它只是在行中显示“错误:在展开可选值时意外发现 nil”
-
然后你找到了答案,你的问题是
lat或long不能转换为CLLocationDegrees请打印什么经纬度包含 -
你好,它包含有效的坐标,我还添加了一些检查来检查纬度或经度是否为空。
-
你能把你的应用程序崩溃的那一行贴出来吗?
标签: ios google-maps google-maps-api-3 swift3