我刚开始颤抖,我不确定为什么我无法使用iOS模拟器上的geolocator插件获得准确的坐标。

它返回此值,它是小数点后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,
      ),
    );
  }
}

最佳答案

尝试通过将latlong解析为double

target: LatLng(_pos.latitude.toDouble(), _pos.longitude.toDouble()),

关于ios - Flutter geolocator插件Geolocator()。getCurrentPosition不返回准确的lng和lat坐标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59367977/

10-09 01:46