4
Guys, I need to get the current latitude and longitude of the cell phone coom flutter, but it’s not working and I’m not finding anything on the internet, someone can help me?
code:
import 'dart:async';
import 'package:location/location.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Completer<GoogleMapController> _controller = Completer();
static const LatLng _center = const LatLng(0, 0);
static var location = new Location();
var x = location.getLocation();
var lg = x.longitude;
var lt = x.latitude;
final Set<Marker> _makers2 = {};
LatLng _lastMapPosition = _center;
static var latitudes = [-23.5629, -19.9324, -22.0154];
static var longitudes = [-46.6544, -43.9388, -47.8911];
static var i = 0;
LatLng posicaoVetor = LatLng(latitudes[i], longitudes[i]);
MapType _currentMapType = MapType.normal;
void _onMapTypeButtonPressed() {
setState(() {
_currentMapType = _currentMapType == MapType.normal
? MapType.satellite
: MapType.normal;
});
}
void _onAddMarkerButtonPressed() {
setState(() {
_makers2.add(Marker(
markerId: MarkerId(LatLng(latitudes[i], longitudes[i]).toString()),
position: LatLng(latitudes[i], longitudes[i]),
icon: BitmapDescriptor.defaultMarker,
));
});
i++;
}
void _onCameraMove(CameraPosition position) {
_lastMapPosition = position.target;
}
void _onMapCreated(GoogleMapController controller) {
_controller.complete(controller);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(''),
backgroundColor: Colors.green[700],
),
body: Stack(
children: <Widget>[
GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: LatLng(0, 0), /*AQUI SERIA A LOCALIZAÇAO ATUAL DA PESSOA*/
zoom: 11.0,
),
mapType: _currentMapType,
markers: _makers2,
onCameraMove: _onCameraMove,
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Align(
alignment: Alignment.topRight,
child: Column(
children: <Widget> [
FloatingActionButton(
onPressed: _onMapTypeButtonPressed,
materialTapTargetSize: MaterialTapTargetSize.padded,
backgroundColor: Colors.green,
child: const Icon(Icons.map, size: 36.0),
),
SizedBox(height: 16.0),
FloatingActionButton(
onPressed: _onAddMarkerButtonPressed,
materialTapTargetSize: MaterialTapTargetSize.padded,
backgroundColor: Colors.green,
child: const Icon(Icons.add_location, size: 36.0),
),
],
),
),
),
],
),
),
);
}
}
tried to use that package and follow the steps on the? https://pub.dartlang.org/packages/location
– Ricardo Pontual