如何在IOS的Storyboard中添加GMSMapView

前端之家收集整理的这篇文章主要介绍了如何在IOS的Storyboard中添加GMSMapView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
https://developers.google.com/maps/documentation/ios-sdk/utility/marker-clustering

我正在为地图聚类工作.在地图聚类中

private var mapView: GMSMapView!

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

从故事板我发现

@IBOutlet var mapView: MKMapView!

这就产生了问题.当我使用exampled谷歌地图时,它会抛出错误

这个类不是键值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
    }
}

解决方法

先来看看UIView:

在UIViewController中添加UIView:

分配类GMSMap视图

现在创建GMSMapView的出口.

@IBOutlet var mapView: GMSMapView!
原文链接:https://www.f2er.com/iOS/331151.html

猜你在找的iOS相关文章