Created
August 28, 2018 13:48
-
-
Save eridbardhaj/28e9c3edd670b83b3e3b4618bda1eb17 to your computer and use it in GitHub Desktop.
A working sample to GoogleMaps SDK, for downloading and binding images from a specific `URL` to a `GMSMarker`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GoogleMapsViewController.swift | |
// | |
// Created by Erid Bardhaj on 8/20/18. | |
// Copyright © 2018 Erid Bardhaj. All rights reserved. | |
// | |
import UIKit | |
import GoogleMaps | |
class GoogleMapsViewController: UIViewController { | |
// MARK: - Properties | |
// MARK: Immutable | |
private let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 12.0) | |
private lazy var mapView = GMSMapView.map(withFrame: .zero, camera: camera) | |
// MARK: - Overrides | |
override func loadView() { | |
view = mapView | |
createMarkers() | |
} | |
// MARK: - Actions | |
func createMarkers() { | |
let mapCoordinates: [CLLocationCoordinate2D] = coordinates() | |
let iconURLs: [URL] = imageURLs() | |
for i in 0..<mapCoordinates.count { | |
let imageURL = iconURLs[i] | |
let marker = GMSMarker() | |
marker.position = mapCoordinates[i] | |
marker.map = mapView | |
applyImage(from: imageURL, to: marker) | |
} | |
} | |
// MARK: - Helpers | |
func applyImage(from url: URL, to marker: GMSMarker) { | |
DispatchQueue.global(qos: .background).async { | |
guard let data = try? Data(contentsOf: url), | |
let image = UIImage(data: data)?.cropped() | |
else { return } | |
DispatchQueue.main.async { | |
marker.icon = image | |
} | |
} | |
} | |
// MARK: Content | |
func coordinates() -> [CLLocationCoordinate2D] { | |
let location0 = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20) | |
let location1 = CLLocationCoordinate2D(latitude: -33.88, longitude: 151.20) | |
let location2 = CLLocationCoordinate2D(latitude: -33.90, longitude: 151.20) | |
let location3 = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.22) | |
let location4 = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.24) | |
let location5 = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.26) | |
return [ | |
location0, | |
location1, | |
location2, | |
location3, | |
location4, | |
location5 | |
] | |
} | |
func imageURLs() -> [URL] { | |
let url0 = "https://wallpaperbrowse.com/media/images/3848765-wallpaper-images-download.jpg" | |
let url1 = "https://cdn.pixabay.com/photo/2016/10/27/22/53/heart-1776746_960_720.jpg" | |
let url2 = "https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=350" | |
let url3 = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRZjVLtJDgMTOExfMHsTZuT4G5cAmaRT0N0vnoVbblrTTKkwSOb" | |
let url4 = "https://www.bmw-me.com/content/dam/bmw/common/all-models/m-series/m4-convertible/2017/images-and-videos/images/BMW-m4-convertible-images-and-videos-1920x1200-06.jpg.asset.1510607725528.jpg" | |
let url5 = "https://wallpaperbrowse.com/media/images/maxresdefault_RlXh3Bt.jpg" | |
return [ | |
url0, | |
url1, | |
url2, | |
url3, | |
url4, | |
url5 | |
].map { URL(string: $0)! } | |
} | |
} | |
// MARK: - Extensions | |
extension UIImage { | |
func cropped() -> UIImage? { | |
let cropRect = CGRect(x: 0, y: 0, width: 44 * scale, height: 44 * scale) | |
guard let croppedCGImage = cgImage?.cropping(to: cropRect) else { return nil } | |
return UIImage(cgImage: croppedCGImage, scale: scale, orientation: imageOrientation) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment