Reputation: 596
Im trying get calculate distance between two location, using this formula
double calculateDistance(lat1, lon1, lat2, lon2){
var p = 0.017453292519943295;
var c = cos;
var a = 0.5 - c((lat2 - lat1) * p) / 2 +
c(lat1 * p) * c(lat2 * p) *
(1 - c((lon2 - lon1) * p)) / 2;
return 12742 * asin(sqrt(a));
}
value of lat1 and lon1 are from geolocator: using getCurrentPosition, while
value of lat2 and lon2 are from array class that i made
but tats getting error for a few milliseconds, and a red screen appears. because when geolocator was tryin to getting latitude and longitude, value of lat1 and lon1 is null, then "red screen" will also appears when GPS is turned off.
this my code of geolocator function
class _HomePageState extends State<HomePage> {
Geolocator geolocator = Geolocator();
Position _currentUserPosition;
var distance;
@override
void initState() {
super.initState();
_initCurrentUserLocation();
}
@override
void didUpdateWidget(Widget oldWidget) {
super.didUpdateWidget(oldWidget);
setState(() {
_currentUserPosition = null;
});
}
_initCurrentUserLocation() async {
geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best,
).then((position) {
if (mounted) {
setState(() =>_currentUserPosition = position);
}
}).catchError((e) {});
}
and this my Widget build using ListView Builder for getting value lat2 and lon2 from array file.
double calculateDistance(lat1, lon1, lat2, lon2){
var p = 0.017453292519943295;
var c = cos;
var a = 0.5 - c((lat2 - lat1) * p) / 2 +
c(lat1 * p) * c(lat2 * p) *
(1 - c((lon2 - lon1) * p)) / 2;
return 12742 * asin(sqrt(a));
}
@override
Widget build(BuildContext context) {
final coordinateData = Data.dataArray;
var distance;
return ListView.builder(
itemCount: dataCoordinate.length,
itemBuilder: (context, index) {
distance = calculateDistance(
_currentPosition.latitude, _currentUserPosition.longitude,
coordinateData[index].lat, coordianteData[index].long
);
return ListTile(
title: Text(distance.toStingAsFixed(2)),
);
},
);
}
my ask is
How to set Value of distance is null when lat1 and lon1 is still null, or gps on mobile is turned off?
Upvotes: 0
Views: 151
Reputation: 46
In your function set default values for the latitudes and longitudes
double calculateDistance({lat1=0, lon1=0, lat2=0, lon2=0}){
var p = 0.017453292519943295;
var c = cos;
var a = 0.5 - c((lat2 - lat1) * p) / 2 +
c(lat1 * p) * c(lat2 * p) *
(1 - c((lon2 - lon1) * p)) / 2;
return 12742 * asin(sqrt(a));
}
and when call the function you set like this
distance = calculateDistance(
lat1:_currentPosition.latitude,
lon1:_currentUserPosition.longitude,
lat2:coordinateData[index].lat,
lon2: coordianteData[index].long,
);
Other way is set function to receive as a parameters Position and validate is the 2 was passed
double calculateDistance({Position position1, Position position2}){
if (position1 == null || position2 == null) return 0;
var lat1=position1.lat, lon1=position1.lon;
var lat2=position2.lat, lon2=position2.lon;
var p = 0.017453292519943295;
var c = cos;
var a = 0.5 - c((lat2 - lat1) * p) / 2 +
c(lat1 * p) * c(lat2 * p) *
(1 - c((lon2 - lon1) * p)) / 2;
return 12742 * asin(sqrt(a));
}
Upvotes: 0
Reputation: 836
getCurrentLocation() async {
//Future because we dont know its future value
try {
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
latitude = position.latitude;
longitude = position.longitude;
} catch (e) {
print("PERMISSION Denied");
}
wrap this code in a try catch block and use async await to get future value conditions when the catch block will execute : 1.permisiion denied 2.not getting location
in the catch block execute what you want if any of the following conditions occurs. You dont need if else conditons just use try catch and async await bettr to go for a async await tutorial just google it if you dont get my point.
Upvotes: 0