Geolocator In Flutter

Geolocator In Flutter :

Geolocator In Flutter : This flutter tutorial post get current location lat, long and this lat, long get current city name, country name and postal code.

Screenshot :

Geolocator In Flutter

main.dart

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Tutorial',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        home: new Scaffold(
            appBar: new AppBar(
              title: new Text('Geolocator in flutter'),
            ),
            body: Center(
                child: RaisedButton(
              child: Text("Geolocator"),
              onPressed: getData,
              color: Colors.blueAccent,
              textColor: Colors.white,
              padding: EdgeInsets.all(10),
              splashColor: Colors.grey,
            ))));
  }

  getData() async {
    Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    print('Current location lat long ' + position.latitude.toString() + " - " + position.longitude.toString());

    List<Placemark> placeMark = await Geolocator().placemarkFromCoordinates(position.latitude, position.longitude);
    print('City name ' + placeMark[0].locality);
    print('Country name ' + placeMark[0].country);
    print('Postal Code ' + placeMark[0].postalCode);
  }
}

pubspec.yaml

name: fluttertutorial
description: A new Flutter application.

version: 1.0.0+1

environment:
sdk: “>=2.1.0 <3.0.0”

dependencies:
flutter:
sdk: flutter

geolocator:

dev_dependencies:
flutter_test:
sdk: flutter

flutter:
uses-material-design: true

gradle.properties

org.gradle.jvmargs=-Xmx1536M<br>
android.enableJetifier=true<br>
android.useAndroidX=true

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in