| // Copyright 2013 The Flutter Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| // ignore_for_file: public_member_api_docs |
| // ignore_for_file: unawaited_futures |
| |
| import 'package:flutter/material.dart'; |
| import 'package:google_maps_flutter/google_maps_flutter.dart'; |
| |
| import 'page.dart'; |
| |
| class MarkerIconsPage extends GoogleMapExampleAppPage { |
| const MarkerIconsPage({Key? key}) |
| : super(const Icon(Icons.image), 'Marker icons', key: key); |
| |
| @override |
| Widget build(BuildContext context) { |
| return const MarkerIconsBody(); |
| } |
| } |
| |
| class MarkerIconsBody extends StatefulWidget { |
| const MarkerIconsBody({Key? key}) : super(key: key); |
| |
| @override |
| State<StatefulWidget> createState() => MarkerIconsBodyState(); |
| } |
| |
| const LatLng _kMapCenter = LatLng(52.4478, -3.5402); |
| |
| class MarkerIconsBodyState extends State<MarkerIconsBody> { |
| GoogleMapController? controller; |
| BitmapDescriptor? _markerIcon; |
| |
| @override |
| Widget build(BuildContext context) { |
| _createMarkerImageFromAsset(context); |
| return Column( |
| mainAxisAlignment: MainAxisAlignment.spaceEvenly, |
| crossAxisAlignment: CrossAxisAlignment.stretch, |
| children: <Widget>[ |
| Center( |
| child: SizedBox( |
| width: 350.0, |
| height: 300.0, |
| child: GoogleMap( |
| initialCameraPosition: const CameraPosition( |
| target: _kMapCenter, |
| zoom: 7.0, |
| ), |
| markers: <Marker>{_createMarker()}, |
| onMapCreated: _onMapCreated, |
| ), |
| ), |
| ) |
| ], |
| ); |
| } |
| |
| Marker _createMarker() { |
| if (_markerIcon != null) { |
| return Marker( |
| markerId: const MarkerId('marker_1'), |
| position: _kMapCenter, |
| icon: _markerIcon!, |
| ); |
| } else { |
| return const Marker( |
| markerId: MarkerId('marker_1'), |
| position: _kMapCenter, |
| ); |
| } |
| } |
| |
| Future<void> _createMarkerImageFromAsset(BuildContext context) async { |
| if (_markerIcon == null) { |
| final ImageConfiguration imageConfiguration = |
| createLocalImageConfiguration(context, size: const Size.square(48)); |
| BitmapDescriptor.fromAssetImage( |
| imageConfiguration, 'assets/red_square.png') |
| .then(_updateBitmap); |
| } |
| } |
| |
| void _updateBitmap(BitmapDescriptor bitmap) { |
| setState(() { |
| _markerIcon = bitmap; |
| }); |
| } |
| |
| void _onMapCreated(GoogleMapController controllerParam) { |
| setState(() { |
| controller = controllerParam; |
| }); |
| } |
| } |