Skip to content

Commit

Permalink
Added route poly lines to map
Browse files Browse the repository at this point in the history
  • Loading branch information
Usaid-Dev committed Mar 16, 2023
1 parent deeebd2 commit 313ab8e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:google_map/polygone_screen.dart';
import 'package:google_map/polyline_screen.dart';

void main() {
runApp(const MyApp());
Expand All @@ -16,7 +16,7 @@ class MyApp extends StatelessWidget {
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const PolygoneScreen(),
home: const PolylineScreen(),
);
}
}
81 changes: 81 additions & 0 deletions lib/polyline_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import 'dart:async';

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

class PolylineScreen extends StatefulWidget {
const PolylineScreen({super.key});

@override
State<PolylineScreen> createState() => _PolylineScreenState();
}

class _PolylineScreenState extends State<PolylineScreen> {
final Completer<GoogleMapController> _controller = Completer();

static const CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(
33.738045,
73.084488,
),
zoom: 14,
);

final Set<Marker> _markers = {};
final Set<Polyline> _polyline = {};

List<LatLng> latlng = [
const LatLng(
33.738045,
73.084488,
),
const LatLng(
33.567997728,
72.635997456,
)
];

@override
void initState() {
super.initState();
for (int i = 0; i < latlng.length; i++) {
_markers.add(
Marker(
markerId: MarkerId(
i.toString(),
),
position: latlng[i],
infoWindow: const InfoWindow(
title: "Really cool place",
snippet: "5 star rating",
),
icon: BitmapDescriptor.defaultMarker,
),
);
setState(() {});
_polyline.add(
Polyline(
polylineId: const PolylineId('1'),
points: latlng,
color: Colors.orange,
),
);
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: GoogleMap(
myLocationEnabled: true,
mapType: MapType.normal,
polylines: _polyline,
initialCameraPosition: _kGooglePlex,
markers: _markers,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
);
}
}

0 comments on commit 313ab8e

Please sign in to comment.