location_plus 1.0.6 location_plus: ^1.0.6 copied to clipboard
Plugin to get current location, ip address, geocoding, reverse geocoding, and you can also calculate the distance between two coordinates easily
import 'dart:convert';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:location_plus/location_plus.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
static const _channel = MethodChannel("location_plus");
//calculateDistanceBetweenTwoCoordinates
String? _distance = "0";
//getCurrentLocation
String? _locality = "unknown";
String? _postalCode = "unknown";
String? _administrativeArea = "unknown";
String? _country = "unknown";
double? _latitude = 0;
double? _longitude = 0;
String? _ipAddress = "0";
//getAddressFromLatLon
String? _addressFromLatLng = "";
//getLatLngFromAddress
double? _latFromAddress = 0;
double? _lngFromAddress = 0;
Future<void> calculateDistanceBetweenTwoCoordinates(double startLatitude,
double startLongitude, double endLatitude, double endLongitude) async {
_distance = await _channel.invokeMethod(
'calculateDistanceBetweenTwoCoordinates', <String, double>{
'startLatitude': startLatitude,
'startLongitude': startLongitude,
'endLatitude': endLatitude,
'endLongitude': endLongitude,
});
setState(() {
print("distancee: ${_distance}");
});
}
Future<dynamic> getCurrentLocation() async {
var result = await LocationPlus.getCurrentLocation();
setState(() {
print(result);
_locality = result['locality'];
_postalCode = result['postalCode'];
_administrativeArea = result['administrativeArea'];
_country = result['country'];
_latitude = double.parse(result['latitude']);
_longitude = double.parse(result['longitude']);
_ipAddress = result['ipAddress'];
});
}
Future<void> getAddressFromLatLong(double latitude, double longitude) async {
_addressFromLatLng = await _channel.invokeMethod('getAddressFromLatLong',
<String, double>{'latitude': latitude, 'longitude': longitude});
setState(() {
print("addressFromLatLng: ${_addressFromLatLng}");
});
}
Future<dynamic> getLatLngFromAddress(String address) async {
var result =
await _channel.invokeMethod('getLatLngFromAddress', <String, String>{
'address': address,
});
setState(() {
Map<String, dynamic> data = jsonDecode(result);
print(data);
_latFromAddress = double.parse(data['latitude']);
_lngFromAddress = double.parse(data['longitude']);
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Scaffold(
body: Container(
margin: EdgeInsets.all(16),
alignment: Alignment.center,
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text("Distance: $_distance"),
ElevatedButton(
onPressed: () {
calculateDistanceBetweenTwoCoordinates(
28.7965736573, 77.7635654, 28.8736573, 77.76546124);
},
child: Text("Click me to get distance")),
SizedBox(
height: 32,
),
Text("Locality: $_locality"),
Text("PostalCode: $_postalCode"),
Text("AdministrativeArea: $_administrativeArea"),
Text("Country: $_country"),
Text("Latitude: $_latitude"),
Text("Longitude: $_longitude"),
Text("IPAddress: $_ipAddress"),
ElevatedButton(
onPressed: () {
getCurrentLocation();
},
child: Text("Click me to get your location")),
SizedBox(
height: 16,
),
Text("Address: ${_addressFromLatLng}"),
ElevatedButton(
onPressed: () {
getAddressFromLatLong(28.5720379, 77.064837);
},
child: Text(
"Click me to get address from latitude and longitude")),
SizedBox(
height: 16,
),
Text("Latitude: $_latFromAddress"),
Text("Longitude: $_lngFromAddress"),
ElevatedButton(
onPressed: () {
getLatLngFromAddress("ranchi jharkhand india");
},
child: Text(
"Click me to get latitude and longitude from address")),
],
),
),
),
)));
}
}