I have list of Latlng that contains on list of places lat&lng
and I want check my current location is near of any of these places by 1 kilo
static locationListen() async {
List<LatLng> savedPlace = [
LatLng(12.333,-344.2222),
LatLng(1.333,-14.2222),
LatLng(2.333,-24.2222),
];
var locationOptions =
LocationOptions(accuracy: LocationAccuracy.high, distanceFilter: 10);
Geolocator().getPositionStream(locationOptions).listen(
(Position position) {
LatLng currentLatlng = LatLng(position.latitude,position.longitude);
//check this if is this currentLatlng near by 1 kilo from
// any of those places savedPlace
},
);
}
I see you're using Geolocator plugin:
Take a try like that:
double distanceInMeters = await Geolocator().distanceBetween(lat1, lng1, lat2, lng2);
you can use plugin call latlong 0.6.1
dependencies:
latlong: ^0.6.1
import 'package:latlong/latlong.dart';
and try it
Geolocator().getPositionStream(locationOptions).listen( (Position position) {
LatLng currentLatlng = LatLng(position.latitude,position.longitude);
savedPlace.forEach((savedlatlong) {
int km = distance.as(LengthUnit.Kilometer,currentLatlng,savedlatlong);
// do whatever you wanna do with km
});
},);
Related
This is all my code. I want it to be able to search for places from lat , lng , and if lat,lng is not there, it will search for places from names. But now it's like when searching for a place from the name. In the part where the name is converted to lat,lng it's correct but my marker appears in the middle of the sea
LatLng centerMap = LatLng(13.730452094709594, 100.64180575311175);
#override
void initState() {
findLocation();
super.initState();
}
void findLocation() async {
var lat = double.tryParse(searchitems[0].address![widget.index].latitude.toString());
var lng = double.tryParse(searchitems[0].address![widget.index].longitude.toString());
if (lat != null && lng != null) {
centerMap = LatLng(lat, lng);
} else {
var name = searchitems[0].name;
var addr = '${searchitems[0].address![widget.index].addr1!}, ${searchitems[0].address![widget.index].thanon!}, ${searchitems[0].address![widget.index].tambon!}, ${searchitems[0].address![widget.index].province!}';
var addresses = await Geocoder.local.findAddressesFromQuery(addr);
var first = addresses.first;
centerMap = LatLng(first.coordinates.latitude!, first.coordinates.longitude!);
print("Location not found, searching for name: $name and address: $addr");
}
print("map:$centerMap");
}
GoogleMap(
initialCameraPosition: CameraPosition(
target: centerMap,
zoom: 16.0,
),
You can add a controller in Google maps, then when you call the API and get new lat long, you can animate camera to that position like this:
Future<void> _goToNewPosition() async {
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(_kLake));
}
There's also an example on the official package, you can check it out by this link
i have a problem with google maps on flutter app, im trying to animate markers iterating marker list with the following method.
Future<void> _iteratePoints() async {
CameraUpdate zoom = CameraUpdate.zoomTo(14);
controller.animateCamera(zoom);
for (final value in listIds) {
await Future.delayed(Duration(seconds: 3));
updatePosition(value);
updateDetailPosition(value);
}
}
Future<void> updateDetailPosition(MarkerAndDetails markerId) async {
print(markerId.toJson().toString());
this.setState(() {
currentlySelectedPin = markerId;
});
}
Future<void> updatePosition(MarkerGlobal markerId) async {
final Marker marker = markers[markerId.markerId];
final LatLng current = marker.position;
var newPosition = CameraPosition(target: LatLng(current.latitude, current.longitude), zoom: 12);
CameraUpdate update = CameraUpdate.newCameraPosition(newPosition);
// CameraUpdate zoom = CameraUpdate.zoomTo(16);
// controller.animateCamera(update);
controller.showMarkerInfoWindow(markerId.markerId);
}
But when im trying on iphone... i have next error:
Unhandled Exception: Concurrent modification during iteration: Instance(length:798) of '_GrowableList'.
I hope someone can help me.
I have some code written in dart where I use the provider package to update the location of a pin on a map. What I would like it to do is have the initial location be equal to the user's current position and then if they drag the pin it will be updated to wherever the pin is dropped.
My problem is that the initial location variable needs to Future<LatLng>, however, when I update the location it ends up being just LatLng and I cannot assign it to the _location variable.
class LocationProvider with ChangeNotifier {
Future<LatLng> _location = LocationService().getLocation();
// Error here, wants it to be Future<LatLng>
LatLng get location => _location;
void calculateNewLocation(oldLocation, zoom, offset) {
var newPoint = const Epsg3857().latLngToPoint(oldLocation, zoom) +
CustomPoint(offset.dx, offset.dy);
LatLng? newLocation = const Epsg3857().pointToLatLng(newPoint, zoom);
// Error here again for the same reason
_location = newLocation ?? _location;
notifyListeners();
}
}
How do I make it so that I can assign both of these values to _location?
You can simply have a method for that in provider file
class LocationProvider with ChangeNotifier {
LatLng? _location;
LatLng? get location => _location;
Future<void> initializeLocation() async {
_location = await LocationService().getLocation();
notifyListeners();
}
void calculateNewLocation(oldLocation, zoom, offset) {
var newPoint = const Epsg3857().latLngToPoint(oldLocation, zoom) +
CustomPoint(offset.dx, offset.dy);
LatLng? newLocation = const Epsg3857().pointToLatLng(newPoint, zoom);
_location = newLocation ?? _location;
notifyListeners();
}
}
Then, you'll have to call initializeLocation when you want it to be initialized, like:
Future<void>? _myFuture;
final _provider = Provider.of<LocationProvider>(listen: false);
_myFuture = _provider.initializeLocation();
and then in FutureBuilder, provide _myFuture in future
PS: ? can be excluded if you're not using dart in null safe mode
Based on your code,
LocationService().getLocation() returns a future, so you have to either await/async or use then().
try these
Future<LatLng> _location = LocationService().getLocation();
LatLng get location = await _location; // put this in a separate method with async keyword
or
LocationService().getLocation().then((value) { location = value } );
I need to get the city name from Longitude and Latitude.
I do not found a single package to do that, so I use location package to get Long and Lat and used geocoder to get the city name.
But I keep have this error and have no result :
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: PlatformException(failed, Failed, null, null)
and this one after it on the console: < asynchronous suspension >
Here is my code :
class _ProfileScreenState extends State<ExploreScreen> {
dynamic currentLocation;
double userLongitude;
double userLatitude;
Coordinates coordinates;
var addresses;
var first;
#override
initState() {
super.initState();
_getCurrentLongAndLat();
_getCurrentPosition(userLatitude, userLatitude);
}
Future _getCurrentLongAndLat() async {
currentLocation = LocationData;
var error;
var location = new Location();
try {
currentLocation = await location.getLocation();
userLatitude = currentLocation.latitude;
userLongitude = currentLocation.longitude;
print('$userLatitude $userLatitude');
} on PlatformException catch (e) {
if (e.code == 'PERMISSION_DENIED') {
error = 'Permission denied';
}
currentLocation = null;
}
}
Future _getCurrentPosition(double long, double lat) async {
coordinates = new Coordinates(userLatitude, userLongitude);
addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);
print(addresses.first);
}
}
I realized flutter packages geolocator and location conflict with each other sometimes.
Try using geolocator to get your current position:
Future _getCurrentPosition() async {
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high).then((Position position) {
setState(() {
_currentPosition = position;
userLongitude = position.longitude;
userLatitude = position.latitude;
});
});
}
It is a Future, so you can use await to really wait for results and after that call the getAddress function with the userLat and userLng.
I am trying to get the current user position before get the stream data using that info. So using Flutter 1.17.5 and geolocator: ^5.3.2+2 I am getting the Positionusing the follow util class
class LocationProvider {
Future<Position> getPosition() async {
Position position = await Geolocator()
.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
return position;
}
}
then I have to pass the info to bloc in order to retreive the Stream data with the marker
Widget _getAllProducts(ProductBloc productBloc, LocationProvider locationProvider) async{
Position position = locationProvider.getPosition();
var longitude = position.longitude;
var latitude = position.latitude;
int meter = 50;
productBloc.allByRange(meter, latitude, longitude);
return StreamBuilder(
stream: productBloc.productsByRangeStream,
How ever I got this message
A value of type 'StreamBuilder<List<ProductModel>>' can't be returned from method '_getAllProducts' because it has a return type of 'Widget'
Edit 1
class ProductBloc {
Stream<List<ProductModel>> get productsByRangeStream =>
_allByRangeProductsController.stream;
void allByRange(double range, double lat, double lon) async {
final products = await _productFirebaseService.allByRange(range, lat, lon);
_allByRangeProductsController.sink.add(products);
}
}
How can I fix it??