Ios Google Map Cluster项目点击事件不起作用

时间:2017-03-24 05:54:07

标签: ios google-maps markerclusterer

我正在使用Google Map for IOS。我已经实现了群集,但群集和群集项目标记点击事件不起作用。但个人谷歌地图标记点击事件正在发挥作用。

这是我的完整代码

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)")
            }
        }




    }





}

1 个答案:

答案 0 :(得分: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)
}

试试这个:(它看起来与上面的几乎相同,只是它的返回类型为Bool)。

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           
}

(使用Swift 3)