【问题标题】:How to programatically getting current users location to load on a map in flutter如何以编程方式获取当前用户位置以在颤振中加载到地图上
【发布时间】:2020-06-28 06:11:03
【问题描述】:

请帮忙。我试图让 initialCameraPosition 以编程方式在地图上显示用户的当前位置。我不想硬编码。 我有一个 getCurrentLocation 方法,但我似乎无法将结果(其纬度和经度)用作地图的 initialCameraPosition 的纬度和经度。

getCurrentLocation 方法是

Future<Position> getCurrentPosition() async {
Position position = await Geolocator()
    .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
print(position);
return position;

}

Widget build(BuildContext context) {
return Scaffold(
  backgroundColor: Colors.white,
  body: Stack(
    children: <Widget>[
      GoogleMap(
        initialCameraPosition:
            CameraPosition(target: LatLng(9.0765, 7.3986), zoom: 17),//these coordinates should not be hard coded
        onMapCreated: _onMapCreated,
        myLocationEnabled: true,
        mapType: MapType.normal,
        markers: _marker,
      ),}

【问题讨论】:

    标签: flutter dart geolocation google-maps-flutter


    【解决方案1】:

    等待加载用户当前位置;

    1 从 init() 中点击 getCurrentPosition() 就像

    Position position;
    
      @override
      void initState() {
        // TODO: implement initState
        super.initState();
        getCurrentPosition()
      }
    
      getCurrentPosition() async 
        {
        position = await Geolocator()
                .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    
        setState((){});
         
        }
    

    2。现在你构建的函数会是这样的:

    Widget build(BuildContext context) {
        return Scaffold(
            backgroundColor: Colors.white,
            body: position==null?CircularProgressIndicator(): Stack(
                children: <Widget>[
            GoogleMap( initialCameraPosition: CameraPosition(target:position.getLatLng, zoom: 17),
    
              onMapCreated: _onMapCreated,
              myLocationEnabled: true,
              mapType: MapType.normal,
              markers: _marker,
    
        )]));
    
      }
    

    【讨论】:

    • 我做到了,但他们告诉我我的声誉仍然很低,这就是它没有出现的原因。请帮我解决这个问题。 stackoverflow.com/questions/62668759/…
    • 您的问题解决了吗? (好吧,那不用担心。你也可以点击Tick(关于upvote))
    • ✅ 完成。我无法解决链接上的问题。它不是在查询
    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    相关资源
    最近更新 更多