Skip to content
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

Add support for reading simple ExtendedData tag #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions Source/KML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public enum KMLTag: String {
case Folder
case Placemark
case Icon
case ExtendedData
case Data

public var str: String {
return self.rawValue
Expand All @@ -50,7 +52,9 @@ public struct KMLConfig {
KMLTag.Folder.str: KMLElement.self,
KMLTag.Placemark.str: KMLPlacemark.self,
KMLTag.Icon.str: KMLIcon.self,
KMLTag.IconStyle.str: KMLIconStyle.self
KMLTag.IconStyle.str: KMLIconStyle.self,
KMLTag.ExtendedData.str: KMLExtendedData.self,
KMLTag.Data.str: KMLData.self
]
}

Expand All @@ -77,8 +81,9 @@ open class KMLElement {
let lines: [String] = element.string.components(separatedBy: CharacterSet.whitespacesAndNewlines)
for line in lines {
let points: [String] = line.components(separatedBy: ",")
assert(points.count >= 2, "points lenth is \(points)")
coordinates.append(CLLocationCoordinate2DMake(atof(points[1]), atof(points[0])))
if points.count >= 2 {
coordinates.append(CLLocationCoordinate2DMake(atof(points[1]), atof(points[0])))
}
}
return coordinates
}
Expand Down Expand Up @@ -390,6 +395,7 @@ open class KMLPlacemark: KMLElement {
open var lineString: KMLLineString?
open var polygon: KMLPolygon?
open var style: KMLStyle?
open var extendedData: KMLExtendedData?

public required init(_ element: AEXMLElement) {
let style = element["styleUrl"].string
Expand All @@ -409,6 +415,43 @@ open class KMLPlacemark: KMLElement {
} else if let polygonGeometry = findElement(KMLPolygon.self) {
polygon = polygonGeometry
}

extendedData = findElement(KMLExtendedData.self)
}
}

// MARK: - Extended Data

open class KMLExtendedData: KMLElement {
open var data: [String: KMLData] = [:]

public required init(_ element: AEXMLElement) {
super.init(element)
for dataElement in findElements(KMLData.self) {
self.data[dataElement.dataName] = dataElement
}
}
}

open class KMLData: KMLElement {
open var dataName: String = ""
open var displayName: String?
open var value: String?

public required init(_ element: AEXMLElement) {
self.dataName = element.attributes["name"] ?? ""

for child: AEXMLElement in element.children {
switch child.name {
case "displayName":
displayName = child.string
case "value":
value = child.string
default:
break
}
}
super.init(element)
}
}

Expand All @@ -424,6 +467,7 @@ open class KMLAnnotation: NSObject, MKAnnotation {
open var title: String?
open var subtitle: String?
open var style: KMLStyle?
open var placemark: KMLPlacemark?

init(_ coordinate: CLLocationCoordinate2D) {
self.coordinate = coordinate
Expand Down Expand Up @@ -580,6 +624,7 @@ open class KMLDocument: KMLElement {
annotation.title = pointPlacemark.name
annotation.subtitle = pointPlacemark.description
annotation.style = pointPlacemark.style
annotation.placemark = pointPlacemark

self.annotations.append(annotation)
}
Expand Down