2017-03-24 72 views
0

我正在使用Google Map for IOS。在哪裏實現了羣集,但羣集和羣集項目標記點擊事件不起作用。但個別谷歌地圖標記點擊事件正在工作。Ios Google Map Cluster項目點擊事件不起作用

這裏是我完整的代碼

import UIKit 
import GoogleMaps 
import Alamofire 
import SwiftyJSON 


let kCameraLatitude = 22.3475 
let kCameraLongitude = 91.8123 



/// Point of Interest Item which implements the GMUClusterItem protocol. 
class POIItem: NSObject, GMUClusterItem { 
    var position: CLLocationCoordinate2D 

    var currencies = "currencies"; 
    var phone = "phone"; 
    var dateTime = "dateTime"; 
    var location = "location"; 
    var link = "link"; 
    var exchangeLimits = "exhangeLimits"; 
    var id = "id"; 
    var updated_at = "updated"; 
    var operationName = "opeationName"; 
    var email = "email"; 
    var address = "address"; 
    var createdAt = "createdAt"; 
    var workingDays = "workingDays"; 
    var longitude = "longitude"; 
    var latitude = "latitude"; 
    var exchange = "exChange"; 


    init(position: CLLocationCoordinate2D, operationName: String) { 
     self.position = position 
     self.operationName = operationName 
    } 
} 

class FirstViewController: UIViewController , GMUClusterManagerDelegate, GMSMapViewDelegate{ 
    private var mapView: GMSMapView! 
    private var clusterManager: GMUClusterManager! 

    override func loadView() { 
     let camera = GMSCameraPosition.camera(withLatitude: kCameraLatitude, 
                  longitude: kCameraLongitude, zoom: 6) 
     mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera) 
     mapView.delegate = self 
     self.view = mapView 




    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 


     // Set up the cluster manager with default icon generator and renderer. 
     let iconGenerator = GMUDefaultClusterIconGenerator() 
     let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm() 
     let renderer = GMUDefaultClusterRenderer(mapView: mapView, clusterIconGenerator: iconGenerator) 
     clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm, renderer: renderer) 


     getAllMapsData() 


     clusterManager.cluster() 


     // Register self to listen to both GMUClusterManagerDelegate and GMSMapViewDelegate events. 
     clusterManager.setDelegate(self, mapDelegate: self) 



    } 

    func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) { 
     print("You tapped at \(coordinate.latitude), \(coordinate.longitude)") 

     var alert = UIAlertController(title: "Choose Media type" , message: "post any" , preferredStyle: .actionSheet); 

     var photos = UIAlertAction(title : "Photos" , style: .default , handler: nil) 
     var videos = UIAlertAction(title : "Videos" , style: .default , handler: nil) 

     var cancel = UIAlertAction(title : "Calcel" , style: .default , handler: nil) 
     alert.addAction(photos) 
     alert.addAction(videos) 
     alert.addAction(cancel) 
     present(alert, animated: true , completion: nil) 

    } 



    // MARK: - GMUClusterManagerDelegate 
    func clusterManager(clusterManager: GMUClusterManager, didTapCluster cluster: GMUCluster) { 
     let newCamera = GMSCameraPosition.camera(withTarget: cluster.position, 
                  zoom: mapView.camera.zoom + 1) 
     let update = GMSCameraUpdate.setCamera(newCamera) 
     mapView.moveCamera(update) 
    } 

    // MARK: - GMUMapViewDelegate 
    func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) -> Bool { 
     if let poiItem = marker.userData as? POIItem { 
      NSLog("Did tap marker for cluster item \(poiItem.operationName)") 
      print("clicked cluster") 
     } else { 
      NSLog("Did tap a normal marker") 
      print("clicked marker") 
     } 
     return false 
    } 




    var allDataList = [[String:AnyObject]]() 

    public func getAllMapsData(){ 


     Alamofire.request("http://coinatmfinder.com/getDatas").responseJSON { response in 

      if let JSON = response.result.value { 

       self.allDataList = JSON as! [[String : AnyObject]] 

       for eachData in self.allDataList { 

        //let allDataModel = AllDataModel() 

        // allDataModel.address = eachData["address"] as! String 

        var operatorName = eachData["operatorName"] 

        var lat = eachData["latitude"] 
        var latDouble = 0.0; 

        if let lats = lat { 
         latDouble = (lats as! NSString).doubleValue 
        } 

        var longs = eachData["longitude"] 

        var longsDouble = 0.0; 

        if let longs = longs { 
         longsDouble = (longs as! NSString).doubleValue 
        } 


        let item = POIItem(position: CLLocationCoordinate2DMake(latDouble, longsDouble), operationName: operatorName as! String) 

        self.clusterManager.add(item) 






       } 




       print("JSON: \(self.allDataList)") 
      } 
     } 




    } 





} 

回答

2

正在這個今天也嘗試了同樣的功能,並發現它不能正常工作,因爲這是錯誤的。相反的:

func clusterManager(clusterManager: GMUClusterManager, didTapCluster cluster: GMUCluster) { 
    let newCamera = GMSCameraPosition.camera(withTarget: cluster.position, 
               zoom: mapView.camera.zoom + 1) 
    let update = GMSCameraUpdate.setCamera(newCamera) 
    mapView.moveCamera(update) 
} 

試試這個:(它看起來幾乎一樣,所不同它具有布爾返回類型之一)。

func clusterManager(_ clusterManager: GMUClusterManager, didTap cluster: GMUCluster) -> Bool { 
    let newCamera = GMSCameraPosition.camera(withTarget: cluster.position, 
              zoom: mapView.camera.zoom + 1) 
    let update = GMSCameraUpdate.setCamera(newCamera) 
    mapView.moveCamera(update) 

    return false   
} 

(使用夫特3)