-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathGeocodingPlugin.m
More file actions
100 lines (85 loc) · 4.29 KB
/
GeocodingPlugin.m
File metadata and controls
100 lines (85 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// GeocodingPlugin.m
// geocoding
//
// Created by Maurits van Beusekom on 07/06/2020.
//
#import "CLPlacemarkExtensions.h"
#import "GeocodingHandler.h"
#import "GeocodingPlugin.h"
@implementation GeocodingPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel
methodChannelWithName:@"flutter.baseflow.com/geocoding"
binaryMessenger:[registrar messenger]];
GeocodingPlugin* instance = [[GeocodingPlugin alloc] init];
[registrar addMethodCallDelegate:instance channel:channel];
}
+ (CGFloat) parseCGFloat:(NSString*)numberString {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle=NSNumberFormatterDecimalStyle;
return [[formatter numberFromString:numberString] doubleValue];
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"locationFromAddress" isEqualToString:call.method]) {
NSString* address = call.arguments[@"address"];
NSString* targetRegionSLat = call.arguments[@"targetRegionSLat"];
NSString* targetRegionNLat = call.arguments[@"targetRegionNLat"];
NSString* targetRegionWLng = call.arguments[@"targetRegionWLng"];
NSString* targetRegionELng = call.arguments[@"targetRegionELng"];
GeocodingHandler* handler = [[GeocodingHandler alloc] init];
[handler geocodeFromAddress:address
locale:[GeocodingPlugin parseLocale: call.arguments]
sLat:[GeocodingPlugin parseCGFloat: targetRegionSLat]
wLng:[GeocodingPlugin parseCGFloat: targetRegionWLng]
nLat:[GeocodingPlugin parseCGFloat: targetRegionNLat]
eLng:[GeocodingPlugin parseCGFloat: targetRegionELng]
success:^(NSArray<CLPlacemark *> * placemarks) {
result([GeocodingPlugin toLocationResult: placemarks]);
}
failure:^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {
result([FlutterError errorWithCode:errorCode
message:errorDescription
details:nil]);
}];
} else if ([@"placemarkFromCoordinates" isEqualToString:call.method]) {
CLLocationDegrees latitude = ((NSNumber *) call.arguments[@"latitude"]).doubleValue;
CLLocationDegrees longitude = ((NSNumber *) call.arguments[@"longitude"]).doubleValue;
CLLocation* location = [[CLLocation alloc] initWithLatitude:latitude
longitude:longitude];
GeocodingHandler* handler = [[GeocodingHandler alloc] init];
[handler geocodeToAddress:location
locale:[GeocodingPlugin parseLocale: call.arguments]
success:^(NSArray<CLPlacemark *> * placemarks) {
result([GeocodingPlugin toPlacemarkResult: placemarks]);
}
failure:^(NSString * _Nonnull errorCode, NSString * _Nonnull errorDescription) {
result([FlutterError errorWithCode:errorCode
message:errorDescription
details:nil]);
}];
} else {
result(FlutterMethodNotImplemented);
}
}
+ (NSLocale*) parseLocale:(NSDictionary*)arguments {
if (arguments[@"localeIdentifier"] == nil) {
return nil;
}
return [[NSLocale alloc] initWithLocaleIdentifier:(NSString*) arguments[@"localeIdentifier"]];
}
+ (NSArray<NSDictionary *> *) toLocationResult:(NSArray<CLPlacemark *> *)placemarks {
NSMutableArray<NSDictionary *> *result = [[NSMutableArray alloc] init];
for (CLPlacemark *placemark in placemarks) {
[result addObject:[placemark toLocationDictionary]];
}
return result;
}
+ (NSArray<NSDictionary *> *) toPlacemarkResult:(NSArray<CLPlacemark *> *)placemarks {
NSMutableArray<NSDictionary *> *result = [[NSMutableArray alloc] init];
for (CLPlacemark *placemark in placemarks) {
[result addObject:[placemark toPlacemarkDictionary]];
}
return result;
}
@end