簡體   English   中英

如何在flutter谷歌地圖網絡服務插件中獲取當前位置?

[英]How to get current Location in flutter google map web service plugin?

我希望在flutter應用程序中獲得具有緯度和經度的當前位置。 我使用谷歌地圖網絡服務插件。 但我無法在此插件中獲取用戶當前位置
我已經使用了另一個插件,但那是更多的錯誤顯示。 請給我一些開發此應用程序的建議。 我使用位置另一個插件但沒有工作

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'),
        )
      ],
    );
  }

}

您需要使用SetState函數來更新窗口小部件以反映$mapLat更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM