| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- #import "AmapLocationPlugin.h"
- @implementation AmapLocationPlugin{
- AMapLocationManager* _manager;
- FlutterMethodChannel* channel;
- }
- + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
- FlutterMethodChannel* channel = [FlutterMethodChannel
- methodChannelWithName:@"amap_location"
- binaryMessenger:[registrar messenger]];
- AmapLocationPlugin* instance = [[AmapLocationPlugin alloc] init];
- instance.channel = channel;
- [registrar addMethodCallDelegate:instance channel:channel];
- [registrar addApplicationDelegate:instance];
-
- }
- - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
- if ([@"getPlatformVersion" isEqualToString:call.method]) {
- result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
- } else if([@"startLocation" isEqualToString:call.method]){
- [self startLocation:call result:result];
- } else if([@"closeLocation" isEqualToString:call.method]){
- [self closeLocation:call result:result];
- }else {
- result(FlutterMethodNotImplemented);
- }
- }
- - (void)startLocation:(FlutterMethodCall*)call result:(FlutterResult)result{
- NSNumber* isOnceLocation = call.arguments[@"isOnceLocation"];
- NSNumber* isNeedAddress = call.arguments[@"isNeedAddress"];
- NSNumber* mockEnable = call.arguments[@"mockEnable"];
- // NSNumber* locationInterval = call.arguments[@"locationInterval"];
- NSNumber* locationTimeOut = call.arguments[@"locationTimeOut"];
- NSNumber* locationMode = call.arguments[@"locationMode"];
-
- NSLog(@"开始定位");
- _manager = [[AMapLocationManager alloc]init];
- _manager.delegate =self;
- _manager.detectRiskOfFakeLocation=mockEnable.boolValue;
-
- [_manager setLocatingWithReGeocode:isNeedAddress.boolValue];
- _manager.locationTimeout = locationTimeOut.intValue;
- if(locationMode.intValue ==0){
- [_manager setDesiredAccuracy:kCLLocationAccuracyThreeKilometers];
- }else if(locationMode.intValue ==1){
- [_manager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
- }else{
- [_manager setDesiredAccuracy:kCLLocationAccuracyBest];
- }
- if(isOnceLocation.boolValue){
- [_manager requestLocationWithReGeocode:isNeedAddress.boolValue completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
- [self handleResult:location regeocode:regeocode error:error];
-
- }];
- }else{
- [_manager startUpdatingLocation];
- }
- result(nil);
-
- }
- - (void)handleResult:(CLLocation *)location regeocode:(AMapLocationReGeocode *)regeocode error:(NSError *)error{
- if(error){
- NSLog(@"error:%@",error);
- [self.channel invokeMethod:@"location" arguments:@""];
- }else{
- NSMutableDictionary* result = [NSMutableDictionary dictionary];
- result[@"lon"] =@(location.coordinate.longitude);
- result[@"lat"] =@(location.coordinate.latitude);
- NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
- if (regeocode)
- {
- result[@"citycode"]=regeocode.citycode;
- result[@"adcode"]=regeocode.adcode;
- result[@"country"]=regeocode.country;
- result[@"province"]=regeocode.province;
- result[@"city"]=regeocode.city;
- result[@"district"]=regeocode.district;
- // result[@"road"]=regeocode.r;
- result[@"street"]=regeocode.street;
- result[@"number"]=regeocode.number;
- result[@"poiname"]=regeocode.POIName;
- result[@"errorCode"]=error==nil?nil:@(error.code);
- result[@"errorInfo"]=nil;
- // result[@"locationType"]=location.;
- // result[@"locationDetail"]=location.detail;
- result[@"aoiname"]=regeocode.AOIName;
- result[@"address"]=regeocode.formattedAddress;
- // result[@"poiid"]=location.poi;
- result[@"floor"]=[[NSString alloc]initWithFormat:@"%ld",(long)location.floor.level];
- result[@"description"]=regeocode.description;
- result[@"time"]=@(location.timestamp.timeIntervalSinceNow);
- result[@"provider"]=regeocode.province;
- // result[@"accuracy"]=location.accur;
- // result[@"isOffset"]=location.;
- NSLog(@"reGeocode:%@", regeocode);
- [self.channel invokeMethod:@"location" arguments:[self convertToJsonData:result]];
- }
- }
- }
- //格式化输出
- - (NSString *)convertToJsonData:(NSDictionary *)userInfo {
- NSError *error;
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:nil error:&error];
- NSString *jsonString=@"";
- if (!jsonData) {
- NSLog(@"%@", error);
- } else {
- jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- }
- return jsonString;
- }
- - (void)closeLocation:(FlutterMethodCall*)call result:(FlutterResult)result{
- NSLog(@"结束定位");
- if(_manager){
- _manager.delegate=nil;
- [_manager stopUpdatingLocation];
- _manager = nil;
-
- }
- result(nil);
- }
- - (void)amapLocationManager:(AMapLocationManager *)manager didFailWithError:(NSError *)error{
- NSLog(@"定位出现错误:%@",error);
-
- }
- - (void)amapLocationManager:(AMapLocationManager *)manager doRequireLocationAuth:(CLLocationManager*)locationManager{
- NSLog(@"申请权限");
- [locationManager requestAlwaysAuthorization];
- }
- - (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode{
- [self handleResult:location regeocode:reGeocode error:nil];
- }
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
- NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
- NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:bundlePath];
- NSString *gaodeAppKey = [infoDict objectForKey:@"GaoDeAppKey"];
- [AMapServices sharedServices].apiKey = gaodeAppKey;
- NSLog(@"配置高德地图定位");
-
- return YES;
-
- }
- @end
|