Google Maps SDK for iOS Utility Library

repository·main·Indexed 20 days ago

https://github.com/googlemaps/google-maps-ios-utils

An open-source collection of classes extending the Google Maps SDK for iOS. It provides advanced features including marker clustering via GMUClusterManager, KML and GeoJSON rendering with GMUKmlParser and GMUGeometryRenderer, heatmap visualization with interpolation for sparse data sets, and Swift-friendly wrappers for spherical geometry utilities.

Tokens
2.5K
Snippets
8
Records
12
Agent score
73%

What's inside Google Maps SDK for iOS Utility Library

  1. Use Swift-friendly Geometry Utilities

    main

    The Google Maps SDK for iOS provides GMSGeometryUtils as a set of C-style functions. This utility library provides Swift-friendly extensions and classes that wrap those functions, allowing you to use idiomatic Swift syntax instead of C-style calls.

    Key components include:

    • MapPoint: A Swift class for representing points.
    • Extensions for CLLocationCoordinate2D, GMSPath, GMSPolygon, and GMSPolyline to provide geometric operations directly on these types.
  2. Use Heatmap Interpolation for sparse data sets

    main

    Heatmap interpolation enables the creation of robust heatmaps by calculating average intensities between data points. This is particularly useful for sparse data sets where you want to produce a more continuous and meaningful heatmap.

    To use this feature:

    1. Provide a list of GMUWeightedLatLng objects (or a single object).
    2. Use the class's point generating function to receive an array of interpolated GMUWeightedLatLng objects.
    3. Add the resulting array of interpolated points to your existing heatmap.
  3. Install Google Maps SDK for iOS Utility Library

    main

    To use this library, you must first include the GoogleMaps dependency. Then, you can add the utility library using one of the following methods:

    Swift Package Manager

    1. Add the package using the URL: https://github.com/googlemaps/google-maps-ios-utils
    2. Important: You must also install the Maps SDK for iOS via SPM using the URL: https://github.com/googlemaps/ios-maps-sdk
    3. Select the version compatible with your Maps SDK version:
      • Version 6.x supports Maps SDK v9.x
      • Version 5.0 supports Maps SDK v8.x
      • Version 4.2.2 supports Maps SDK v7.x

    CocoaPods

    Add the following to your Podfile:

    use_frameworks!
    
    target 'TARGET_NAME' do
      pod 'GoogleMaps', '10.0.0'
      pod 'Google-Maps-iOS-Utils', '7.1.0'
    end

    Then run pod install in your terminal.

    Carthage

    Note: Only supported if using Maps SDK v7.1.0 or earlier.

    Add this to your Cartfile:

    github "googlemaps/google-maps-ios-utils" ~> 7.1.0
  4. Run the Swift sample app

    main

    To run the Swift demonstration application, navigate to the SwiftDemoApp directory, install the necessary CocoaPods dependencies, and open the workspace. You must provide a valid Google Maps API key by replacing the placeholder in AppDelegate.swift.

    cd SwiftDemoApp
    pod install 
    open SwiftDemoApp.xcworkspace

    Configuration Step: In AppDelegate.swift, locate the mapsAPIKey variable and replace its value with your actual Google Maps API key.

    // In AppDelegate.swift
    let mapsAPIKey = "YOUR_API_KEY_HERE"
  5. Run the Objective-C sample app

    main

    To run the Objective-C demonstration application, navigate to the ObjCDemoApp directory, install the necessary CocoaPods dependencies, and open the workspace. You must provide a valid Google Maps API key by replacing the placeholder in AppDelegate.m.

    cd ObjCDemoApp
    pod install 
    open ObjCDemoApp.xcworkspace

    Configuration Step: In AppDelegate.m, locate the kMapsAPIKey variable and replace its value with your actual Google Maps API key.

    // In AppDelegate.m
    NSString *kMapsAPIKey = @"YOUR_API_KEY_HERE";
  6. Customize cluster and item markers using GMUClusterRendererDelegate

    main

    To customize the appearance of markers (both individual items and clusters) before or after they are added to the map, implement the GMUClusterRendererDelegate protocol on a GMUDefaultClusterRenderer instance.

    This delegate provides hooks to modify marker properties, allowing for custom icons, colors, or other visual attributes for both cluster markers and individual item markers during the rendering process.

    // Implementation details can be found in the CustomMarkerViewController sample.
    // Use GMUClusterRendererDelegate on GMUDefaultClusterRenderer to customize marker properties.
  7. Render KML and GeoJSON geometries on a map

    main

    Starting from version 2.0.0, the utility library supports rendering common geometries from KML and GeoJSON inputs, including Point, Polyline, Polygon, and GroundOverlay.

    Limitations:

    • The rendered geometries do not support user interaction (e.g., tapping or selecting them).

    For a concrete implementation example, refer to the KMLViewController in the sample app.

    /* See KMLViewController for implementation details: 
    https://github.com/googlemaps/google-maps-ios-utils/blob/master/app/KMLViewController.m 
    */
  8. Display KML data using GMUKmlParser and GMUGeometryRenderer

    main

    To render KML files on a map:

    1. Create a GMUKmlParser with the URL of your KML file.
    2. Call .parse() on the parser.
    3. Use GMUGeometryRenderer to render the parsed placemarks, styles, and styleMaps onto your GMSMapView.
    import GoogleMaps
    import GoogleMapsUtils
    
    func renderKml() {
        // Parse KML
        let path: String = // Path to your KML file...
        let kmlUrl = URL(fileURLWithPath: path)
        let kmlParser = GMUKmlParser(url: kmlUrl)
        kmlParser.parse()
    
        // Render parsed KML
        let renderer = GMUGeometryRenderer(
            map: mapView,
            geometries: kmlParser.placemarks,
            styles: kmlParser.styles,
            styleMaps: kmlParser.styleMaps
        )
        renderer.render()
    }
  9. Cluster markers using GMUClusterManager

    main

    To handle the display of a large number of points, use GMUClusterManager. This requires setting up an icon generator, an algorithm, and a renderer.

    1. Initialize GMUDefaultClusterIconGenerator.
    2. Choose an algorithm (e.g., GMUNonHierarchicalDistanceBasedAlgorithm).
    3. Initialize GMUDefaultClusterRenderer with your GMSMapView and the icon generator.
    4. Create the GMUClusterManager with the map, algorithm, and renderer.
    5. Set the map delegate to the cluster manager to listen for events.
    6. Add your markers to the manager and call .cluster().
    import GoogleMaps
    import GoogleMapsUtils
    
    class MarkerClustering: UIViewController, GMSMapViewDelegate {
      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.
        let iconGenerator = GMUDefaultClusterIconGenerator()
        let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
        let renderer = GMUDefaultClusterRenderer(mapView: mapView, 
                                                clusterIconGenerator: iconGenerator)
        clusterManager = GMUClusterManager(map: mapView, algorithm: algorithm, 
                                                          renderer: renderer)
    
        // Register self to listen to GMSMapViewDelegate events.
        clusterManager.setMapDelegate(self)
        // ...
      }
    }
    
    let markerArray = [marker1, marker2, marker3, marker4] // define your own markers
    clusterManager.add(markerArray)
    
    clusterManager.cluster()
  10. Requirements for Google Maps SDK for iOS Utility Library

    main

    To use this library, ensure your environment meets the following requirements:

    • iOS: 16.0+
    • Xcode: 15.0+
    • Maps SDK for iOS: A compatible version (refer to the library's release notes for specific version pairings).
    • API Key: A valid Google Maps Platform API key from a project with the Maps SDK for iOS enabled.