ios - Flutter 地理定位器插件 Geolocator().getCurrentPosition 不返回准确的 lng 和 lat 坐标

标签 ios google-maps flutter dart geolocation

我刚刚开始使用 flutter,我不知道为什么当我无法在 iOS 模拟器上使用地理定位器插件获得准确的坐标时。

它返回此值,是否为小数点后 1 位,并给出了不准确的位置

Lat: 1.3, Long: 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

目标:LatLng(_pos.latitude.toDouble(), _pos.longitude.toDouble()),

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

相关文章:

ios - 可重复使用的 TableViewCell 异步高度

iphone - 提醒应用程序 UI 是如何创建的?

ios - 适用于 iOS 的多个构建环境的 Firebase crashlytics

android - 如何在等待加载谷歌地图时防止卡住

android - 在应用程序中离线使用谷歌地图

flutter - 对齐属性在Flutter的ListView中不起作用

ios - 仅在 UITableView 的最后 2 部分添加渐变背景

java - 如何使用 Google Maps Android API 在 MapFragment 中绘制两点之间的路线

android - flutter ,云火存储查询方法,其中无法正常工作

android - Flutter Webview 抛出编译错误-ThreadedInputConnectionProxyAdapterView location : class InputAwareWebView