I'm implementing the google_maps_flutter plugin in my flutter app and stucked in highlighting the country for example India by red color, Australia by blue etc. I went throught geoJson but not sure how to implement it in flutter.
Thanks in advance.
I'm implementing the google_maps_flutter plugin in my flutter app and stucked in highlighting the country for example India by red color, Australia by blue etc. I went throught geoJson but not sure how to implement it in flutter.
Thanks in advance.
After spending 4 days in looking into map stuff and it's features I got most easy and feasible solution for filling any country, state, continent in flutter. We can get the geoJson data of any region from here
Extract the latLong co-ordinates of that region and draw a polygon based on it.
Following is the code for filling India with colour.
GeoJson.dart
class GeoJson
{
static const List IN = [
[77.83745079947457,35.494009507787766],[78.91226891471322,34.32193634697579],
[78.81108646028574,33.50619802503242],[79.20889163606857,32.994394639613716],
[79.17612877799553,32.48377981213771],[78.45844648632601,32.61816437431273],
[78.73889448437401,31.515906073527063],[79.7213668151071,30.882714748654728],
[81.11125613802932,30.183480943313402],[80.4767212259174,29.72986522065534],
[80.08842451367627,28.79447011974014],[81.05720258985203,28.416095282499043],
[81.99998742058497,27.925479234319994],[83.30424889519955,27.36450572357556],
[84.6750179381738,27.234901231387536],[85.25177859898338,26.72619843190634],
[86.02439293817918,26.63098460540857],[87.22747195836628,26.397898057556077],
[88.06023766474982,26.41461538340249],[88.17480431514092,26.81040517832595],
[88.04313276566123,27.445818589786825],[88.12044070836987,27.876541652939594],
[88.73032596227856,28.086864732367516],[88.81424848832054,27.29931590423936],
[88.83564253128938,27.098966376243762],[89.74452762243884,26.719402981059957],
[90.37327477413407,26.87572418874288],[91.21751264848643,26.808648179628022],
[92.03348351437509,26.83831045176356],[92.10371178585973,27.452614040633208],
[91.69665652869668,27.77174184825166],[92.50311893104364,27.89687632904645],
[93.41334760943268,28.640629380807226],[94.56599043170294,29.277438055939985],
[95.40480228066464,29.03171662039213],[96.11767866413103,29.452802028922466],
[96.58659061074749,28.830979519154344],[96.24883344928779,28.41103099213444],
[97.32711388549004,28.26158274994634],[97.40256147663612,27.88253611908544],
[97.0519885599681,27.69905894623315],[97.1339990580153,27.083773505149964],
[96.41936567585097,27.264589341739224],[95.12476769407496,26.5735720891323],
[95.1551534362626,26.001307277932085],[94.60324913938538,25.162495428970402],
[94.55265791217164,24.675238348890332],[94.10674197792505,23.85074087167348],
[93.3251876159428,24.078556423432204],[93.28632693885928,23.043658352139005],
[93.06029422401463,22.70311066333557],[93.16612755734836,22.278459580977103],
[92.67272098182556,22.041238918541254],[92.14603478390681,23.627498684172593],
[91.86992760617132,23.624346421802784],[91.70647505083211,22.985263983649183],
[91.15896325069971,23.50352692310439],[91.46772993364367,24.072639471934792],
[91.91509280799443,24.13041372323711],[92.37620161333481,24.976692816664965],
[91.79959598182207,25.147431748957317],[90.8722107279121,25.132600612889547],
[89.92069258012185,25.26974986419218],[89.83248091019962,25.96508209889548],
[89.35509402868729,26.014407253518073],[88.56304935094977,26.44652558034272],
[88.2097892598025,25.76806570078271],[88.93155398962308,25.238692328384776],
[88.30637251175602,24.866079413344206],[88.08442223506242,24.501657212821925],
[88.69994022009092,24.23371491138856],[88.52976972855377,23.631141872649163],
[88.87631188350309,22.879146429937826],[89.03196129756623,22.055708319582976],
[88.88876590368542,21.690588487224748],[88.20849734899521,21.703171698487807],
[86.97570438024027,21.49556163175521],[87.03316857294887,20.743307806882413],
[86.49935102737378,20.151638495356607],[85.0602657409097,19.4785788029711],
[83.94100589390001,18.302009792549725],[83.18921715691785,17.67122142177898],
[82.19279218946592,17.016636053937813],[82.19124189649719,16.556664130107848],
[81.69271935417748,16.310219224507904],[80.79199913933014,15.951972357644491],
[80.32489586784388,15.899184882058348],[80.02506920768644,15.136414903214147],
[80.2332735533904,13.835770778859981],[80.28629357292186,13.006260687710833],
[79.8625468281285,12.056215318240888],[79.85799930208682,10.35727509199711],
[79.340511509116,10.30885427493962],[78.88534549348918,9.546135972527722],
[79.18971967968828,9.216543687370148],[78.2779407083305,8.933046779816934],
[77.94116539908435,8.252959092639742],[77.53989790233794,7.965534776232333],
[76.59297895702167,8.89927623131419],[76.13006147655108,10.299630031775521],
[75.74646731964849,11.308250637248307],[75.39610110870957,11.781245022015824],
[74.86481570831681,12.741935736537897],[74.61671715688354,13.99258291264968],
[74.44385949086723,14.617221787977696],[73.5341992532334,15.99065216721496],
[73.11990929554943,17.928570054592498],[72.82090945830865,19.208233547436166],
[72.8244751321368,20.419503282141534],[72.6305334817454,21.356009426351008],
[71.17527347197395,20.757441311114235],[70.4704586119451,20.877330634031384],
[69.16413008003883,22.0892980005727],[69.64492760608239,22.450774644454338],
[69.34959679553435,22.84317963306269],[68.1766451353734,23.69196503345671],
[68.84259931831878,24.35913361256094],[71.04324018746823,24.3565239527302],
[70.84469933460284,25.21510203704352],[70.28287316272558,25.72222870533983],
[70.16892662952202,26.491871649678842],[69.51439293811312,26.940965684511372],
[70.61649620960193,27.989196275335868],[71.77766564320032,27.913180243434525],
[72.8237516620847,28.961591701772054],[73.45063846221743,29.97641347911987],
[74.42138024282026,30.979814764931177],[74.40592898956501,31.69263947196528],
[75.25864179881322,32.2711054550405],[74.45155927927871,32.7648996038055],
[74.10429365427734,33.44147329358685],[73.74994835805195,34.31769887952785],
[74.24020267120497,34.74888703057125],[75.75706098826834,34.50492259372132],
[76.87172163280403,34.65354401299274],[77.83745079947457,35.494009507787766]];
}
main.dart
@override
void initState() {
addPoints();
List< Polygon > addPolygon = [
Polygon(
polygonId: PolygonId( 'India' ),
points: point,
consumeTapEvents: true,
strokeColor: Colors.grey,
strokeWidth: 1,
fillColor: Colors.redAccent,
),
];
polygon.addAll( addPolygon );
super.initState();
}
void addPoints()
{
for( var i=0 ; i < GeoJson.IN.length ; i++ )
{
var ltlng= LatLng( GeoJson.IN[ i ][ 1 ], GeoJson.IN[ i ][ 0 ] );
point.add( ltlng );
}
}
void _onMapCreated(GoogleMapController controller)
{
mapController = controller;
}
Widget mapState()
{
return GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 0.0
),
scrollGesturesEnabled: true,
zoomGesturesEnabled: true,
myLocationButtonEnabled: false,
gestureRecognizers: Set()
..add( Factory<PanGestureRecognizer>(() => PanGestureRecognizer())),
//markers: markers,
polygons: polygon,
);
}
For anyone wondering - here is the full main.dart code that can be copy & pasted in order to work. Make sure to add your API key for google maps under android/app/src/main/AndroidManifest.xml.
Lastly add the geojson.dart as described above.
import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; import 'geojson.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( // This is the theme of your application. // // Try running your application with "flutter run". You'll see the // application has a blue toolbar. Then, without quitting the app, try // changing the primarySwatch below to Colors.green and then invoke // "hot reload" (press "r" in the console where you ran "flutter run", // or simply save your changes to "hot reload" in a Flutter IDE). // Notice that the counter didn't reset back to zero; the application // is not restarted. primarySwatch: Colors.blue, // This makes the visual density adapt to the platform that you run // the app on. For desktop platforms, the controls will be smaller and // closer together (more dense) than on mobile platforms. visualDensity: VisualDensity.adaptivePlatformDensity, ), home: MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State { Set polygon = new Set(); List point = []; GoogleMapController mapController; @override void initState() { addPoints(); List addPolygon = [ Polygon( polygonId: PolygonId( 'India' ), points: point, consumeTapEvents: true, strokeColor: Colors.grey, strokeWidth: 1, fillColor: Colors.redAccent, ), ]; polygon.addAll( addPolygon ); super.initState(); } void addPoints() { for( var i=0 ; i (() => PanGestureRecognizer())), //markers: markers, polygons: polygon, ); } @override Widget build(BuildContext context) { // TODO: implement build return mapState(); } }
Full Code (combination of two above answers):
main.dart:
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:map_test/dist1.dart';
import 'dist2.dart';
import 'dist3.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: GoogleMapPage(),
);
}
}
class GoogleMapPage extends StatefulWidget {
@override
_GoogleMapPageState createState() => _GoogleMapPageState();
}
class _GoogleMapPageState extends State<GoogleMapPage> {
Completer<GoogleMapController> _controller = Completer();
Set<Polygon> polygon = new Set();
@override
void initState() {
List<LatLng> distPoints = addPoints();
List<Polygon> distPolygon = [
Polygon(
polygonId: PolygonId('dist'),
points: distPoints,
consumeTapEvents: true,
strokeColor: Colors.grey,
strokeWidth: 1,
fillColor: Color(0xFFFF1744).withOpacity(0.4),
),
];
polygon.addAll(distPolygon);
super.initState();
}
addPoints() {
List<LatLng> point = [];
for (var i = 0; i < Dist.IN.length; i++) {
var latLang = LatLng(Dist.IN[i][1], Dist.IN[i][0]);
point.add(latLang);
}
return point;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: GoogleMap(
onMapCreated: (controller) {
_controller.complete(controller);
},
gestureRecognizers: Set()
..add(Factory<PanGestureRecognizer>(() => PanGestureRecognizer())),
initialCameraPosition:
CameraPosition(target: LatLng(35.697791, 51.379233), zoom: 10),
polygons: polygon,
),
),
);
}
}
dist.dart:
class Dist
{
static const List IN = [
[
51.38412002338114,
35.74300506579035
],
[
51.38461899807706,
35.74429378340329
],
[
51.38539139045448,
35.74586119929674
],
[
51.385842085388134,
35.74709769765289
],
[
51.385986840855935,
35.748299327291846
],
[
51.38606194270838,
35.74984922500766
],
[
51.38611558688868,
35.75121622959622
],
[
51.386255061757474,
35.75327985415754
],
[
51.38629529489273,
35.75416791679879
],
[
51.38644558241663,
35.75496025810577
],
[
51.38672989657226,
35.75584823692799
],
[
51.38709459317934,
35.75661015192645
],
[
51.387958264482336,
35.7582948306453
],
[
51.3882801295642,
35.75890863741469
],
[
51.3885215283756,
35.75957893821146
],
[
51.38860199464608,
35.760101382236286
],
[
51.38866636766244,
35.76085005382773
],
[
51.3884839774494,
35.76201664711144
],
[
51.388237298039,
35.76340091842053
],
[
51.387411093843184,
35.76834565455968
],
[
51.38734135640877,
35.76914654540792
],
[
51.387384271753035,
35.76987778264077
],
[
51.38751301778577,
35.77062637041319
],
[
51.38772759450703,
35.7714620461596
],
[
51.388017273080685,
35.772193283392454
],
[
51.388725376260794,
35.77338586057598
],
[
51.38959449580085,
35.774717661171216
],
[
51.39010939611279,
35.7754923166625
],
[
51.39056009104643,
35.77615381646097
],
[
51.391750991849335,
35.77794695700689
],
[
51.3925126553907,
35.77918295244885
],
[
51.39300618184956,
35.78017511832749
],
[
51.3931349278823,
35.7811587346649
],
[
51.3931349278823,
35.782011677131834
],
[
51.39296326650532,
35.78290812167626
],
[
51.392115688456414,
35.78668542634139
],
[
51.39186892522699,
35.788199868606796
],
[
51.39186892522699,
35.78861753884191
],
[
51.39194402707943,
35.78910494651146
],
[
51.392230688167956,
35.78941935169951
],
[
51.39282747967391,
35.79008294697373
],
[
51.39326736195243,
35.79044621865728
],
[
51.39373272521663,
35.790748637723794
],
[
51.39462321860981,
35.79104451890581
],
[
51.39596164090855,
35.791440563830776
],
[
51.39750927551051,
35.79185395929528
],
[
51.39920174939931,
35.79234564173544
],
[
51.401124893263436,
35.79323764387117
],
[
51.401865182951724,
35.79355095941179
],
[
51.402605472640005,
35.7936640312856
],
[
51.40314199826213,
35.79363796356674
],
[
51.403549694032506,
35.79351617451363
],
[
51.40457957847542,
35.7929939819459
],
[
51.40594214065532,
35.79205403532404
],
[
51.40700429542545,
35.791749436962704
],
[
51.40783049962127,
35.79171465206454
],
[
51.409270762043576,
35.79179302285922
],
[
51.4106547818956,
35.791758154142
],
[
51.41294538839486,
35.79137099403442
],
[
51.41652881963958,
35.79059633854314
],
[
51.4182561622456,
35.79002192671865
],
[
51.419618724425476,
35.789273590403326
],
[
51.42018198831874,
35.78859038147564
],
[
51.42065942152351,
35.78791588972726
],
[
51.42104565962174,
35.78708038161892
],
[
51.42166256769531,
35.78577054161002
],
[
51.42258524759665,
35.784569498704286
],
[
51.424201529985595,
35.7830384602706
],
[
51.42424821718626,
35.782994120002826
],
[
51.4257073388907,
35.78137523922388
],
[
51.42670512064449,
35.78006095680627
],
[
51.4275476695515,
35.778700154826055
],
[
51.42929638601057,
35.778851280540266
],
[
51.43052458628259,
35.77918186280144
],
[
51.43168338439634,
35.779894659847315
],
[
51.43320369399396,
35.78094968999977
],
[
51.434167026125685,
35.78200622889477
],
[
51.43451034887965,
35.78241534958869
],
[
51.43493958614118,
35.78276345002748
],
[
51.43586612171799,
35.78340114522092
],
[
51.43671244248142,
35.783834070519845
],
[
51.43718987568619,
35.78401679600901
],
[
51.437650377446516,
35.784114277542926
],
[
51.43935441836172,
35.78459129165253
],
[
51.440974472607074,
35.78491324055341
],
[
51.44240409001236,
35.785274416761155
],
[
51.44655883177739,
35.78642324041013
],
[
51.44986339710357,
35.787711371289845
],
[
51.45414638198716,
35.790021172347366
],
[
51.456953648998024,
35.79145104120974
],
[
51.45848938129748,
35.79260237942964
],
[
51.46048486098601,
35.79319414179369
],
[
51.4631912937017,
35.79280689786708
],
[
51.46631338499577,
35.79189301896406
],
[
51.46968215570024,
35.79137099403442
],
[
51.47176354989628,
35.79070086087569
],
[
51.47392004594476,
35.790230887564775
],
[
51.4755669222803,
35.79008504244953
],
[
51.47634602018027,
35.79011689368161
],
[
51.475195184874565,
35.788893890189534
],
[
51.474001518043615,
35.78769502657866
],
[
51.47337656334301,
35.78710326421461
],
[
51.473164668830776,
35.786918275611555
],
[
51.47271405771615,
35.786696322815516
],
[
51.47197645023689,
35.786498342262576
],
[
51.47143464401577,
35.786372194619815
],
[
51.470898286031684,
35.78625468033732
],
[
51.47047977760624,
35.78605016189988
],
[
51.470093539507985,
35.7853669529722
],
[
51.468210628779104,
35.78154606241056
],
[
51.46661203220586,
35.77806447128938
],
[
51.46429468743545,
35.77534865084211
],
[
51.46226685360071,
35.773120321883454
],
[
51.4601961882407,
35.770726450337094
],
[
51.457868030815234,
35.7679843945329
],
[
51.456591383142865,
35.76649576852929
],
[
51.45604412868468,
35.76584281827206
],
[
51.45545404270126,
35.76389276849875
],
[
51.45466019265168,
35.76165538708466
],
[
51.45437043025899,
35.76105465608421
],
[
51.45382325961984,
35.76041042300628
],
[
51.45252507045632,
35.75474258008034
],
[
51.45226757839083,
35.753575819158605
],
[
51.45126988045607,
35.75290543454278
],
[
51.44961755588349,
35.751895331391346
],
[
51.44899528339187,
35.75162543410917
],
[
51.449166944768876,
35.750301931598074
],
[
51.4494673521786,
35.74830804447114
],
[
51.44947808101466,
35.74777688326705
],
[
51.449360063818,
35.74710633101316
],
[
51.44894163921157,
35.746165965296115
],
[
51.44855548493237,
35.74559121819553
],
[
51.44729744508504,
35.743562378532374
],
[
51.447220834490054,
35.743335564232495
],
[
51.44709896161791,
35.74297455566281
],
[
51.446921935822864,
35.741990520230246
],
[
51.44640494003514,
35.742042739487005
],
[
51.44568619183801,
35.742173329538446
],
[
51.44501019134705,
35.742364939845
],
[
51.44450593605214,
35.74251304807407
],
[
51.44359398498687,
35.74261757040665
],
[
51.442885881806774,
35.74260885322735
],
[
51.442124134446374,
35.74257398451012
],
[
51.440976148987716,
35.742582617870426
],
[
51.4400051893241,
35.74266978966341
],
[
51.43922198429155,
35.74266978966341
],
[
51.43831003322629,
35.74269585738227
],
[
51.43709767475127,
35.742687140202975
],
[
51.436325282373815,
35.74260885322735
],
[
51.43569228104616,
35.742495529896445
],
[
51.43511284007977,
35.742364939845
],
[
51.43449056758817,
35.7420863253835
],
[
51.43376109055498,
35.74176412502552
],
[
51.43313873424435,
35.741398338771035
],
[
51.432537919424874,
35.74109357277163
],
[
51.432001477621775,
35.74086717756691
],
[
51.43125045909741,
35.7406146308243
],
[
51.43032777919609,
35.74047532359356
],
[
51.428568250081895,
35.74044917205566
],
[
51.4245771230668,
35.74028379710606
],
[
51.422999732708575,
35.7402472520082
],
[
51.42043579234684,
35.74018799195278
],
[
51.41870844974082,
35.740153039416526
],
[
51.41754973544612,
35.7400834696202
],
[
51.4168738187742,
35.74024012739051
],
[
51.416165799413136,
35.740527542850344
],
[
51.415403968233704,
35.740980333259756
],
[
51.4137785495703,
35.74212119410072
],
[
51.41259837760347,
35.74265235530481
],
[
51.41119298389836,
35.74310514571425
],
[
51.40940118445695,
35.743505716866906
],
[
51.40708375586754,
35.74399337599354
],
[
51.40637833489646,
35.74407602155884
],
[
51.40548247708528,
35.74408918114682
],
[
51.404372042552865,
35.7440194437124
],
[
51.40323478593026,
35.74384099299385
],
[
51.40175429037273,
35.74333154091897
],
[
51.400820881635326,
35.742826531252774
],
[
51.40001613511163,
35.74234323071578
],
[
51.39903444661195,
35.74181206951167
],
[
51.39844436062853,
35.74160738343616
],
[
51.39739838293153,
35.7413417609246
],
[
51.39660436524389,
35.74122852141272
],
[
51.39584798230151,
35.74120245369386
],
[
51.395139879121416,
35.741263390129944
],
[
51.39391142739231,
35.74147679338472
],
[
51.3929780186549,
35.741772842204824
],
[
51.39210361851585,
35.74206897484393
],
[
51.39070618761875,
35.74261757040665
],
[
51.38982642306166,
35.74293532835594
],
[
51.389268523586416,
35.74310514571425
],
[
51.38880718363578,
35.74324889535367
],
[
51.388526892793635,
35.743303293905285
],
[
51.38771158507194,
35.7434077324188
],
[
51.38697657598266,
35.7434491390205
],
[
51.38647508671579,
35.743414270303276
],
[
51.38534319451125,
35.74325752871394
],
[
51.38412002338114,
35.74300506579035
]
];
}
© 2022 - 2024 — McMap. All rights reserved.