Skip to content

Instantly share code, notes, and snippets.

@DMCApps
Created January 29, 2017 21:48
Show Gist options
  • Save DMCApps/dda5fbb824ce279a47510f5855c1010b to your computer and use it in GitHub Desktop.
Save DMCApps/dda5fbb824ce279a47510f5855c1010b to your computer and use it in GitHub Desktop.
//
// GoogleStaticMapUrl.swift
// DMCApps
//
// Created by Daniel Carmo on 2017-01-29.
// Copyright © 2017 DMCApps. All rights reserved.
import Foundation
import CoreLocation
class GoogleStaticMapUrl {
fileprivate static func encodeValue(_ value:String) -> String {
return value.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
}
class Builder {
public enum Color: String {
case black = "black"
case brown = "brown"
case green = "green"
case purple = "purple"
case yellow = "yellow"
case blue = "blue"
case gray = "gray"
case orange = "orange"
case red = "red"
case white = "white"
}
private static let BASE_URL = "https://maps.googleapis.com"
private static let BASE_API_KEY_PATH = "/maps/api/staticmap?size=%dx%d&key=%@"
private static let BASE_CLIENT_PATH = "/maps/api/staticmap?size=%dx%d&client=%@"
private static let DIVIDER_CHARACTER = GoogleStaticMapUrl.encodeValue("|")
private var apiKey:String?
private var client:String?
private var apiSecret:String?
private let width:Int
private let height:Int
private var scale:Int?
private var centerLocation:String?
private var centerLat:Double?
private var centerLng:Double?
private var zoom:Int?
private var markerLocations = [MarkerLocation]()
// private var paths = [Path]()
public init(width:Int, height:Int) {
self.width = width
self.height = height
}
class MarkerLocation {
public enum MarkerSize: String {
case tiny = "tiny"
case mid = "mid"
case small = "small"
}
let color:Color
let size:MarkerSize
var label:Character?
var location:String?
var lat:Double?
var lng:Double?
required init(color:Color, size:MarkerSize) {
self.color = color
self.size = size
}
convenience init(color:Color, size:MarkerSize, location:String) {
self.init(color:color, size:size)
self.location = location
}
convenience init(color:Color, size:MarkerSize, location:String, label:Character) {
self.init(color:color, size:size, location:location)
self.label = label
}
convenience init(color:Color, size:MarkerSize, lat:Double, lng:Double) {
self.init(color:color, size:size)
self.lat = lat
self.lng = lng
}
convenience init(color:Color, size:MarkerSize, lat:Double, lng:Double, label:Character) {
self.init(color:color, size:size, lat:lat, lng:lng)
self.label = label
}
public func toString() -> String {
var urlPart = ""
urlPart = urlPart.appendingFormat("color:%@", GoogleStaticMapUrl.encodeValue(self.color.rawValue))
urlPart = urlPart.appendingFormat("%@size:%@", DIVIDER_CHARACTER, GoogleStaticMapUrl.encodeValue(self.size.rawValue))
if let label = self.label {
urlPart = urlPart.appendingFormat("%@label:%@", DIVIDER_CHARACTER, GoogleStaticMapUrl.encodeValue(String(label)).uppercased())
}
if let lat = self.lat, let lng = self.lng {
urlPart = urlPart.appendingFormat("%@%f,%f", DIVIDER_CHARACTER, lat, lng)
}
else if let location = self.location {
urlPart = urlPart.appendingFormat("%@%@", DIVIDER_CHARACTER, GoogleStaticMapUrl.encodeValue(location))
}
return urlPart
}
}
func apiKey(_ apiKey:String) -> Builder {
self.apiKey = apiKey
return self
}
func client(_ client:String) -> Builder {
self.client = client
return self
}
func center(location:CLLocation) -> Builder {
self.centerLocation = "\(location.coordinate.latitude),\(location.coordinate.longitude)"
return self
}
func center(lat:Double, lng:Double) -> Builder {
self.centerLat = lat
self.centerLng = lng
return self
}
func scale(_ scale:Int) -> Builder {
self.scale = scale
return self
}
func zoom(_ zoom:Int) -> Builder {
self.zoom = zoom
return self
}
func apiSecret(_ apiSecret:String) -> Builder {
self.apiSecret = apiSecret
return self
}
func addMarkerLocation(_ markerLocation:MarkerLocation) -> Builder {
self.markerLocations.append(markerLocation)
return self
}
func toString() -> String {
var url = Builder.BASE_URL
let pathAndQuery = buildPathAndQueryString()
url.append(pathAndQuery)
return url
}
func buildPathAndQueryString() -> String {
var pathAndQuery = ""
if let client = self.client {
pathAndQuery = pathAndQuery.appendingFormat(Builder.BASE_CLIENT_PATH, width, height, client)
}
else if let apiKey = self.apiKey {
pathAndQuery = pathAndQuery.appendingFormat(Builder.BASE_API_KEY_PATH, width, height, apiKey)
}
if let scale = self.scale {
pathAndQuery = pathAndQuery.appendingFormat("&scale=%d", scale)
}
if let zoom = self.zoom {
pathAndQuery = pathAndQuery.appendingFormat("&zoom=%d", zoom)
}
if let lat = self.centerLat, let lng = self.centerLng {
pathAndQuery = pathAndQuery.appendingFormat("&center=%f,%f", lat, lng)
}
else if let location = self.centerLocation {
pathAndQuery = pathAndQuery.appendingFormat("&center=%@", GoogleStaticMapUrl.encodeValue(location))
}
if self.markerLocations.count > 0 {
self.markerLocations.forEach({ (markerLocation) in
pathAndQuery = pathAndQuery.appendingFormat("&markers=%@", markerLocation.toString())
})
}
return pathAndQuery
}
// TODO: Paths
// TODO: Url Signer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment