简体   繁体   English

有些标记不会在使用iOS和Swift的谷歌地图中消失

[英]Some markers wont go away in Google Maps using iOS and Swift

I hope someone can help me find this out, I have this problem since yesterday and have been searching what could be wrong, maybe I missed something? 我希望有人可以帮我找到这个,我从昨天开始就遇到这个问题并一直在寻找可能出错的地方,也许我错过了什么? I'm new (about 2 months using a mac) to iOS and Swift and the whole mac ecosystem in general. 我是新的(大约2个月使用mac)到iOS和Swift以及整个mac生态系统。

The thing is that I'm migrating to native iOS an Phonegap app that relies on a lot of markers (about 300 to 400, and lags a lot) that some of them can be disabled by the user(by groups), the problem comes when deleting the markers some of them remain like ghosts, their map property is set to nil, and no touch event is triggered(I've got them set to perform a segue) 问题是我正在迁移到本机iOS一个依赖于大量标记(大约300到400,并且滞后很多)的Phonegap应用程序,其中一些可以被用户禁用(按组),问题来了当删除标记时,它们中的一些仍然像鬼一样,它们的map属性设置为nil,并且没有触发触摸事件(我已将它们设置为执行segue)

Here is the code that I use to store the objects, and then to delete them, currently I'm using a dictionary with arrays to determine which markers to delete. 这是我用来存储对象然后删除它们的代码,目前我正在使用带数组的字典来确定要删除的标记。

I translated the comments because they are in Spanish so you can have an idea of what i'm doing (or trying to do) in short terms. 我翻译了这些评论,因为它们是西班牙语,所以你可以在短期内了解我正在做什么(或尝试做什么)。

Here is the code where I add the markers, also I know that there might be some better ways to do some things like 这是我添加标记的代码,我也知道可能有更好的方法来做一些事情

//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
        }
    }
}

Here I delete the markers 在这里我删除标记

//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
        }
    }
}

I have googled for the answer and I have been researching and found nothing relevant, other than that some years ago, in an old google maps api had a bug, but it was fixed, also searched in the documentation but the only thing I found was that the other way to delete markers is with mapView.clear() but then i would have to reassign all the markers and the cpu goes to about 60% and thats worst than about 5% cpu usage with some poorly coded ways to cast string to int. 我已经搜索了答案,我一直在研究,发现没什么相关的,除了几年前,在旧的谷歌地图api有一个错误,但它是固定的,也在文档中搜索,但我发现的唯一的事情是删除标记的另一种方法是使用mapView.clear()然后我将不得不重新分配所有标记,并且cpu变为大约60%,这比使用一些编码不良的方法将字符串转换为大约5%的cpu使用率更差INT。

Here I give you just overview you can cross check it with your existing code 在这里,我为您提供概述,您可以使用现有代码进行交叉检查

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!)

                }

          }
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM