如何在Flutter Google Map Web Service插件中获取当前位置?

时间:2019-03-21 09:47:23

标签: android ios dart flutter

我想在Flutter应用中获取经纬度的当前位置。我使用了Google Map Web Service插件。但是我无法在此插件中获取用户的当前位置
我已经使用了另一个插件,但是更多的错误显示。 请给我一些开发此应用程序的建议。 我使用了另一个插件,但无法正常工作

import 'dart:io'
import 'package:flutter/material.dart';
import "package:google_maps_webservice/geocoding.dart";
import 'package:google_maps_webservice/geolocation.dart';



class MyLocation extends StatefulWidget {
  @override
  _MyLocationState createState() => _MyLocationState();
}

class _MyLocationState extends State<MyLocation> {


  final geocoding = new GoogleMapsGeocoding(apiKey: API_KEY);
  GeocodingResponse response;
//  final _geocoding = new GoogleMapsGeocoding(apiKey: API_KEY, httpClient: new BrowserClient());
//  final _1geocoding = new GoogleMapsGeocoding(baseUrl: "http://myProxy.com");

//  final geolocation = new GoogleMapsGeolocation(apiKey: Platform.environment[API_KEY]);
  final geolocation = new GoogleMapsGeolocation(apiKey: API_KEY);
  GeolocationResponse res;

  var mapLat, mapLng;

  @override
  void initState() {
    // TODO: implement initState

    initGocoding();
    super.initState();
  }
  Future<void> initGocoding()async {

    var params = {
      "considerIp": "false",
      "wifiAccessPoints": [
        {
          "macAddress": "00:25:9c:cf:1c:ac",
          "signalStrength": "-43",
          "signalToNoiseRatio": "0"
        },
        {
          "macAddress": "00:25:9c:cf:1c:ad",
          "signalStrength": "-55",
          "signalToNoiseRatio": "0"
        }
      ]
    };

    // No params -> google uses current location
//     res = await geolocation.getGeolocation();

//    // works with map/json
//    res = await geolocation.getGeolocationFromMap(params);
//
//    // define optional parameter explicit
    res = await geolocation.getGeolocation(considerIp: false);

    print(res.status);

    if (res.isOkay) {
       mapLat = res.location.lat;
       mapLng = res.location.lng;

      print("Latitude: ${res.location.lat}");
      print("Longitude: ${res.location.lng}");
      print("Accuracy: ${res.accuracy}");
    } else {
      print(res.errorMessage);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        Center(
          child: Text('My Location $mapLat : $mapLng'),
        )
      ],
    );
  }

}

1 个答案:

答案 0 :(得分:0)

您需要使用SetState函数来更新小部件以反映您的$mapLat的更改。