Skip to content

Language support #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
11 changes: 7 additions & 4 deletions MapKitGoogleStyler/Classes/MapKit+GoogleStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ public enum MapKitGoogleStylerError: Error {
}

public struct MapKitGoogleStyler {
public static func buildOverlay(with jsonArray: [[String: Any]]) -> MKTileOverlay {
let mapStyle = MapStyle(json: jsonArray)
public static func buildOverlay(with jsonArray: [[String: Any]], langCode: String? = nil) -> MKTileOverlay {
var mapStyle = MapStyle(json: jsonArray)
if let langCodeSet = langCode {
mapStyle.langCode = langCodeSet
}
let overlay = MKTileOverlay(urlTemplate: mapStyle.urlString)
overlay.canReplaceMapContent = true
return overlay
}

public static func buildOverlay(with jsonFileURL: URL) throws -> MKTileOverlay {
public static func buildOverlay(with jsonFileURL: URL, langCode: String? = nil) throws -> MKTileOverlay {
let data = try Data(contentsOf: jsonFileURL)
let object = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
if let array = object as? [[String: Any]] {
return MapKitGoogleStyler.buildOverlay(with: array)
return MapKitGoogleStyler.buildOverlay(with: array, langCode: langCode)
} else {
throw MapKitGoogleStylerError.invalidJSONFormat
}
Expand Down
8 changes: 6 additions & 2 deletions MapKitGoogleStyler/Classes/MapStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import Foundation

internal struct MapStyle {

private let baseURL = "https://mts0.google.com/vt/lyrs=m@289000001&hl=en&src=app&x={x}&y={y}&z={z}&s=DGal"
var langCode = "en"
private let baseURL = "https://mts0.google.com/vt/lyrs=m@289000001&hl={lang}&src=app&x={x}&y={y}&z={z}&s=DGal"
private let prefix = "&apistyle="

internal var googleStyles = [GoogleStyle]()
Expand All @@ -30,12 +31,15 @@ internal struct MapStyle {
}
}
}

url = url.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""

url = url
.replacingOccurrences(of: ":", with: "%3A")
.replacingOccurrences(of: ",", with: "%2C")

return "\(baseURL)\(url)"
var urlPrefix = baseURL.replacingOccurrences(of: "{lang}", with: langCode)

return "\(urlPrefix)\(url)"
}
}