Commit 8de4b325 authored by Yasiru-Deshan's avatar Yasiru-Deshan

Show direction function completed

parent 9de08b4a
......@@ -27,4 +27,22 @@ class LocationService{
print(results);
return results;
}
Future<Map<String, dynamic>> getDirections(String origin, String destination)async{
final String url = 'https://maps.googleapis.com/maps/api/directions/json?origin=$origin&destination=$destination&key=$key';
var response = await http.get(Uri.parse(url));
var json = convert.jsonDecode(response.body);
var results = {
'bounds_ne': json['routes'][0]['bounds']['northeast'],
'bounds_sw':json['routes'][0]['bounds']['southwest'],
'start_location':json['routes'][0]['legs']['start_location'],
'end_location':json['routes'][0]['legs']['end_location'],
'polyine': json['routes'][0]['overview_polyline']['points'],
'polyline_decoded': PolylinePoints().decodePolyline(json['routes'][0]['overview_polyline']['points']),
};
return results;
}
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:look_ai/location_service.dart';
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
void main() => runApp(MyApp());
......@@ -26,14 +27,68 @@ class MapSample extends StatefulWidget {
class MapSampleState extends State<MapSample> {
final Completer<GoogleMapController> _controller =
Completer<GoogleMapController>();
TextEditingController _searchController = TextEditingController();
TextEditingController _originController = TextEditingController();
TextEditingController _destinationController = TextEditingController();
Set<Marker> _markers = Set<Marker>();
Set<Polygon> _polygons = Set<Polygon>();
Set<Polyline> _polylines = Set<Polyline>();
List<LatLng> polygonLatLngs = <LatLng>[];
int _polygonIdCounter = 1;
int _polylineIdCounter = 1;
static const CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(37.42796133580664, -122.085749655962),
zoom: 14.4746,
);
static final Marker _kGooglePlexMarker = Marker(
@override
void initStates(){
super.initState();
_setMarker(LatLng(37.42796133580664, -122.085749655962));
}
void _setMarker(LatLng point){
setState(() {
_markers.add(
Marker(markerId: MarkerId('marker'), position: point),
);
});
}
void _setPolygon(){
final String polygonIdVal = 'polygon_$_polygonIdCounter';
_polygonIdCounter++;
_polygons.add(Polygon(
polygonId: PolygonId(polygonIdVal),
points: polygonLatLngs,
strokeWidth: 2,
fillColor: Colors.transparent,
));
}
void _setPolyline(List<PointLatLng> points){
final String polylineIdVal = 'polyline_$_polylineIdCounter';
_polylineIdCounter++;
_polylines.add(
Polyline(
polylineId: PolylineId(polylineIdVal),
width: 2,
color: Colors.blue,
points: points
.map(
(point) => LatLng(point.latitude, point.longitude),
)
.toList(),
),
) ;
}
/*static final Marker _kGooglePlexMarker = Marker(
markerId: MarkerId('_kGooglePlex'),
infoWindow: InfoWindow(title:"Google Plex"),
icon: BitmapDescriptor.defaultMarker,
......@@ -72,7 +127,7 @@ class MapSampleState extends State<MapSample> {
],
strokeWidth: 5,
fillColor: Colors.transparent,
);
);*/
@override
Widget build(BuildContext context) {
......@@ -81,9 +136,52 @@ class MapSampleState extends State<MapSample> {
body: Column(
children:[
Row(children:[
Expanded(child:
Column(children: [
TextFormField(
controller: _originController,
//textCapitalization: TextCapitalization.words,
decoration: InputDecoration(hintText: 'Origin'),
onChanged: (value) {
print(value);
},
),
TextFormField(
controller: _destinationController,
//textCapitalization: TextCapitalization.words,
decoration: InputDecoration(hintText: 'Destination'),
onChanged: (value) {
print(value);
},
)
],
),
),
IconButton(
onPressed: () async{
var directions = await
LocationService().getDirections(_originController.text, _destinationController.text);
_goToPlace(
directions['start_location']['lat'],
directions['start_location']['lng'],
directions['bounds_ne'],
directions['bounds_sw'],
);
_setPolyline(directions['polyline_decoded']);
/*var place = await LocationService().getPlace(_searchController.text);
_goToPlace(place);*/
},
icon: Icon(Icons.search),
),
],
),
/* Row(children:[
Expanded(child: (TextFormField(
controller: _searchController,
textCapitalization: TextCapitalization.words,
//textCapitalization: TextCapitalization.words,
decoration: InputDecoration(hintText: 'Search City'),
onChanged: (value) {
print(value);
......@@ -95,25 +193,26 @@ class MapSampleState extends State<MapSample> {
_goToPlace(place);
},
icon: Icon(Icons.search),),
],),
],),*/
Expanded(
child:GoogleMap(
mapType: MapType.normal,
markers: {_kGooglePlexMarker,
//_kLakeMarker,
},
/* polylines:{
_kPolyline,
},
polygons: {
_kPolygon,
},*/
markers: _markers,
polygons: _polygons,
polylines: _polylines,
initialCameraPosition: _kGooglePlex,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
onTap: (point){
setState(() {
polygonLatLngs.add(point);
_setPolygon();
});
},
),
),
/* floatingActionButton: FloatingActionButton.extended(
onPressed: _goToTheLake,
label: const Text('To the lake!'),
......@@ -123,21 +222,33 @@ class MapSampleState extends State<MapSample> {
);
}
Future<void> _goToPlace(Map<String, dynamic> place) async {
final double lat = place['geometry']['location']['lat'];
final double lng = place['geometry']['location']['lng'];
Future<void> _goToPlace(
Map<String, dynamic> boundsNe,
Map<String, dynamic> boundSw,
double lat,
double lng,
) async {
//final double lat = place['geometry']['location']['lat'];
//final double lng = place['geometry']['location']['lng'];
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(target: LatLng(lat, lng), zoom: 12),
));
}
controller.animateCamera(
CameraUpdate.newLatLngBounds(
LatLngBounds(
southwest: LatLng(boundSw['lat'], boundSw['lng']),
northeast: LatLng(boundsNe['lat'], boundsNe['lng']),
),
25),
);
_setMarker(LatLng(lat, lng)); }
Future<void> _goToTheLake() async {
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(_kLake));
}
}
/*import 'package:flutter/material.dart';
......
......@@ -70,6 +70,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.0.7"
flutter_polyline_points:
dependency: "direct main"
description:
name: flutter_polyline_points
sha256: "02699e69142f51a248d784b6e3eec524194467fca5f7c4da19699ce2368b6980"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
flutter_test:
dependency: "direct dev"
description: flutter
......
......@@ -30,6 +30,7 @@ dependencies:
cupertino_icons: ^1.0.2
google_maps_flutter: ^2.0.9
http: ^0.13.4
flutter_polyline_points: ^1.0.0
dev_dependencies:
flutter_test:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment