【问题标题】:how to change google map API's My Location button postion in flutter?如何在颤动中更改谷歌地图 API 我的位置按钮位置?
【发布时间】:2019-08-11 21:00:23
【问题描述】:

我正在使用波纹管小部件,我想将 MyLocation 按钮更改为地图视图中的底部位置

GoogleMap(

                  onMapCreated: onMapCreated,

              options: GoogleMapOptions(


                    myLocationEnabled :true, 
                    mapType: MapType.hybrid,

                  cameraPosition: CameraPosition(
                    target: LatLng(6.8814635,79.8876697),
                    zoom: 15.0,),
                  ),
    ),

【问题讨论】:

    标签: google-maps flutter


    【解决方案1】:

    我找到了解决方案。我创建 FAB 按钮来更改“我的位置”按钮

    GoogleMap(
            mapType: MapType.hybrid,
            initialCameraPosition: _kGooglePlex,
            onMapCreated: (GoogleMapController controller) {
              _controller.complete(controller);
            },
            myLocationEnabled: true,
          ),
          floatingActionButton: FloatingActionButton.extended(
            onPressed: _currentLocation,
            label: Text('My Location'),
            icon: Icon(Icons.location_on),
          ),
        );
      }
    

    另外,设置_currentLocation 方法来调用我的位置

    void _currentLocation() async {
       final GoogleMapController controller = await _controller.future;
       LocationData currentLocation;
       var location = new Location();
       try {
         currentLocation = await location.getLocation();
         } on Exception {
           currentLocation = null;
           }
    
        controller.animateCamera(CameraUpdate.newCameraPosition(
          CameraPosition(
            bearing: 0,
            target: LatLng(currentLocation.latitude, currentLocation.longitude),
            zoom: 17.0,
          ),
        ));
      }
    

    【讨论】:

    • 按下 FAB 会显示屏幕上的当前位置,但它会转到您当前的位置吗?
    • 是的。它会。 animateCamera 的代码就是为它准备的
    • 我找不到 LocationData 类,它来自哪个?
    • @funder7 安装位置包以获得pub.dev/packages/location/install
    • 这可行,但速度很慢,我通过始终使用 location.onLocationChanged.listen((event) { myLocation = event; }); 更新位置来解决它而不是使用 myLocation = location.getLocation();每次我按下按钮
    【解决方案2】:

    使用 GoogleMap 小部件的 padding 属性

    return Stack(
      children: <Widget>[
        GoogleMap(
          onMapCreated: _onMapCreated,
          initialCameraPosition: CameraPosition(
            target: _center,
            zoom: 11.0,
          ),
          mapType: _currentMapType,
          markers: _markers,
          onCameraMove: _onCameraMove,
          myLocationEnabled: true,
          padding: EdgeInsets.only(top: 40.0,),
        ),
      ],
    );
    

    image 1: without padding

    image 2: with padding

    【讨论】:

    • 虽然这确实会移动按钮,但它也会移动地图的中心,因此按下按钮可能不会达到预期的效果
    【解决方案3】:

    只需在顶部添加填充! 就我而言;

    padding: EdgeInsets.only(top: 155.0),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-30
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多