如何在IOS的Storyboard中添加GMSMapView

时间:2017-03-20 02:16:53

标签: ios swift google-maps mkmapview gmsmapview

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

我正在为地图群集工作。在地图聚类中

private var mapView: GMSMapView!

用于mapView但找不到任何GMSMapView!在故事板连接。

从故事板我发现

 @IBOutlet var mapView: MKMapView!

这就产生了问题。当我使用exampled google map时会抛出错误

此类不符合键值mapView的键值编码。

这里有完整的代码:

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

2 个答案:

答案 0 :(得分:17)

首先采取UIView:

Take UIView

在UIViewController中添加UIView:

Add View

分配类GMSMap视图

Assign class

现在创建GMSMapView的出口。

@IBOutlet var mapView: GMSMapView!

答案 1 :(得分:4)

要在IOS的Storyboard中添加GMSMapView,请打开Identity Inspector,然后在Custom Class add GMSMapView下。您可以按照此tutorial关于 Google Maps SDK for iOS的Xcode Storyboard 进行逐步说明。对于您的error ,此类不符合键值mapView的键值编码,您可能已创建了一个名为mapview的插座,稍后会将其删除。检查MapView的出口是否在Xib / Storyboard中断开。以下是一些可能有用的参考资料:https://www.raywenderlich.com/109888/google-maps-ios-sdk-tutorialWhat does this mean? "'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X"