如何在IOS的Storyboard中添加GMSMapView

8

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

我正在处理地图聚合。在地图聚合中,


请提供需要翻译的具体内容。
private var mapView: GMSMapView!

用于地图视图的是GMSMapView,但在故事版连接中找不到任何GMSMapView!

从故事板中发现:

 @IBOutlet var mapView: MKMapView!

这就是问题所在。当我使用谷歌地图的示例时,它会抛出错误。

这个类不符合关键值编码(KVC)规范,无法访问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
    }
}

只需添加一个视图并将其类设置为“GMSMapView”,而不是“MKMapView”。 - chengsam
2个回答

27

首先获取UIView:

获取UIView

在UIViewController中添加UIView:

添加View

指定GMSMapView的类:

指定类

现在创建GMSMapView的outlet。

@IBOutlet var mapView: GMSMapView!

1
将我的答案标记为正确答案,以便其他人可以轻松找到答案。 - Sakir Sherasiya
2
请确保在您的视图控制器中添加 "import GoogleMaps"。 - Jonathan Vukadinovic

4

要在IOS的Storyboard中添加GMSMapView,请打开“Identity Inspector”,并在“Custom Class”下添加GMSMapView。您可以按照此tutorial关于Xcode Storyboard中的Google Maps SDK for iOS的逐步说明。对于您的error此类不符合关键值编码键mapView,您可能已经创建了一个名为mapview的outlet并将其删除。请检查Xib/Storyboard中的MapView的outlets是否已损坏。这里还有一些可能有所帮助的参考资料:https://www.raywenderlich.com/109888/google-maps-ios-sdk-tutorial这是什么意思?“'NSUnknownKeyException',原因:…此类不符合关键值编码键X”


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接