簡體   English   中英

如何解決 Flutter 中的地理定位問題

[英]How do I solve Problem with Geolocation in Flutter

我嘗試使用地理定位服務構建應用程序

這是代碼:

import 'package:brew_two/services/auth.dart';
import 'package:flutter/material.dart';
import 'package:geolocation/geolocation.dart';
import 'package:flutter_map/flutter_map.dart';


class Home extends StatelessWidget {

 MapController controller = new MapController();

 getPermission() async {
  final GeolocationResult result =
   await Geolocation.requestLocationPermission(
   const LocationPermission(
    android: LocationPermissionAndroid.fine,
    ios : LocationPermissionIOS.always));
  return result;
 }

 getLocation() {
 return getPermission().then((result) async {
  if(result.isSuccessful){
  final coords = await Geolocation.currentLocation(
    accuracy: LocationAccuracy.best);
  }
 });
 }

 buildMap(){
 getLocation().then((response) {
  if(response.isSuccessful){
    response.listen((value){
      controller.move(
        new LatLng(value.location.lattitude, value.location.longtiude),
        15.0);
    });
  }
  });
 }



final AuthService _auth = AuthService();

@override
Widget build(BuildContext context) {
return Scaffold(
  backgroundColor: Colors.brown[50],
  appBar: AppBar(
    title: Text('Brew Crew'),
    backgroundColor: Colors.brown[400],
    elevation: 0.0,
    actions: <Widget>[
      FlatButton.icon(
        onPressed: () async {
          await _auth.signOut();
        },
        icon: Icon(Icons.person), 
        label: Text('Logout'))
    ],
  ),
  body: new FlutterMap(
    mapController: controller,
    options: new MapOptions(center: buildMap(), minZoom: 5.0),
    layers: [
      new TileLayerOptions(
        urlTemplate:
        "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
        subdomains: ['a', 'b', 'c']),
    ]),
  );
  }
 }

但我在這里面臨以下問題:

await Geolocation.requestLocationPermission(
   const LocationPermission(
    android: LocationPermissionAndroid.fine,
    ios : LocationPermissionIOS.always));

位置 arguments 太多:預期為 0,但找到了 1。 嘗試刪除額外的位置 arguments,或指定名為 arguments 的名稱。

LatLng function 的另一個問題:

new LatLng(value.location.lattitude, value.location.longtiude),

構造函數返回不是預期類型“LatLng”的“動態”類型。 未定義 class 'LatLng'。 嘗試將名稱更改為現有 class 的名稱,或創建名稱為“LatLng”的 class。

有人能幫助我嗎?

對於 LatLng 問題,請導入 latlong 包

import "package:latlong/latlong.dart";

對於權限問題,請使用下面給出的權限

getPermission() async {
    final GeolocationResult result =
        await Geolocation.requestLocationPermission(
            permission: const LocationPermission(
                android: LocationPermissionAndroid.fine,
                ios: LocationPermissionIOS.always));
    return result;
}

暫無
暫無

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

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