Skip to content

Instantly share code, notes, and snippets.

@iam111
Last active October 12, 2023 07:45
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save iam111/9668cb4ee273b65b5d9c608b94e1d85b to your computer and use it in GitHub Desktop.
Custom Google Map Flutter
Set<Marker> markerLocations = <Marker>{
const Marker(
markerId: MarkerId('location1'),
position: LatLng(37.483210, -122.145176),
),
const Marker(
markerId: MarkerId('location2'),
position: LatLng(37.482144, -122.144668),
)
};
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class CustomMapSample extends StatefulWidget {
const CustomMapSample({super.key});
@override
State<CustomMapSample> createState() => CustomMapSampleState();
}
class CustomMapSampleState extends State<CustomMapSample> {
final Completer<GoogleMapController> _controller =
Completer<GoogleMapController>();
Set<Marker> markerLocations = <Marker>{
const Marker(
markerId: MarkerId('location1'),
position: LatLng(37.483210, -122.145176),
),
const Marker(
markerId: MarkerId('location2'),
position: LatLng(37.482144, -122.144668),
)
};
static const CameraPosition nearMetaHeadquarters = CameraPosition(
target: LatLng(37.483498, -122.144196),
zoom: 15,
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Map Sample"),),
body: GoogleMap(
mapType: MapType.normal,
initialCameraPosition: nearMetaHeadquarters,
markers: markerLocations, //******ADD THIS *********
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
);
}
}
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class CustomMapSample extends StatefulWidget {
const CustomMapSample({super.key});
@override
State<CustomMapSample> createState() => CustomMapSampleState();
}
class CustomMapSampleState extends State<CustomMapSample> {
final Completer<GoogleMapController> _controller =
Completer<GoogleMapController>();
Set<Marker> markerLocations = {};
static const CameraPosition nearMetaHeadquarters = CameraPosition(
target: LatLng(37.483498, -122.144196),
zoom: 15,
);
@override
void initState() {
// TODO: implement initState
super.initState();
setMarkers();
}
Future<void> setMarkers() async {
markerLocations = <Marker>{
Marker(
icon: await BitmapDescriptor.fromAssetImage(const ImageConfiguration(size: Size(5, 5)), 'assets/free.png'),
markerId: MarkerId('location1'),
position: LatLng(37.483210, -122.145176),
),
Marker(
icon: await BitmapDescriptor.fromAssetImage(const ImageConfiguration(size: Size(5, 5)), 'assets/delivery_bike.png'),
markerId: MarkerId('location2'),
position: LatLng(37.482144, -122.144668),
)
};
setState(() {
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Map Sample"),),
body: GoogleMap(
mapType: MapType.normal,
initialCameraPosition: nearMetaHeadquarters,
markers: markerLocations!,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
);
}
}
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class CustomMapSample extends StatefulWidget {
const CustomMapSample({super.key});
@override
State<CustomMapSample> createState() => CustomMapSampleState();
}
class CustomMapSampleState extends State<CustomMapSample> {
final Completer<GoogleMapController> _controller =
Completer<GoogleMapController>();
static const CameraPosition nearMetaHeadquarters = CameraPosition(
target: LatLng(37.483498, -122.144196),
zoom: 15,
);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Map Sample"),),
body: GoogleMap(
mapType: MapType.normal,
initialCameraPosition: nearMetaHeadquarters,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment