Make a Map App Using Flutter

 

In this article, I will show you how you can add a Mapping feature to your app using Flutter. for this tutorial you will not need google maps API, so you don't need to pay anything because we're going to use another Free API, so without further ado let's dive into it.


Dependencies 

Create a new Flutter Project and let's add some dependencies that we're going to use. Open your pubspec.yaml file and inside the dependencies add these lines.


  flutter_map: any
  geocoding: ^1.0.5
  geocoder: ^0.2.1
  tuple: ^1.0.2
  latlong: ^0.6.1
  positioned_tap_detector_2: ^1.0.0
  transparent_image: ^1.0.0
  async: ^2.1.0
  flutter_image: ^3.0.0
  vector_math: ^2.0.0
  proj4dart: ^1.0.4
  meta: ^1.1.0
  collection: ^1.14.0



Now let's start building our App

source code:
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:geocoder/geocoder.dart';
import 'package:latlong/latlong.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MapApp(),
    );
  }
}

class MapApp extends StatefulWidget {
  @override
  _MapAppState createState() => _MapAppState();
}

class _MapAppState extends State<MapApp> {
  double long = 49.5;
  double lat = -0.09;
  LatLng point = LatLng(49.5-0.09);
  var location = [];

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        FlutterMap(
          options: MapOptions(
            onTap: (p) async {
              location = await Geocoder.local.findAddressesFromCoordinates(
                  new Coordinates(p.latitude, p.longitude));

              setState(() {
                point = p;
                print(p);
              });

              print(
                  "${location.first.countryName} - ${location.first.featureName}");
            },
            center: LatLng(49.5-0.09),
            zoom: 5.0,
          ),
          layers: [
            TileLayerOptions(
                urlTemplate:
                    "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
                subdomains: ['a''b''c']),
            MarkerLayerOptions(
              markers: [
                Marker(
                  width: 80.0,
                  height: 80.0,
                  point: point,
                  builder: (ctx) => Container(
                    child: Icon(
                      Icons.location_on,
                      color: Colors.red,
                    ),
                  ),
                )
              ],
            ),
          ],
        ),
        Padding(
          padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 34.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Card(
                child: TextField(
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.all(16.0),
                    hintText: "Search for your localisation",
                    prefixIcon: Icon(Icons.location_on_outlined),
                  ),
                ),
              ),
              Card(
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Column(
                    children: [
                      Text(
                          "${location.first.countryName},${location.first.locality}, ${location.first.featureName}"),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}





















Make a Map App Using Flutter Make  a Map App Using Flutter Reviewed by Medics on March 22, 2021 Rating: 5

10 comments:

  1. Text("${location.first.countryName},${location.first.locality}, ${location.first.featureName}"),
    ],
    Exception caught by widgets library =======================================================
    The following StateError was thrown building MapTest(dirty, state: _MapTestState#d4aa7):
    Bad state: No element

    The relevant error-causing widget was:
    MapTest file:///home/shubham/AndroidStudioProjects/any_maptest1/lib/main.dart:19:13
    When the exception was thrown, this was the stack:
    #0 List.first (dart:core-patch/growable_array.dart:332:5)
    #1 _MapTestState.build (package:any_maptest1/main.dart:98:39)
    #2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27)
    #3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
    #4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
    ...
    ====================================================================================================

    ======== Exception caught by widgets library =======================================================
    The following StateError was thrown building MapTest(dirty, state: _MapTestState#d4aa7):
    Bad state: No element

    The relevant error-causing widget was:
    MapTest file:///home/shubham/AndroidStudioProjects/any_maptest1/lib/main.dart:19:13
    When the exception was thrown, this was the stack:
    #0 List.first (dart:core-patch/growable_array.dart:332:5)
    #1 _MapTestState.build (package:any_maptest1/main.dart:98:39)
    #2 StatefulElement.build (package:flutter/src/widgets/framework.dart:4691:27)
    #3 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4574:15)
    #4 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4746:11)
    ...
    ====================================================================================================
    Help ,me in this. why this is happening

    ReplyDelete
    Replies
    1. solved by commenting out

      // Card(
      // child: Padding(
      // padding: const EdgeInsets.all(8.0),
      // child: Column(
      // children: [
      // Text(
      // "${location.first.countryName},${location.first.locality}, ${location.first.featureName}"),
      // ],
      // ),
      // ),
      // ),

      Delete
  2. Apollo Medical Ptv Ltd is here to provide you affordable medical billing services around the globe. Our trained and certified coding specialist are here to help you provide medical billing soultions. Get our services today!

    medical billing services

    ReplyDelete
  3. Thanks for this code
    Discountspk is a consumer-friendly service provider website which was initiated in June 2020 and since then our website has been facilitating the online consumer market and brand market.

    ReplyDelete
  4. Nice tutorial. I have written a similar tutorial at my site please check it css important

    ReplyDelete
  5. I will visit your blog regularly for some latest post. Thanks for sharing,

    Best endocrinologist chennai

    ReplyDelete
  6. Thanks for information and would like to request you follow below Oracle SQL Training


    Oracle SQL Training


    ReplyDelete

-->
Powered by Blogger.