現在GoogleMap SDK for ios を使ってアプリを開発中です。
マーカーを作成し、マーカーの画像、サイズ、吹き出しなどを一通り仕上げた後
マーカーのクラスター化を実装しようと考えました。
しかし、マーカーを作成する方法がすでに作っていたコードと異なっていました。
問題点としては、
1、マーカーの画像をデフォルトから変更したい
2、xibファイルで作成した吹き出しをマーカーがタップされた時に呼びたい
初学者である為、拙いコードですが
よろしくお願いします。
#####参考記事
https://qiita.com/mshrwtnb/items/b7929275bd22868c7717
https://developers.google.com/maps/documentation/ios-sdk/utility/marker-clustering
Swift
1class MapView: UIViewController, UITextFieldDelegate, GMUClusterManagerDelegate, GMSMapViewDelegate, GMUClusterRendererDelegate{ 2 var mapView: GMSMapView! 3 var marker : GMSMarker? 4 let tokushimaLatitude = 34.077875549971 //徳島市の緯度経度 5 let tokushimaLongitude = 134.56156512254 6 7 var clusterManager: GMUClusterManager! 8 let isClustering: Bool = true 9 let isCustom: Bool = true 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 let camera = GMSCameraPosition.camera(withLatitude: tokushimaLatitude, 15 longitude: tokushimaLongitude, 16 zoom: 10.0) 17 let mapView = GMSMapView.map(withFrame: view.frame, 18 camera: camera) 19 //viewにGoogleMapsを表示 20 self.view = mapView 21 //GoogleMapsにmarkerを表示 22 self.mapView = mapView 23 //地図のタイプ 24 self.mapView.mapType = .normal 25 //GoogleMapsのmarkerを消す 26 self.mapView.clear() 27 //接続 28 mapView.delegate = self 29 30 //再描画 31// reflesh() 32 33 if isClustering { 34 var iconGenerator: GMUDefaultClusterIconGenerator! 35 if isCustom { // クラスターの画像 36 //marker数[5, 10, 15, 20, 25]の順にimagesの画像が適応 37 let images: [UIImage] = [UIImage(named: "img_1")!, UIImage(named: "img_2")!, UIImage(named: "img_3")!, UIImage(named: "img_4")!, UIImage(named: "img_5")!] 38 iconGenerator = GMUDefaultClusterIconGenerator(buckets: [5, 10, 15, 20, 25], backgroundImages: images) 39 } else { 40 iconGenerator = GMUDefaultClusterIconGenerator() 41 } 42 43 let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm() 44 let renderer = GMUDefaultClusterRenderer(mapView: mapView, clusterIconGenerator: iconGenerator) 45 renderer.delegate = self 46 clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm, renderer: renderer) 47 } else { 48 } 49 50// let firstLocation = CLLocationCoordinate2DMake(tokushimaLatitude, tokushimaLongitude) 51// let marker = GMSMarker(position: firstLocation) 52// marker.icon = UIImage(named: "img_xx") 53// marker.map = mapView 54 55// let secondLocation = CLLocationCoordinate2DMake(34.057876, 134.531566) 56// let secondMarker = GMSMarker(position: secondLocation) 57// secondMarker.icon = UIImage(named: "img_yy") 58// secondMarker.map = mapView 59 60 addMarkers(cameraLatitude: tokushimaLatitude, cameraLongitude: tokushimaLongitude) 61 62 clusterManager.cluster() 63 clusterManager.setDelegate(self, mapDelegate: self) 64 } 65 /// Point of Interest Item which implements the GMUClusterItem protocol. 66 class MarkerModel: NSObject, GMUClusterItem { 67 var position: CLLocationCoordinate2D 68 var name: String! 69 70 init(position: CLLocationCoordinate2D, name: String) { 71 self.position = position 72 self.name = name 73 } 74 } 75 func renderer(_ renderer: GMUClusterRenderer, willRenderMarker marker: GMSMarker){ 76 if let markerData = marker.userData{ 77 //??? 78 } 79 } 80 81 func clusterManager(_ clusterManager: GMUClusterManager, didTap cluster: GMUCluster) -> Bool{ 82 let newCamera = GMSCameraPosition.camera(withTarget: cluster.position, zoom: mapView.camera.zoom + 1) 83 let update = GMSCameraUpdate.setCamera(newCamera) 84 mapView.moveCamera(update) 85 return false 86 } 87 func addMarkers(cameraLatitude : Double, cameraLongitude : Double) { 88 let extent = 0.01 89 for index in 1...100 { 90 let lat = cameraLatitude + extent * randomScale() 91 let lng = cameraLongitude + extent * randomScale() 92 let name = "Item (index)" 93 94 let position = CLLocationCoordinate2DMake(lat, lng) 95 96 let item = MarkerModel(position: position, name: name) 97 clusterManager.add(item) 98 } 99 clusterManager.cluster() 100 clusterManager.setDelegate(self, mapDelegate: self) 101 } 102 103 private func randomScale() -> Double { 104 return Double(arc4random()) / Double(UINT32_MAX) * 2.0 - 1.0 105 } 106 107 func renderer(_ renderer: GMUClusterRenderer, markerFor object: Any) -> GMSMarker? { 108 109 let marker = GMSMarker() 110 if let model = object as? MarkerModel { 111 // set image view for gmsmarker 112 } 113 114 return marker 115 } 116 117 //markerをタップする時最初に呼ばれる 118 func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool { 119 if let poiItem = marker.userData as? MarkerModel { 120 NSLog("Did tap marker for cluster item (String(describing: poiItem.name))") 121 } else { 122 NSLog("Did tap a normal marker") 123 } 124 let position = marker.position 125 let point = mapView.projection.point(for: position) 126 let newPoint = mapView.projection.coordinate(for: point) 127 let camera = GMSCameraUpdate.setTarget(newPoint) 128 //吹き出しの位置を常にmarkerの上にするためのコード 129 self.marker = marker 130 //アニメーション? 131 mapView.animate(toLocation: position) 132 mapView.animate(with: camera) 133 //透過度 134 let opaqueWhite = UIColor(white: 1, alpha: 0.85) 135 //markerの吹き出し生成 136 if let infoView = infoView{ //nilの場合elseへ 137 infoView.layer.backgroundColor = opaqueWhite.cgColor //透過度 138 infoView.layer.cornerRadius = 8 //吹き出しの角を丸くする 139 infoView.center = mapView.projection.point(for: position) //吹き出しをピンの真上に出す 140 infoView.label1.text = "aaa" 141 142 mapView.addSubview(infoView) 143 } 144 return false 145 } 146}
あなたの回答
tips
プレビュー