Skip to content

Instantly share code, notes, and snippets.

@xantiagoma
Last active August 14, 2020 03:59
Show Gist options
  • Save xantiagoma/31eca9266e24e886543d56df001e3469 to your computer and use it in GitHub Desktop.
Save xantiagoma/31eca9266e24e886543d56df001e3469 to your computer and use it in GitHub Desktop.
LatLng methods
import 'package:latlong/latlong.dart';
import 'dart:math' as math;
LatLng getCenter(LatLng p1, LatLng p2) {
double dLon = degToRadian(p2.longitude - p1.longitude);
final lat1 = degToRadian(p1.latitude);
final lat2 = degToRadian(p2.latitude);
final lon1 = degToRadian(p1.longitude);
double dx = math.cos(lat2) * math.cos(dLon);
double dy = math.cos(lat2) * math.sin(dLon);
double lat3 = math.atan2(math.sin(lat1) + math.sin(lat2),
math.sqrt((math.cos(lat1) + dx) * (math.cos(lat1) + dx) + dy * dy));
double lon3 = lon1 + math.atan2(dy, math.cos(lat1) + dx);
final p3 = LatLng(radianToDeg(lat3), radianToDeg(lon3));
return p3;
}
/// Returns SW and NE locations
LatLngBounds getBounds(List<LatLng> points) {
LatLng _southWest;
LatLng _northEast;
for (LatLng latLng in points) {
if (_southWest == null && _northEast == null) {
_southWest = new LatLng(latLng.latitude, latLng.longitude);
_northEast = new LatLng(latLng.latitude, latLng.longitude);
} else {
_southWest.latitude = math.min(latLng.latitude, _southWest.latitude);
_southWest.longitude = math.min(latLng.longitude, _southWest.longitude);
_northEast.latitude = math.max(latLng.latitude, _northEast.latitude);
_northEast.longitude = math.max(latLng.longitude, _northEast.longitude);
}
}
return LatLngBounds(_southWest, _northEast);
}
double getZoom(LatLng point1, LatLng point2, BoxConstraints constraints) {
var pixelWidth = constraints.maxWidth;
var bounds = getBounds([point1, point2]);
var sw = bounds.southWest;
var ne = bounds.northEast;
const GLOBE_WIDTH = 256;
var west = sw.longitude;
var east = ne.longitude;
var angle = east - west;
if (angle < 0) {
angle += 360;
}
var zoom = (math.log(pixelWidth * 360 / angle / GLOBE_WIDTH) / math.ln2)
.roundToDouble();
zoom--;
return zoom > 15 ? 15 : zoom;
}
Stream<LatLng> getLocation() async* {
Geolocator geolocator = Geolocator();
LocationPermissions locationPermissions = LocationPermissions();
// if(await have permission)
// Check what ever if can get with native plugin
// else fallback to get from ip
PermissionStatus permission =
await locationPermissions.checkPermissionStatus();
if (permission == PermissionStatus.unknown ||
permission == PermissionStatus.denied ||
permission == null) {
await LocationPermissions().requestPermissions();
}
GeolocationStatus perStatus =
await geolocator.checkGeolocationPermissionStatus();
if (perStatus == GeolocationStatus.denied ||
perStatus == GeolocationStatus.disabled ||
perStatus == null ||
perStatus == GeolocationStatus.unknown) {
var response = await http.get("https://ipapi.co/json/");
var ipapiRes = IpApiCo.fromJson(response.body);
yield LatLng(ipapiRes.latitude, ipapiRes.longitude);
} else {
LocationOptions locationOptions = LocationOptions(
accuracy: LocationAccuracy.high,
distanceFilter: 10,
timeInterval: 3000);
Stream<LatLng> flow = geolocator
.getPositionStream(locationOptions)
.map((pos) => LatLng(pos.latitude, pos.longitude));
yield* flow;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment