Home > Net >  Clickable ListTile in Flutter to take to location based on JSON coordinates
Clickable ListTile in Flutter to take to location based on JSON coordinates

Time:09-10

I have a json file in Flutter which includes x,y coordinates for each place listed. When I click on the ListTile, I'd like to go to the address on Google Maps that matches with the JSON coordinates. In my JSON file, I have attributes named 'location_x' and 'location_y' which are coordinates for particular locations. Here's my code so far:

void onTouch() {
    print('Home Clicked');
    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Farmers\' Market Finder'),
      ),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: ElevatedButton(
              onPressed: readJsonFile,
              child: const Text('Nearby Markets'),
            ),
          ),
          if (_farmersMarkets.isNotEmpty)
            Expanded(
              child: ListView.builder(
                itemCount: _farmersMarkets.length,
                itemBuilder: (BuildContext context, index) {
                  return Card(
                    margin: const EdgeInsets.all(15),
                    color: const Color.fromARGB(255, 243, 186, 54),
                    child: ListTile(
                      title: Text(_farmersMarkets[index]['listing_name']),
                      subtitle:
                          Text(_farmersMarkets[index]['location_address']),
                      onTap: onTouch,
                    ), 

CodePudding user response:

You could use maps_launcher to open the map with the lat and lng.

Example :

import 'package:maps_launcher/maps_launcher.dart';

MapsLauncher.launchQuery('1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA');

MapsLauncher.launchCoordinates(37.4220041, -122.0862462);
  • Related