“Как получить местоположение в Flutter” Ответ

Расположение Керрунта в трепете

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

void main() => runApp(MapScreen());

class MapScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "Map",
      home: MapActivity(),
    );
  }
}

class MapActivity extends StatefulWidget {
  @override
  _MapActivityState createState() => _MapActivityState();
}

class _MapActivityState extends State<MapActivity> {
  LatLng _center ;
  Position currentLocation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getUserLocation();
  }

 Future<Position> locateUser() async {
    return Geolocator()
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
  }

  getUserLocation() async {
    currentLocation = await locateUser();
    setState(() {
      _center = LatLng(currentLocation.latitude, currentLocation.longitude);
    });
    print('center $_center');
  }
}
Modern Mantis

Как получить местоположение в Flutter

 add these in android/app/main/AndroidManifest.xml

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
//------------------------------------------------------------
import 'package:http/http.dart' as http;
//------------------------------------------------------------
void getData() async {
    Uri url = Uri.parse(
        'https://api.openweathermap.org/data/2.5/weather?lat=55&lon=180&appid=YOUR_API_KEY');
    http.Response response = await http.get(url);
    print(response.body);
  }
 
  @override
  Widget build(BuildContext context) {
    getData();
 
    return Scaffold(
      body: Container(),
    );
  }
//--------------------------------------------------------- 
  // you can get IPA key form OpenWeather
  //https://openweathermap.org/current
CodeFather

Ответы похожие на “Как получить местоположение в Flutter”

Вопросы похожие на “Как получить местоположение в Flutter”

Больше похожих ответов на “Как получить местоположение в Flutter” по Dart

Смотреть популярные ответы по языку

Смотреть другие языки программирования