gpt4 book ai didi

ios - IOS中Storyboard中如何添加GMSMapView

转载 作者:IT王子 更新时间:2023-10-29 05:46:04 24 4
gpt4 key购买 nike

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

我正在为 map 聚类工作。在 map 聚类中

private var mapView: GMSMapView!

用于 mapView 但找不到任何 GMSMapView!在 Storyboard连接中。

从 Storyboard 我找到了

 @IBOutlet var mapView: MKMapView!

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

此类对于键 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:

Take UIView

在 UIViewController 中添加 UIView:

Add View

分配类 GMSMap View

Assign class

现在创建 GMSMapView 的导出。

@IBOutlet var mapView: GMSMapView!

关于ios - IOS中Storyboard中如何添加GMSMapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42894830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com