2017-06-21 31 views
1

我希望有人能幫助我發現這一點,自從昨天起我有這個問題,一直在尋找可能是錯的,也許我錯過了什麼?我是新手(大約2個月使用蘋果機)到iOS和Swift以及整個Mac生態系統。有些標記不會在谷歌地圖中使用iOS和Swift消失

問題是,我正在遷移到原生iOS Phonegap應用程序,它依賴於很多標記(大約300到400,並且滯後很多),其中一些可以被用戶禁用(按組) ,刪除標記時會出現問題,它們中的一些仍然像鬼一樣,它們的地圖屬性設置爲零,並且沒有觸摸事件被觸發(我讓它們設置爲執行搜索)

這裏是代碼我用來存儲對象,然後刪除它們,目前我正在使用包含數組的字典來確定要刪除哪些標記。

我翻譯了這些評論,因爲它們是西班牙文的,所以你可以在短期內瞭解我在做什麼(或者試圖做什麼)。

這裏是我添加標記的代碼,我也知道可能有一些更好的方式做一些事情像

//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%,並且這比cpu使用率大約低5%,使用一些糟糕的編碼方式將字符串轉換爲int。

+1

嗨@zxinik檢查它,你能下來減少的問題成片的代碼,其他人可以在他們的機器測試?從您發佈的代碼片段中很難看到究竟發生了什麼。 – paulvs

+1

你的英文很好,但請養成用大寫字母開始的句子,並把人稱代詞「I」表示爲首都。無論公平還是其他方面,如果他們看起來像是急於寫信,或者在首都太難以打字的移動電話上,帖子可能會被低估。 – halfer

+0

只是讓製造商內循環,並設置'製造商= nill'刪除製造商 –

回答

0

這裏,我給你只是概述您可以交叉與您現有的代碼

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

       } 

      } 
    } 
} 
+0

謝謝,我會檢查它是否會按你的方式工作,我會盡快通知你 – xzinik