我希望有人能帮我找出这个问题,我从昨天开始就有这个问题,一直在寻找可能是哪里出了问题,也许我漏掉了什么?我是 iOS 和 Swift 以及整个 Mac 生态系统的新手(使用 Mac 大约 2 个月)。
问题是我正在迁移到 native iOS Phonegap 应用程序,它依赖于很多标记(大约 300 到 400,并且滞后很多),其中一些可以被用户(按组)禁用,删除标记时会出现问题,其中一些标记仍然像幽灵一样,它们的 map 属性设置为 nil,并且没有触发触摸事件(我已将它们设置为执行 segue)
这是我用来存储对象然后删除它们的代码,目前我正在使用带有数组的字典来确定要删除的标记。
我翻译了这些评论,因为它们是西类牙语,所以您可以在短期内了解我在做什么(或试图做什么)。
这是我添加标记的代码,我也知道可能有一些更好的方法来做一些事情,比如
//parses some data, and retrieves some more to create the markers
func procesaMarcadores(retorno: [String:Any]) {
//skiped a lot of previous code
if let servicios = retorno["servicios"] as? NSArray {
//a simple cycle to iterate through data recieved
for item in servicios {
let items = item as! NSDictionary
//lazy, easy, and dirty way to retrieve latitude and longitude, must change
let latitud = (items["direccion_georeferenciada_latitud"] as! NSString).doubleValue
let longitud = (items["direccion_georeferenciada_longitud"] as! NSString).doubleValue
let foto = self.urlFotos + String((items["foto"] as! NSString))
//new marker
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: latitud, longitude: longitud)
marker.userData = foto //a simple string with a url to use latter in the next view
//setting mapView in main thread just for safety?, don't wanna mess something
DispatchQueue.main.async(){
marker.map = self.mapView
}
marker.icon = imagenChica //custom marker image
//storing the objects in dictionary
let tipo = items["id_tipo_servicio"] as! Int
var arregloServicios = dicServicios[tipo] ?? []
arregloServicios.append(marker)
dicServicios[tipo] = arregloServicios
}
}
}
这里我删除了标记
//updates markers, gets new data for markers, and deletes markers
func actualizaMarcadores(marcadores: [Any]?, seleccionado: Int, perfil: Int?){
//lots of deleted code
//deletes markers group if variable noSale is set to delete markers of the group of the variable seleccionado
if noSale {
//asynchronous delete just to be safe?
DispatchQueue.main.async {
//whiping markers from mapView
var arregloServicios = self.dicServicios[seleccionado]
for i in arregloServicios! {
i.map = nil
}
//releasing a bit of memory
self.dicServicios[seleccionado] = nil
//already tried not deleting the marker objects
}
}
}
我已经用谷歌搜索了答案,我一直在研究但没有发现任何相关的东西,除了几年前,在一个旧的谷歌地图 api 中有一个错误,但它是固定的,也在文档中搜索但唯一的事情我发现删除标记的另一种方法是使用 mapView.clear()
但随后我将不得不重新分配所有标记并且 cpu 达到大约 60%,这比大约 5% cpu 更糟糕使用一些编码不佳的方法将字符串转换为 int。
最佳答案
这里我只给你一个概述,你可以用你现有的代码交叉检查它
import GoogleMaps
var arrayMarkerObj = [String:GMSMarker]() //Use dictionary
var driverMarker:GMSMarker! //Define it as gloabal
class markerController:UIViewController{
func procesaMarcadores(retorno: [String:Any]) {
//skiped a lot of previous code
if let servicios = retorno["servicios"] as? NSArray {
//a simple cycle to iterate through data recieved
for item in servicios {
let items = item as! NSDictionary
//lazy, easy, and dirty way to retrieve latitude and longitude, must change
let latitud = (items["direccion_georeferenciada_latitud"] as! NSString).doubleValue
let longitud = (items["direccion_georeferenciada_longitud"] as! NSString).doubleValue
let foto = self.urlFotos + String((items["foto"] as! NSString))
//new marker
let positionDriver = CLLocationCoordinate2DMake(latitude: latitud, longitude: longitud)
var marker:GMSMarker! = GMSMarker(position: positionDriver)
marker.userData = foto //a simple string with a url to use latter in the next view
//setting mapView in main thread just for safety?, don't wanna mess something
// DispatchQueue.main.async(){
// marker.map = self.mapView
// }
marker.icon = imagenChica //custom marker image
marker.map = self.mapView
self.arrayMarkerObj.updateValue(marker, forKey: key) //Put some uniqe KEY which define marker location
self.animateMarker(objMarker: marker, locations: positionDriver)
//storing the objects in dictionary
// let tipo = items["id_tipo_servicio"] as! Int
// var arregloServicios = dicServicios[tipo] ?? []
// arregloServicios.append(marker)
// dicServicios[tipo] = arregloServicios
}
}
}
func animateMarker(objMarker:GMSMarker,locations:CLLocationCoordinate2D){
CATransaction.begin()
CATransaction.setAnimationDuration(2.0)
objMarker.position = locations
CATransaction.commit()
}
//Delete Marker
func deleteMark(){
for(key, value) in self.arrayMarkerObj{
self.driverMarker = self.arrayMarkerObj[key!]
if(self.driverMarker != nil){
self.driverMarker.map = nil
self.arrayMarkerObj.removeValue(forKey: key!)
}
}
}
}
关于ios - 使用 iOS 和 Swift 的谷歌地图中的一些标记不会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44687077/