Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add GMSMapView in Storyboard in IOS

https://developers.google.com/maps/documentation/ios-sdk/utility/marker-clustering

I am working for map clustering . In map clustering

private var mapView: GMSMapView!

is used for mapView but couldn't find any GMSMapView! in storyboard connection .

From Storyboard i found

 @IBOutlet var mapView: MKMapView!

that makes the problem . when i used exampled google map it throws error

this class is not key value coding-compliant for the key mapView.'

Here the complete code :

import UIKit
import MapKit
import GooglePlaces
import GoogleMaps



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

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

let kClusterItemCount = 10000
let kCameraLatitude = -33.8
let kCameraLongitude = 151.2

class FirstViewController: UIViewController , GMUClusterManagerDelegate,
GMSMapViewDelegate {



   // @IBOutlet var mapView: MKMapView!


    private var mapView: GMSMapView!



     private var clusterManager: GMUClusterManager!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set up the cluster manager with the supplied icon generator and
        // renderer.


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

        // Generate and add random items to the cluster manager.
        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)


    }

//    override func loadView() {
//        
//        // Create a GMSCameraPosition that tells the map to display the
//        // coordinate -33.86,151.20 at zoom level 6.
//        let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
//        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
//        view = mapView
//        
//        // Creates a marker in the center of the map.
//        let marker = GMSMarker()
//        marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
//        marker.title = "Sydney"
//        marker.snippet = "Australia"
//        marker.map = mapView
//        
//    }
//    

    // 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.name)")
        } else {
            NSLog("Did tap a normal marker")
        }
        return false
    }

    // MARK: - Private
    /// Randomly generates cluster items within some extent of the camera and adds them to the
    /// cluster manager.
    private func generateClusterItems() {
        let extent = 0.2
        for index in 1...kClusterItemCount {
            let lat = kCameraLatitude + extent * randomScale()
            let lng = kCameraLongitude + extent * randomScale()
            let name = "Item \(index)"
            let item = POIItem(position: CLLocationCoordinate2DMake(lat, lng), name: name)
            clusterManager.add(item)
        }
    }

    /// Returns a random value between -1.0 and 1.0.
    private func randomScale() -> Double {
        return Double(arc4random()) / Double(UINT32_MAX) * 2.0 - 1.0
    }
}
like image 949
arif hasnat Avatar asked Mar 20 '17 02:03

arif hasnat


2 Answers

First Take UIView:

Take UIView

Add UIView In UIViewController:

Add View

Assign class GMSMap View

Assign class

And Now create outlet of GMSMapView.

@IBOutlet var mapView: GMSMapView!
like image 61
Sakir Sherasiya Avatar answered Nov 05 '22 00:11

Sakir Sherasiya


To add GMSMapView in Storyboard in IOS, open the Identity Inspector and under Custom Class add GMSMapView. You may follow this tutorial about Google Maps SDK for iOS in Xcode Storyboard for the step-by-step instructions. For your error this class is not key value coding-compliant for the key mapView, you might have created an outlet called mapview and deleted it later. Check whether the outlets of MapView is broken in Xib/Storyboard. Here are also some references which might help: https://www.raywenderlich.com/109888/google-maps-ios-sdk-tutorial and What does this mean? "'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X"

like image 4
abielita Avatar answered Nov 05 '22 00:11

abielita