Bisclavret
Bisclavret

Reputation: 1351

Extract latitude and longitude from Firebase GeoPoint

Working in Flutter I can access sections of my database like so:

Streambuilder(
  stream: Firestore.instance.collection('stores').snapshots(),
  builder: (context, snapshot) {
    if (!snapshot.hasData) return CircularProgressIndicator();
    return ListView.builder(
      itemExtent: 60,
      itemCount: snapshot.data.documents.length,
      itemBuilder: (context, index) =>
         _buildListRows(context, snapshot.data.documents[index]),
    );
  }
),

And then the _buildListRows widget:

Widget _buildListRows(BuildContext context, DocumentSnapshot document) {
    geoPoint = document.reference.firestore.
    return Container(
      height: MediaQuery.of(context).size.height * 0.7,
      child: ListView(
        children: <Widget>[
          Row(
            children: <Widget>[
              Expanded(
                child: Text(
                  'Business Name',
                  style: Theme
                      .of(context)
                      .textTheme
                      .headline,
                ),
              ),
              Container(
                decoration: const BoxDecoration(
                  color: Colors.teal,
                ),
                padding: const EdgeInsets.all(10),
                child: Text(
                  document['store_name'],
                  style: Theme
                      .of(context)
                      .textTheme
                      .display1,
                ),
              ),
            ],
          ),
          Row(
            children: <Widget>[
              Expanded(
                child: Text(
                  'Location',
                  style: Theme
                      .of(context)
                      .textTheme
                      .headline,
                ),
              ),
              Container(
                decoration: const BoxDecoration(
                  color: Colors.teal,
                ),
                padding: const EdgeInsets.all(10),
                child: Text(
                  document['location'].toString(),
                  style: Theme
                      .of(context)
                      .textTheme
                      .display1,
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

I am just getting started on this so looking at the best way to retrieve data on demand from the database and displaying it in the app. I just can't find anywhere that explains how to extract the longitude and latitude from the GeoPoint reference returned by: document['location'].toString(),

What I get from this output is:

Instance of 'GeoPoint'

Also, am I doing this right? Is this the best way to extract specific data from the database? It feels like I am doing this very inefficiently, but can't find another way to do this.

Upvotes: 1

Views: 1790

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80924

To access the longitude and latitude, then do the following:

     child: Text(
                  document['location'].latitude.toString(),
                  style: Theme
                      .of(context)
                      .textTheme
                      .display1,
                ),

Since document['location'] returns an instance of GeoPoint, then just call the property latitude to get the value.

Upvotes: 2

Related Questions