Flutter : Réseau et localisation

Retrouver l’adresse ip : https://pub.dev/packages/dart_ipify

Exemple main.dart :

import ‘package:dart_ipify/dart_ipify.dart’;
import ‘package:background_location/background_location.dart’;
import ‘package:flutter/material.dart’;

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

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State {
String latitude = ‘waiting…’;
String longitude = ‘waiting…’;
String altitude = ‘waiting…’;
String accuracy = ‘waiting…’;
String bearing = ‘waiting…’;
String speed = ‘waiting…’;
String time = ‘waiting…’;
String IpAdress =’…’;

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text(‘Background Location Service’),
),
body: Center(
child: ListView(
children: [
locationData(‘Latitude: ‘ + latitude),
locationData(‘Longitude: ‘ + longitude),
locationData(‘Altitude: ‘ + altitude),
locationData(‘Accuracy: ‘ + accuracy),
locationData(‘Bearing: ‘ + bearing),
locationData(‘Speed: ‘ + speed),
locationData(‘Time: ‘ + time),
locationData(‘IpAdress: ‘ + IpAdress),
ElevatedButton(
onPressed: () async {
await BackgroundLocation.setAndroidNotification(
title: ‘Background service is running’,
message: ‘Background location in progress’,
icon: ‘@mipmap/ic_launcher’,
);
//await BackgroundLocation.setAndroidConfiguration(1000);
await BackgroundLocation.startLocationService(
distanceFilter: 20);
BackgroundLocation.getLocationUpdates((location) {
setState(() {
latitude = location.latitude.toString();
longitude = location.longitude.toString();
accuracy = location.accuracy.toString();
altitude = location.altitude.toString();
bearing = location.bearing.toString();
speed = location.speed.toString();
time = DateTime.fromMillisecondsSinceEpoch(
location.time!.toInt())
.toString();
});
print( »’\n
Latitude: $latitude
Longitude: $longitude
Altitude: $altitude
Accuracy: $accuracy
Bearing: $bearing
Speed: $speed
Time: $time
IpAdress: $IpAdress
 »’);
});
},
child: Text(‘Start Location Service’)),
ElevatedButton(
onPressed: () {
BackgroundLocation.stopLocationService();
},
child: Text(‘Stop Location Service’)),
ElevatedButton(
onPressed: () {
getCurrentLocation();
},
child: Text(‘Get Current Location’)),
ElevatedButton(
onPressed: () {
GetIpAdress();
},
child: Text(‘Get IpAdress’)),
],
),
),
),
);
}

Widget locationData(String data) {
return Text(
data,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
textAlign: TextAlign.center,
);
}

void getCurrentLocation() {
BackgroundLocation().getCurrentLocation().then((location) {
print(‘This is current Location ‘ + location.toMap().toString());
});
}

@override
void dispose() {
BackgroundLocation.stopLocationService();
super.dispose();
}

void GetIpAdress() async {
IpAdress = await Ipify.ipv64(format: Format.JSON);
}
}

Avec android/app/src/Emain/AndroidManifest.xml :

Et puspec.yaml

dependencies:
flutter:
sdk: flutter
dart_ipify: ^1.1.1
background_location: ^0.6.1

Ne pas oublier d’augmenter la version minimum de 16 à 18 dans build.gradle d’android :

defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId « com.andrologiciels.flutter_iploc »
minSdkVersion 18
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}

Références :

https://pub.dev/packages/dart_ipify

Sur medium (utilisation de background_location)

https://fluttergems.dev/geolocation-maps/

Géolocalisation :

getCurrentLocationAddress(String latitude, String longitude) async {
try {
var lati = double.tryParse(latitude);
var longi = double.tryParse(longitude);
List listPlaceMarks = await placemarkFromCoordinates(
lati!, longi!);
Placemark place = listPlaceMarks[0];
setState(() {
currentLocationAddress = « ${place.locality}, ${place.postalCode}, ${place.country} »;
}
);
} catch (e) {
print(e);
}
}

Avec

import 'package:geocoding/geocoding.dart';

Références :

https://pub.dev/packages/geocoding

Le code source est ici

Articles récents
Commentaires récents
fatima dans Bienvenue !
AdminDroid dans Bienvenue !
fatima dans Bienvenue !
Archives
Catégories