Mapbox Flutter:初始相机位置显示不正确的位置

Mapbox Flutter: Initial Camera Position Shows Incorrect Location

我正在使用 mapbox_gl, which is a Flutter plugin for access to Mapbox services. Application needs to display the initial camera position fed from user's current location acquired via geolocator 插件。在Android模拟器上,将当前位置设置为斯洛文尼亚卢布尔雅那的坐标(约46N,13E)显示刚果共和国地图,这显然是不正确的。

MapboxMap 小部件按以下方式构建:

class HomeScreen extends StatelessWidget {
  final String mapboxToken = 'XXX-TOKEN-XXX';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder(
        future: _acquireCurrentPosition(),
        builder: (BuildContext context, AsyncSnapshot snapshot) =>
            snapshot.hasData
                ? MapboxMap(
                    accessToken: mapboxToken,
                    minMaxZoomPreference: MinMaxZoomPreference(6.0, 15.0),
                    compassEnabled: false,
                    initialCameraPosition: CameraPosition(
                      target: snapshot.data,
                    ),
                  )
                : Center(
                    child: CircularProgressIndicator(),
                  ),
      ),
    );
  }
  
  Future<LatLng> _acquireCurrentPosition() async {
    Position position = await getCurrentPosition(
      desiredAccuracy: LocationAccuracy.high,
    );
    return LatLng(position.latitude, position.longitude);
  }
}

方法_acquireCurrentPosition()正确获取latitude/longitude组合,并在Android模拟器,iOS模拟器甚至物理Android设备(小米Redmi Note 8临)。即使使用不同的位置,初始相机位置的错误仍然存​​在。

非常感谢任何形式的帮助。

经过一些解决方法后,我设法使用以下步骤确定并解决了问题:

首先,删除地理定位器包并将其替换为 location 包,并在 Android 和 iOS 上明确声明位置权限,方法是在 AndroidManifest.xml[ 中提及它们=18=]

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Info.plist分别

<key>NSLocationWhenInUseUsageDescription</key>
<string>Shows your location on the map and helps improve the map</string>

接下来,您可以使用 onMapCreated 回调来代替通过 initialCameraPosition 参数设置初始相机位置:

// This is how you'd build the MapboxMap widget
MapboxMap(
  accessToken: mapboxToken,
  onMapCreated: (controller) {
    _acquireCurrentLocation().then((LatLong location) {
      if (location != null) {
        controller.animateCamera(
          CameraUpdate.newCameraPosition(
            CameraPosition(
              target: location,
            ),
          ),
        );
      }
    }).catchError((error) => print(error));
  },
  minMaxZoomPreference: MinMaxZoomPreference(6.0, 15.0),
  initialCameraPosition: CameraPosition(
    target: LatLng(45.45, 45.45),
  ),
);

// Method that uses location plugin
Future<LatLng> _acquireCurrentLocation() async {
  Location location = new Location();

  bool serviceEnabled;
  PermissionStatus permissionGranted;
  LocationData locationData;

  serviceEnabled = await location.serviceEnabled();
    if (!serviceEnabled) {
      serviceEnabled = await location.requestService();
      if (!serviceEnabled) {
        return null;
      }
    }

  permissionGranted = await location.hasPermission();
  if (permissionGranted == PermissionStatus.denied) {
    permissionGranted = await location.requestPermission();
    if (permissionGranted != PermissionStatus.granted) {
      return null;
    }
  }

  locationData = await location.getLocation();
  return LatLng(locationData.latitude, locationData.longitude);
}

我个人的建议是将 Android 的最低 SDK 版本更改为 23。