How to find the nearest location from a currenr location to some other places in flutter?

Viewed 4131

I want to build a flutter app which will have the functionality described below: 1. Have a database of places. Possibly stored by their latitude and longitude 2. The user will have a slider in kilometers in which distance range he wants to find those places 3. When he hits search, the app will go through the places which meets the criteria of that of that distance range, and show the places by their distance order.

I am really having a tough time figuring out how to do it. Specially getting the distance of those places from current location.

Also, is there any way to find out places of specific distance range without going through all of them?

Thanks in advance.

2 Answers

You can calculate distance using the latlong package.

final int meter = distance(
    new LatLng(lat1, long1),
    new LatLng(lat2, long2)
    );

You can get the phone's location with the location package.

I think this task is for backend, but if you interesting just formula of distance, I have php solution:

$dist = 6371.0 * 2.0 * asin(sqrt(
                        pow(sin(($src_lat - abs($dst_lat)) * pi() / 180.0 / 2.0), 2) +
                        cos($src_lat * pi() / 180.0) *
                        cos(abs($dst_lat) * pi() / 180.0) *
                        pow(sin(($src_lon - $dst_lon) * pi() / 180.0 / 2.0), 2)));
Related