Skip to content

Commit

Permalink
Merge pull request #43 from mapbox/1ec5-public-placemark
Browse files Browse the repository at this point in the history
Took GeocodedPlacemark, QualifyingPlacemark public
  • Loading branch information
1ec5 committed May 22, 2016
2 parents 38dc075 + a137fb0 commit 6759a16
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Geocoder Example/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ - (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
[self.geocodingDataTask cancel];
MBReverseGeocodeOptions *options = [[MBReverseGeocodeOptions alloc] initWithCoordinate:self.mapView.centerCoordinate];
[self.geocoder geocodeWithOptions:options completionHandler:^(NSArray<MBPlacemark *> * _Nullable placemarks, NSString * _Nullable attribution, NSError * _Nullable error) {
[self.geocoder geocodeWithOptions:options completionHandler:^(NSArray<MBGeocodedPlacemark *> * _Nullable placemarks, NSString * _Nullable attribution, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error);
} else if (placemarks.count > 0) {
Expand Down
6 changes: 3 additions & 3 deletions MapboxGeocoder/MBGeocoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class Geocoder: NSObject {
- parameter attribution: A legal notice indicating the source, copyright status, and terms of use of the placemark data.
- parameter error: The error that occurred, or `nil` if the placemarks were obtained successfully.
*/
public typealias CompletionHandler = (placemarks: [Placemark]?, attribution: String?, error: NSError?) -> Void
public typealias CompletionHandler = (placemarks: [GeocodedPlacemark]?, attribution: String?, error: NSError?) -> Void

/**
A closure (block) to be called when a geocoding request is complete.
Expand All @@ -64,7 +64,7 @@ public class Geocoder: NSObject {
- parameter attributionsByQuery: An array of legal notices indicating the sources, copyright statuses, and terms of use of the placemark data for each query.
- parameter error: The error that occurred, or `nil` if the placemarks were obtained successfully.
*/
public typealias BatchCompletionHandler = (placemarksByQuery: [[Placemark]]?, attributionsByQuery: [String]?, error: NSError?) -> Void
public typealias BatchCompletionHandler = (placemarksByQuery: [[GeocodedPlacemark]]?, attributionsByQuery: [String]?, error: NSError?) -> Void

/**
The shared geocoder object.
Expand Down Expand Up @@ -153,7 +153,7 @@ public class Geocoder: NSObject {
let url = URLForGeocoding(options: options)
let task = dataTaskWithURL(url, completionHandler: { (json) in
let featureCollections = json as! [JSONDictionary]
let placemarksByQuery = featureCollections.map { (featureCollection) -> [Placemark] in
let placemarksByQuery = featureCollections.map { (featureCollection) -> [GeocodedPlacemark] in
assert(featureCollection["type"] as? String == "FeatureCollection")
let features = featureCollection["features"] as! [JSONDictionary]
return features.flatMap { GeocodedPlacemark(featureJSON: $0) }
Expand Down
36 changes: 19 additions & 17 deletions MapboxGeocoder/MBPlacemark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,8 @@ public class Placemark: NSObject, NSCopying, NSSecureCoding {
/**
A concrete subclass of `Placemark` to represent results of geocoding requests.
*/
internal class GeocodedPlacemark: Placemark {
@objc(MBGeocodedPlacemark)
public class GeocodedPlacemark: Placemark {
private let propertiesJSON: JSONDictionary

override init(featureJSON: JSONDictionary) {
Expand All @@ -352,24 +353,24 @@ internal class GeocodedPlacemark: Placemark {
superiorPlacemarks = contextJSON?.map { QualifyingPlacemark(featureJSON: $0) }
}

override func copyWithZone(zone: NSZone) -> AnyObject {
public override func copyWithZone(zone: NSZone) -> AnyObject {
return GeocodedPlacemark(featureJSON: featureJSON)
}

override var debugDescription: String {
public override var debugDescription: String {
return qualifiedName
}

override var qualifiedName: String! {
public override var qualifiedName: String! {
return featureJSON["place_name"] as! String
}

override var location: CLLocation {
public override var location: CLLocation {
let centerCoordinate = CLLocationCoordinate2D(geoJSON: featureJSON["center"] as! [Double])
return CLLocation(coordinate: centerCoordinate)
}

override var region: RectangularRegion? {
public override var region: CLRegion? {
guard let boundingBox = featureJSON["bbox"] as? [Double] else {
return nil
}
Expand All @@ -380,7 +381,7 @@ internal class GeocodedPlacemark: Placemark {
return RectangularRegion(southWest: southWest, northEast: northEast)
}

override var name: String {
public override var name: String {
let text = super.name

// For address features, `text` is just the street name. Look through the fully-qualified address to determine whether to put the house number before or after the street name.
Expand All @@ -397,19 +398,19 @@ internal class GeocodedPlacemark: Placemark {
}
}

override var code: String? {
public override var code: String? {
return (propertiesJSON["short_code"] as? String)?.uppercaseString
}

override var wikidataItemIdentifier: String? {
public override var wikidataItemIdentifier: String? {
let item = propertiesJSON["wikidata"] as? String
if let item = item {
assert(item.hasPrefix("Q"))
}
return item
}

override var genres: [String]? {
public override var genres: [String]? {
let categoryList = propertiesJSON["category"] as? String
return categoryList?.componentsSeparatedByString(", ")
}
Expand All @@ -420,7 +421,7 @@ internal class GeocodedPlacemark: Placemark {
}

@available(iOS 9.0, *)
override var postalAddress: CNPostalAddress? {
public override var postalAddress: CNPostalAddress? {
let postalAddress = CNMutablePostalAddress()

if scope == .Address {
Expand Down Expand Up @@ -448,7 +449,7 @@ internal class GeocodedPlacemark: Placemark {
return postalAddress
}

override var addressDictionary: [NSObject: AnyObject]? {
public override var addressDictionary: [NSObject: AnyObject]? {
var addressDictionary: [String: AnyObject] = [:]
if scope == .Address {
addressDictionary[MBPostalAddressStreetKey] = name
Expand All @@ -473,24 +474,25 @@ internal class GeocodedPlacemark: Placemark {
/**
The phone number to contact a business at this location.
*/
override var phoneNumber: String? {
public override var phoneNumber: String? {
return propertiesJSON["tel"] as? String
}
}

/**
A concrete subclass of `Placemark` to represent entries in a `GeocodedPlacemark` object’s `superiorPlacemarks` property. These entries are like top-level geocoding results, except that they lack location information and are flatter, with properties directly at the top level.
*/
private class QualifyingPlacemark: Placemark {
override func copyWithZone(zone: NSZone) -> AnyObject {
@objc(MBQualifyingPlacemark)
public class QualifyingPlacemark: Placemark {
public override func copyWithZone(zone: NSZone) -> AnyObject {
return QualifyingPlacemark(featureJSON: featureJSON)
}

override var code: String? {
public override var code: String? {
return (featureJSON["short_code"] as? String)?.uppercaseString
}

override var wikidataItemIdentifier: String? {
public override var wikidataItemIdentifier: String? {
let item = featureJSON["wikidata"] as? String
if let item = item {
assert(item.hasPrefix("Q"))
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ options.focalLocation = [[CLLocation alloc] initWithLatitude:45.3 longitude:-66.
options.allowedScopes = MBPlacemarkScopeAddress | MBPlacemarkScopePointOfInterest;

NSURLSessionDataTask *task = [geocoder geocodeWithOptions:options
completionHandler:^(NSArray<MBPlacemark *> * _Nullable placemarks,
completionHandler:^(NSArray<MBGeocodedPlacemark *> * _Nullable placemarks,
NSString * _Nullable attribution,
NSError * _Nullable error) {
MBPlacemark *placemark = placemarks[0];
Expand Down Expand Up @@ -149,7 +149,7 @@ MBReverseGeocodeOptions *options = [[MBReverseGeocodeOptions alloc] initWithCoor
// Or perhaps: [[MBReverseGeocodeOptions alloc] initWithLocation:locationManager.location]

NSURLSessionDataTask *task = [geocoder geocodeWithOptions:options
completionHandler:^(NSArray<MBPlacemark *> * _Nullable placemarks,
completionHandler:^(NSArray<MBGeocodedPlacemark *> * _Nullable placemarks,
NSString * _Nullable attribution,
NSError * _Nullable error) {
MBPlacemark *placemark = placemarks[0];
Expand Down

0 comments on commit 6759a16

Please sign in to comment.