Search code examples
firebasefluttergoogle-mapsgoogle-cloud-firestoregoogle-polyline

Getting Polyline from Firestore


Can I retrieve all the coordinates as LatLng that is stored in Firestore from different documents and use the coordinates to build a route using the Polyline in GoogleMap API. Or do I need to save it in Array in Firestore to be able to do so? I am making a route from 3 to 4 coordinates in the Firestore is it possible or is it just 2 coordinates is the max in making the Polyline as the destination and starting point. Thank you in advance. Any documentation is appreciated as well.

Image of my Firestore: Firestore Image


Solution

  • Can I retrieve all the coordinates as LatLng that is stored in Firestore from different documents and use the coordinates to build a route using the Polyline in GoogleMap API.

    As I see in your screenshot, the location field in your database is of type GeoPoint. So you need to create a query, loop through the results, create a new LatLng object of each location, and add all of them to the polyline.

    Or do I need to save it in Array in Firestore to be able to do so.

    That's nothing mandatory. Save them to an array only if those are your requirements.

    I am making a route from 3 to 4 coordinates in the Firestore is it possible or is it just 2 coordinates is the max in making the Polyline as the destination and starting point.

    You can add as many coordinates as you want into a Polyline.

    Edit:

    If you are using only the origin and the destination, the following code will work fine:

    PolylinePoints polylinePoints = PolylinePoints();
    PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(googleAPiKey,
            _originLatitude, _originLongitude, _destLatitude, _destLongitude);
    print(result.points);
    

    However, if you need more than that, you indeed need a List of points:

    List<PointLatLng> result = polylinePoints.decodePolyline("_p~iF~ps|U_ulLnnqC_mqNvxq`@");
    print(result);
    

    More info is below: