flutter gps code example

Example 1: flutter geolocator web

@JS('navigator.geolocation') // navigator.geolocation namespace
 library jslocation; // library name can be whatever you want

 import "package:js/js.dart";

@JS('getCurrentPosition') // Accessing method getCurrentPosition 
from       Geolocation API
external void getCurrentPosition(Function success(GeolocationPosition pos));

@JS()
@anonymous
class GeolocationCoordinates {
  external double get latitude;
  external double get longitude;
  external double get altitude;
  external double get accuracy;
  external double get altitudeAccuracy;
  external double get heading;
  external double get speed;

external factory GeolocationCoordinates(
  {double latitude,
  double longitude,
  double altitude,
  double accuracy,
  double altitudeAccuracy,
  double heading,
  double speed});
  }

@JS()
@anonymous
class GeolocationPosition {
external GeolocationCoordinates get coords;

external factory GeolocationPosition({GeolocationCoordinates 
coords});
}

______________________________

NEW FILE

______________________________

 import 'package:Shared/locationJs.dart';

  success(pos) {
     try {
       print(pos.coords.latitude);
       print(pos.coords.longitude);
       } catch (ex) {
        print("Exception thrown : " + ex.toString());
         }
     }

  _getCurrentLocation() {
      if (kIsWeb) {
        getCurrentPosition(allowInterop((pos) => success(pos)));
                }
      }

Example 2: geolocation using flutter

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

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
  Position _currentPosition;
  String _currentAddress;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Location"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            if (_currentPosition != null) Text(_currentAddress),
            FlatButton(
              child: Text("Get location"),
              onPressed: () {
                _getCurrentLocation();
              },
            ),
          ],
        ),
      ),
    );
  }

  _getCurrentLocation() {
    geolocator
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
        .then((Position position) {
      setState(() {
        _currentPosition = position;
      });
      _getAddressFromLatLng();
    }).catchError((e) {
      print(e);
    });
  }

  _getAddressFromLatLng() async {
    try {
      List<Placemark> p = await geolocator.placemarkFromCoordinates(
          _currentPosition.latitude, _currentPosition.longitude);
      Placemark place = p[0];
      setState(() {
        _currentAddress =
            "${place.locality}, ${place.postalCode}, ${place.country}";
      });
    } catch (e) {
      print(e);
    }
  }
}