Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Google-Maps-iOS-Utils not working,when added manually #37

Closed
nitisha1 opened this issue Sep 12, 2016 · 4 comments
Closed

Google-Maps-iOS-Utils not working,when added manually #37

nitisha1 opened this issue Sep 12, 2016 · 4 comments

Comments

@nitisha1
Copy link

Hi,
I've added two folders "Clustering and QuadTree" from Google-Maps-Utils library to integrate marker clustering but its not showing clusters.
Below is my code-
class ViewController: UIViewController,GMSMapViewDelegate,GMUClusterManagerDelegate{

@IBOutlet var gmsMapView: GMSMapView!
let kClusterItemCount = 10000
let kCameraLatitude = -33.8
let kCameraLongitude = 151.2
private var clusterManager: GMUClusterManager!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let camera = GMSCameraPosition.cameraWithLatitude(kCameraLatitude,
                                                      longitude: kCameraLongitude, zoom: 10)
    gmsMapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)



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


    generateClusterItems()

    // Call cluster() after items have been added to perform the clustering and rendering on map.
    clusterManager.cluster()

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

}



// MARK: - GMUClusterManagerDelegate

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

// MARK: - GMUMapViewDelegate

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


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


// MARK: - Private

/// Randomly generates cluster items within some extent of the camera and adds them to the
/// cluster manager.
private func generateClusterItems() {

    dispatch_async(dispatch_get_main_queue()) {

    if(self.gmsMapView != nil){


    let extent = 0.2
    for index in 1...self.kClusterItemCount {
    let marker:GMSMarker = GMSMarker()
    marker.title = "marker"
    let lat = self.kCameraLatitude + extent * self.randomScale()
    let lng = self.kCameraLongitude + extent * self.randomScale()

    marker.position = CLLocationCoordinate2DMake(lat, lng)
    marker.snippet = "1234"
    marker.icon = self.generateSpot(index)
   let spotObje:Spot = Spot()
   spotObje.location = marker.position
   spotObje.marker = marker
   spotObje.marker.map = self.gmsMapView
   self.clusterManager.addItem(spotObje)
    }
    }

    }

}

func generateSpot(count:Int) -> UIImage{


    let sizeVar:CGSize = CGSize(width: 41, height: 37)

    let customeMarker :CustomMarker = NSBundle.mainBundle().loadNibNamed("customview", owner: self.view, options: nil).first! as! CustomMarker

    customeMarker.frame = CGRectMake(customeMarker.frame.minX,customeMarker.frame.minY,customeMarker.frame.width,customeMarker.frame.height)
    customeMarker.mappedNumbers.layer.masksToBounds = true
    customeMarker.mappedNumbers.layer.cornerRadius = 8
    customeMarker.mappedNumbers.text = String(count)


    UIGraphicsBeginImageContextWithOptions(sizeVar, false, 0)
    customeMarker.layer.renderInContext(UIGraphicsGetCurrentContext()!)

    /// customeMarker.drawViewHierarchyInRect(customeMarker.bounds, afterScreenUpdates: true)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()


    return newImage

}

/// Returns a random value between -1.0 and 1.0.
private func randomScale() -> Double {
    return Double(arc4random()) / Double(UINT32_MAX) * 2.0 - 1.0
}

}

//Spot.h

import <Foundation/Foundation.h>

@import CoreLocation;

import "GMUClusterItem.h"

@interface Spot : NSObject

@Property (nonatomic) CLLocationCoordinate2D location;

@Property (nonatomic, strong) GMSMarker *marker;

@EnD

@anand-y
Copy link

anand-y commented Sep 14, 2016

@nitisha1 I to am facing similar kind of problem #40

@nitisha1
Copy link
Author

nitisha1 commented Sep 15, 2016

@yadavanand
well,i just added self.view = gmsmapView but it is not the acceptable solution i guess atleast in my case.it is because of delegate i found and update my code.
` //[THis method contains Show Google Map Logic for the properties! that are synch]
func loadGoogleMap(){

    dispatch_async(dispatch_get_main_queue()) {

        print(" GOOGLE \(self.googleMapView)")
        if(self.googleMapView != nil){

            self.clearMap()
            self.googleMapView!.clear()
            self.googleMapView.clearsContextBeforeDrawing = true

            let path = GMSMutablePath()
            var count = 0

            self.delay(seconds: 0.1) { () -> () in


                    count = 0

                for Prop : Property in self.properties!
                {

                    if Prop.propLat != ""
                    {
                        let latStr = Prop.propLat
                        let latDbl : Double  = Double(latStr.floatValue)
                        let langStr = Prop.propLang as NSString
                        let langDbl : Double = Double(langStr.floatValue)
                        path.addCoordinate(CLLocationCoordinate2DMake(langDbl,latDbl))

                        if(count == 0)
                        {

                            // Set up the cluster manager with default icon generator and renderer.
                            let iconGenerator = GMUDefaultClusterIconGenerator()
                            let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
                           // let renderer1 = MyCustomRenderer(mapView: self.googleMapView, clusterIconGenerator: iconGenerator)

                            let renderer = GMUDefaultClusterRenderer(mapView: self.googleMapView!, clusterIconGenerator: iconGenerator)
                            self.clusterManager = GMUClusterManager(map: self.googleMapView!, algorithm: algorithm, renderer: renderer)
                            }

                        let marker:GMSMarker = GMSMarker()
                        marker.position = CLLocationCoordinate2DMake(langDbl,latDbl)
                        marker.appearAnimation = kGMSMarkerAnimationPop
                        marker.title = Prop.propBuildingName as String
                        marker.snippet = Prop.propCode as String
                        //    marker.infoWindowAnchor = CGPointMake(0.44, 0.45)

                        let markerIcon:UIImage = self.generateSpot(Prop)
                        marker.icon = markerIcon

                        //marker.map = self.googleMapView
                        self.markersArray!.append(marker)

                        let spotObje:Spot = Spot()
                        spotObje.location = marker.position
                        spotObje.marker = marker

                         self.clusterManager.addItem(spotObje)

                    }
                    count = count+1

                    }

                let bounds = GMSCoordinateBounds(path: path)

                if(self.googleMapView != nil)
                {

                    // transaction animaton inreases memory usage
                    CATransaction.begin()
                    CATransaction.setValue(NSNumber(float: 3.0), forKey: kCATransactionAnimationDuration)
                    self.googleMapView!.animateWithCameraUpdate(GMSCameraUpdate.fitBounds(bounds, withPadding: 200.0))
                    CATransaction.commit()

                    self.googleMapView.delegate = self
                    // Call cluster() after items have been added to perform the clustering and rendering on map.

                    if(self.clusterManager != nil)
                    {
                    self.clusterManager.cluster()


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

            }

        }
    }
}`

@mountainvat
Copy link
Contributor

Can you please attach a screenshot? How many items are there in self. properties? How is it populated?

Can you do a pod try Google-Maps-iOS-Utils and select the Swift demo app to see if it works for you and take it from there?

Alternatively if there is a git repo for me to take a look it would be easier for me but I understand that it may not be possible.

Unrelated but I do not think you need the create a marker for each spotObje. The ClusterManager will take care of creating the marker for you.

@domesticmouse
Copy link
Contributor

Closing this out as stale. If this is still an issue, please re-open this issue with a link to a minimal reproduction case in a GitHub repository. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants