Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

dart - flutter - Using geolocator to obtain lon and lat

Been trying to use geolocator to get device location (lon and lat to be exact) and send them to another widget for api request but I can't use Geolocator.getCurrentPosition().longitude nor Geolocator.getCurrentPosition().latitude and here is my latest code.

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

/// Determine the current position of the device.
/// When the location services are not enabled or permissions
/// are denied the `Future` will return an error.

Future<Position> fetchPos(String input) async {
  //String input;
  bool serviceEnabled;
  LocationPermission permission;

  serviceEnabled = await Geolocator.isLocationServiceEnabled();
  if (!serviceEnabled) {
    return Future.error('Location services are disabled.');
  }

  permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.deniedForever) {
    return Future.error(
        'Location permissions are permantly denied, we cannot request permissions.');
  }

  if (permission == LocationPermission.denied) {
    permission = await Geolocator.requestPermission();
    if (permission != LocationPermission.whileInUse &&
        permission != LocationPermission.always) {
      return Future.error(
          'Location permissions are denied (actual value: $permission).');
    }
  }
  if (input == "lat") {
    return await Geolocator.getCurrentPosition();
  } else if (input == "lon") {
    return await Geolocator.getCurrentPosition();
  }
}

class GeoLoc extends StatefulWidget {
  String input;

  GeoLoc(this.input);

  @override
  _GeoLocState createState() => _GeoLocState(input);
}

class _GeoLocState extends State<GeoLoc> {
  //double _lat = 0;
  //double _lon = 0;
  String _output;
  Future<Position> futurePos;

  _GeoLocState(String input);
  @override
  void initState() {
    super.initState();
    futurePos = fetchPos("lat");
    if (widget.input == "lat") {
      _output = "${fetchPos("lat")}";
    } else if (widget.input == "lon") {
      _output = "${fetchPos("lon")}";
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: FutureBuilder<Position>(
          future: futurePos,
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return Text(
                snapshot.data.longitude.toString(),
              );
            } else if (snapshot.hasError){
              return Text("${snapshot.error}");
            }return CircularProgressIndicator();
          }),
    );
  }
}

this is where I want my lon and lat to be

 Future<Album> fetchAlbum() async {
  //TODO change 524901 with GPS 'api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={id}');
  final response = await http.get(
      'https://api.openweathermap.org/data/2.5/weather?id=524901&appid={id}');

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response,
    // then parse the JSON.
    return Album.fromJson(jsonDecode(response.body));
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load album');
  }
}

both are sperate files.

ps: there is some random nonsense just for testing purposes.

question from:https://stackoverflow.com/questions/65939829/flutter-using-geolocator-to-obtain-lon-and-lat

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...