-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.swift
executable file
·90 lines (67 loc) · 2.85 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// ViewController.swift
// GoogleMapsUtils_test
//
// Created by alex on 03/10/16.
// Copyright © 2016 alex. All rights reserved.
//
import UIKit
import GoogleMaps
let kClusterItemCount = 15000
let kCameraLatitude = 55.9825
let kCameraLongitude = 37.1814
class ViewController: UIViewController, GClusterManagerDelegate, GMSMapViewDelegate {
private var mapView: GMSMapView!
private var clusterManager: GClusterManager!
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Google Maps Utils Test"
let camera = GMSCameraPosition.cameraWithLatitude(kCameraLatitude, longitude: kCameraLongitude, zoom: 10)
mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
self.view = mapView
let algorithm = NonHierarchicalDistanceBasedAlgorithm()
let renderer = GDefaultClusterRenderer(mapView: mapView)
clusterManager = GClusterManager(mapView: mapView, algorithm: algorithm, renderer: renderer)
generateClusterItems()
clusterManager.cluster()
// Register self to listen to both GClusterManagerDelegate and GMSMapViewDelegate events.
clusterManager.setDelegate(self, mapDelegate: self)
}
// MARK: - GClusterManagerDelegate
func clusterManager(clusterManager: GClusterManager, didTapCluster cluster: GCluster) {
let newCamera = GMSCameraPosition.cameraWithTarget(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 spot = marker.userData as? Spot {
NSLog("Did tap marker for cluster item \(spot.id)")
} 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 pos = CLLocationCoordinate2DMake(lat, lng)
let id = "Item \(index)"
let item = Spot(id: id, position: pos)
clusterManager.addItem(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
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}