Flutter geolocator插件Geolocator().getCurrentPosition不返回准确的lng和lat坐标

3pvhb19x  于 2023-04-22  发布在  Flutter
关注(0)|答案(2)|浏览(375)

我刚开始在Flutter,我不知道为什么当我不能得到准确的坐标使用地理定位器插件在iOS模拟器。
它返回这个,是1个小数点,并给出了一个不准确的位置
横向:1.3,纵向:103.8
我怎么才能让它把这个退掉呢?
1.3825983,103.8814173

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geolocator/geolocator.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

class Home extends StatefulWidget {
  Home(this.listType);
  final String listType;

  @override
  _HomeState createState() => new _HomeState();
}

class _HomeState extends State<Home> {
  GoogleMapController _mapController;
  // final LatLng _center = const LatLng(1.290270, 103.851959);
  Position _pos;
  Widget _mapWidget; 

  void _onMapCreated(GoogleMapController controller) {
    _mapController = controller;
  }

  @override
  void initState() {
    _mapWidget = SpinKitRotatingCircle(
      color: Colors.white,
      size: 50.0,
    );
    getCurrentLocation();
    super.initState();
  }

  void getCurrentLocation() async {
    GeolocationStatus geolocationStatus  = await Geolocator().checkGeolocationPermissionStatus();
    print(geolocationStatus);

    Position currPos = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.best);
    print(currPos);

    setState(() {
      _pos = currPos;
      _mapWidget = initMapWidget();
    });
  }

  Widget initMapWidget () {
    return GoogleMap(
      mapType: MapType.normal,
      onMapCreated: _onMapCreated,
      myLocationEnabled: true,
      initialCameraPosition: CameraPosition(
        target: LatLng(_pos.latitude, _pos.longitude),
        zoom: 15.0,
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: _mapWidget,
      ),
    );
  }
}
dluptydi

dluptydi1#

尝试将latlong解析为double
target: LatLng(_pos.latitude.toDouble(), _pos.longitude.toDouble()),

umuewwlo

umuewwlo2#

尝试将定位精度标记为高

Position currPos = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
print(currPos);

相关问题