How to add GMSMapView to storyboards in iOS

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

I work for clustering maps. In map clustering

private var mapView: GMSMapView!

used for mapView but cannot find GMSMapView! in the storyboard.

From the storyboard I found

 @IBOutlet var mapView: MKMapView!

what creates the problem. when i used the google map displayed it gives an error

this class is not the key for encoding the key for the key mapView.

Here is the full 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
    }
}
+6
source share
2 answers

First take a UIView:

Take uiview

Add UIView to UIViewController:

Add View

Assign GMSMap View Class

Assign class

And now create the GMSMapView output.

@IBOutlet var mapView: GMSMapView!
+9
source

GMSMapView Storyboard IOS, Custom Class GMSMapView. SDK Google Maps iOS Xcode Storyboard . mapView, , mapview , , MapView Xib/Storyboard. , : https://www.raywenderlich.com/109888/google-maps-ios-sdk-tutorial ? "NSUnknownKeyException" , :... , X "

+4

Source: https://habr.com/ru/post/1015881/


All Articles