Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hqwlkj opened this issue Feb 7, 2020 · 12 comments
Open

Comments

@hqwlkj
Copy link

hqwlkj commented Feb 7, 2020

@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
Copy link

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

@corporale
Copy link

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

@hqwlkj
Copy link
Author

hqwlkj commented Apr 13, 2020

`
/// 初始化 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
Copy link

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

@jiangyh1024
Copy link

@corporale
是这样 ?
image

@corporale
Copy link

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

@jiangyh1024
Copy link

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

@corporale
Copy link

corporale commented May 17, 2020

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

@jiangyh1024
Copy link

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

@jiangyh1024
Copy link

@corporale
还是不行
NSLocationUsageDescription
用于定位使用
NSLocationAlwaysUsageDescription
用于始终定位你的当前位置
NSLocationWhenInUseUsageDescription
用于定位您的当前位置

是不是还要加上

NSLocationAlwaysAndWhenInUseUsageDescription
用于定位您的当前位置

@jiangyh1024
Copy link

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

@corporale
Copy link

@jiangyh1024 好的👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants