Commit 6dc5cb25 authored by Deployer's avatar Deployer

Added example for reverse geocoding

parent 767c85af
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = 'CedarMaps' s.name = 'CedarMaps'
s.version = '1.0.1' s.version = '1.0.2'
s.cocoapods_version = '>= 0.36' s.cocoapods_version = '>= 0.36'
s.license = 'MIT' s.license = 'MIT'
s.homepage = 'https://www.kikojas.com/about-cedarmaps' s.homepage = 'https://www.kikojas.com/about-cedarmaps'
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
@interface CSBookmarksViewController () <MGLMapViewDelegate> @interface CSBookmarksViewController () <MGLMapViewDelegate>
@property (nonatomic, strong) CSMapKit *mapKit;
@end @end
#pragma mark - CSViewController Implementation #pragma mark - CSViewController Implementation
...@@ -24,12 +26,12 @@ ...@@ -24,12 +26,12 @@
[super viewDidLoad]; [super viewDidLoad];
CSAuthenticationManager *auth = [CSAuthenticationManager sharedManager]; CSAuthenticationManager *auth = [CSAuthenticationManager sharedManager];
[auth setCredentialsWithClientID:@"clientID" [auth setCredentialsWithClientID:nil
clientSecret:@"clientSecret"]; clientSecret:nil];
CSMapKit *mapKit = [[CSMapKit alloc] initWithMapID:@"cedarmaps.streets"]; self.mapKit = [[CSMapKit alloc] initWithMapID:@"cedarmaps.streets"];
[mapKit styleURLWithCompletion:^(NSURL *url) { [self.mapKit styleURLWithCompletion:^(NSURL *url) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
self.mapView.styleURL = url; self.mapView.styleURL = url;
}); });
...@@ -43,13 +45,28 @@ ...@@ -43,13 +45,28 @@
{ {
[super viewDidAppear:animated]; [super viewDidAppear:animated];
// [self markBusStation];
// [self markTrainStation];
// [self markPointNumberOne];
// [self markPointNumberTwo];
// [self markPointNumberThree];
[self markBusStation]; [self.mapKit reverseGeocodingWithCoordinate:CLLocationCoordinate2DMake(35.770889877650724, 51.439468860626214) completion:^(NSDictionary *result, NSError *error) {
[self markTrainStation];
[self markPointNumberOne]; NSString *city = result[@"city"];
[self markPointNumberTwo]; NSString *locality = result[@"locality"];
[self markPointNumberThree]; NSString *address = result[@"address"];
dispatch_async(dispatch_get_main_queue(), ^{
MGLPointAnnotation *point = [[MGLPointAnnotation alloc] init];
point.coordinate = CLLocationCoordinate2DMake(35.770889877650724, 51.439468860626214);
point.title = [NSString stringWithFormat:@"%@، %@، %@", city, locality, address];
point.subtitle = nil;
[self.mapView addAnnotation:point];
});
}];
} }
#pragma mark #pragma mark
...@@ -62,6 +79,9 @@ ...@@ -62,6 +79,9 @@
#pragma mark - MGLMapViewDelegate Methods #pragma mark - MGLMapViewDelegate Methods
- (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id<MGLAnnotation>)annotation { - (MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id<MGLAnnotation>)annotation {
if (!annotation.subtitle) {
return nil;
}
MGLAnnotationImage *image = [MGLAnnotationImage annotationImageWithImage:[UIImage imageNamed:annotation.subtitle] reuseIdentifier:annotation.subtitle]; MGLAnnotationImage *image = [MGLAnnotationImage annotationImageWithImage:[UIImage imageNamed:annotation.subtitle] reuseIdentifier:annotation.subtitle];
return image; return image;
} }
......
...@@ -12,14 +12,14 @@ import CedarMaps ...@@ -12,14 +12,14 @@ import CedarMaps
class CSBookmarksViewController: UIViewController { class CSBookmarksViewController: UIViewController {
@IBOutlet weak var mapView: MGLMapView! @IBOutlet weak var mapView: MGLMapView!
let mapKit = CSMapKit(mapID: "cedarmaps.streets")
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
let auth = CSAuthenticationManager.shared() let auth = CSAuthenticationManager.shared()
auth?.setCredentialsWithClientID("clientID", clientSecret: "clientSecret") auth?.setCredentialsWithClientID(nil, clientSecret: nil)
let mapKit = CSMapKit(mapID: "cedarmaps.streets")
mapKit?.styleURL(completion: { (url) in mapKit?.styleURL(completion: { (url) in
DispatchQueue.main.async { DispatchQueue.main.async {
self.mapView.styleURL = url self.mapView.styleURL = url
...@@ -33,11 +33,28 @@ class CSBookmarksViewController: UIViewController { ...@@ -33,11 +33,28 @@ class CSBookmarksViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) { override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated) super.viewDidAppear(animated)
markBusStation() // markBusStation()
markTrainStation() // markTrainStation()
markPointNumberOne() // markPointNumberOne()
markPointNumberTwo() // markPointNumberTwo()
markPointNumberThree() // markPointNumberThree()
mapKit?.reverseGeocoding(with: CLLocationCoordinate2DMake(35.770889877650724, 51.439468860626214), completion: { (result, error) in
let city = result?["city"] as? String
let locality = result?["locality"] as? String
let address = result?["address"] as? String
DispatchQueue.main.async {
let point = MGLPointAnnotation()
point.coordinate = CLLocationCoordinate2DMake(35.770889877650724, 51.439468860626214)
point.title = "\(city ?? "شهر یافت نشد")، \(locality ?? "محله یافت نشد")، \(address ?? "آدرس یافت نشد")"
point.subtitle = nil
self.mapView.addAnnotation(point)
}
})
} }
@IBAction func attributionDidTouchUpInside(_ sender: UIButton?) { @IBAction func attributionDidTouchUpInside(_ sender: UIButton?) {
...@@ -49,7 +66,8 @@ class CSBookmarksViewController: UIViewController { ...@@ -49,7 +66,8 @@ class CSBookmarksViewController: UIViewController {
extension CSBookmarksViewController: MGLMapViewDelegate { extension CSBookmarksViewController: MGLMapViewDelegate {
func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? { func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {
let image = MGLAnnotationImage(image: UIImage(named: annotation.subtitle!!)!, reuseIdentifier: annotation.subtitle!!) guard let s1 = annotation.subtitle, let s2 = s1 else { return nil }
let image = MGLAnnotationImage(image: UIImage(named: s2)!, reuseIdentifier: annotation.subtitle!!)
return image return image
} }
...@@ -57,6 +75,7 @@ extension CSBookmarksViewController: MGLMapViewDelegate { ...@@ -57,6 +75,7 @@ extension CSBookmarksViewController: MGLMapViewDelegate {
return true return true
} }
fileprivate func markBusStation() { fileprivate func markBusStation() {
let annotation = MGLPointAnnotation() let annotation = MGLPointAnnotation()
annotation.coordinate = CLLocationCoordinate2DMake(35.770889877650724, 51.439468860626214) annotation.coordinate = CLLocationCoordinate2DMake(35.770889877650724, 51.439468860626214)
......
...@@ -215,7 +215,9 @@ ...@@ -215,7 +215,9 @@
NSDictionary *output = [NSDictionary dictionary]; NSDictionary *output = [NSDictionary dictionary];
if ([json.allKeys containsObject:@"result"]) { if ([json.allKeys containsObject:@"result"]) {
NSDictionary *result = [json objectForKey:@"result"]; NSDictionary *result = [json objectForKey:@"result"];
output = result; completion(result, nil);
} else {
completion(output, nil);
} }
} }
else { else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment