best-flutter / flutter_amap_location

高德地图flutter定位组件
MIT License
274 stars 70 forks source link

android 定位没有问题,iOS定位失败获取不到位置信息 #42

Open hqwlkj opened 4 years ago

hqwlkj commented 4 years ago
@override
  void initState() {
    super.initState();
    _type = widget.type;
    WidgetsBinding.instance.addPostFrameCallback((call) async {
      /// !注意: 只要是返回Future的方法, 一律使用`await`修饰, 确保当前方法执行完成后再执行下一行,
      /// 在不能使用`await`修饰的环境下, 在`then`方法中执行下一步.
      /// 初始化 iOS在init方法中设置, android需要去AndroidManifest.xml里去设置, 详见 https://lbs.amap.com/api/android-sdk/gettingstarted
      if (Platform.isIOS) {
        LogUtil.e('', tag: 'IOS初始化高德地图API:');
        await AMapLocationClient.setApiKey('这是appKey').then((boo){
          LogUtil.e('$boo', tag: 'IOS初始化高德地图API返回值:');
        });
      }
      // 单次定位
      if (await _requestPermission()) {
        //先启动一下
        bool boo = await AMapLocationClient.startup(new AMapLocationOption(
            desiredAccuracy: CLLocationAccuracy.kCLLocationAccuracyHundredMeters));
        LogUtil.e('$boo', tag: '启动高德地图API返回值:');
        // 直接获取定位:
//        await AMapLocationClient.getLocation(true);

        // 监听定位
        AMapLocationClient.onLocationUpate.listen((AMapLocation loc) {
          LogUtil.e('$mounted', tag: 'IOS初始化高德地图API - mounted:');
          LogUtil.e('$loc', tag: 'IOS初始化高德地图API - loc:');
          if (!mounted) return;
          setState(() {
            _location = loc;
          });
        });

        AMapLocationClient.startLocation();
      } else {
        Utils.showToast('定位失败!');
      }
    });
  }

iOS环境下完全没有执行到监听,android环境没有问题。是不是我哪里少了配置。

采用直接获取定位也是获取不到位置信息的


   AMapLocationClient.getLocation(true).then((AMapLocation loc) {
        LogUtil.e('$mounted', tag: '高德地图API - mounted:');
        LogUtil.e('$loc', tag: '高德地图API - loc:');
        if (!mounted) return;
        setState(() {
          _location = loc;
        });
   });
x2201783 commented 4 years ago

用ios调试,看看- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result 里面的else if([@"setApiKey" isEqualToString:method]) 有没有触发

corporale commented 4 years ago

把高德的key写在iOS工程里的AppDelegate就可以获取位置信息 https://lbs.amap.com/api/ios-sdk/guide/create-project/note

hqwlkj commented 4 years ago

` /// 初始化 iOS在init方法中设置, android需要去AndroidManifest.xml里去设置, 详见 https://lbs.amap.com/api/android-sdk/gettingstarted

  if (Platform.isIOS) {
    LogUtil.e('', tag: 'IOS初始化高德地图API:');
    await AMapLocationClient.setApiKey('这是appKey').then((boo){
      LogUtil.e('$boo', tag: 'IOS初始化高德地图API返回值:');
    });
  }

`

我最后处理是在 runapp 函数调用前加上以上代码就可以了。

jiangyh1024 commented 4 years ago

@hqwlkj 解决吗 我也遇到了问题 我再 runApp之前调用也是 定位失败 image

jiangyh1024 commented 4 years ago

@corporale 是这样 ? image

corporale commented 4 years ago

@jiangyh1024 差不多,我是红框的那句放在前面

jiangyh1024 commented 4 years ago

@corporale 红框那个就这么些就行? 不需要import 什么东西么

corporale commented 4 years ago

@jiangyh1024 是的,我加了这句代码就可以获取到定位信息了,如果报错就 import AMapFoundationKit

jiangyh1024 commented 4 years ago

@corporale OK 谢了 明天上班我试试

jiangyh1024 commented 4 years ago

@corporale 还是不行

NSLocationUsageDescription
<string>用于定位使用</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>用于始终定位你的当前位置</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>用于定位您的当前位置</string>

是不是还要加上

NSLocationAlwaysAndWhenInUseUsageDescription
<string>用于定位您的当前位置</string>
jiangyh1024 commented 4 years ago

@corporale 行了 确实要加上最后那个

corporale commented 4 years ago

@jiangyh1024 好的👌