cannot display custom google map marker by Flutter

Viewed 32

I am trying to use googlemapsdk in flutter to display custom icons I have prepared myself as pins. I wrote the following code, but the markers are showing the default ones. I do not know what is causing this. Why is the default marker displayed instead of the custom marker?

import 'dart:async';
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Google Maps Demo',
      home: MapSample(),
    );
  }
}

class MapSample extends StatefulWidget {
  @override
  State<MapSample> createState() => MapSampleState();
}

class MapSampleState extends State<MapSample> {
  Completer<GoogleMapController> _controller = Completer();

  static final CameraPosition _kGooglePlex = CameraPosition(
    target: LatLng(35.6809591, -86.0),
    zoom: 1,
  );

  static final CameraPosition _kLake = CameraPosition(
      bearing: 192.8334901395799,
      target: LatLng(35.6809591, -86.0),
      tilt: 59.440717697143555,
      zoom: 1);

  BitmapDescriptor? _markerIcon;

  Future<LatLng> _initAsync(BuildContext context) async {
    await _loadPinAsset();

    final result = await checkLocationSetting();
    if (result != LocationSettingResult.enabled) {
      await recoverLocationSettings(context, result);
    }
    return await getCurrentLocation();
  }

  Future<void> _loadPinAsset() async {
    _markerIcon = await BitmapDescriptor.fromAssetImage(
        const ImageConfiguration(size: Size(48, 48)), 'assets/images/marker.png');
  }

  Marker _createMarker() {
    print(_markerIcon);
    print('odgrogogdr');
    return Marker(
      markerId: const MarkerId('marker'),
      position: const LatLng(35.6809591, -86.0),
      icon: _markerIcon ?? BitmapDescriptor.defaultMarkerWithHue(2.0),
      infoWindow: const InfoWindow(title: 'title'),
    );
  }

  Future<LatLng> getCurrentLocation() async {
    final position = await Geolocator.getCurrentPosition();
    return LatLng(position.latitude, position.longitude);
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<LatLng>(
      future: _initAsync(context),
      builder: (BuildContext context, AsyncSnapshot<LatLng> snapshot) {
        if (!snapshot.hasData) {
          return const Center(child: CircularProgressIndicator());
        }
        return GoogleMap(
          mapType: MapType.normal,
          initialCameraPosition: CameraPosition(
              target: snapshot.data ?? LatLng(35.6809591, -86.0),
              zoom: 1),
          myLocationEnabled: true,
          onMapCreated: (GoogleMapController controller) {
            _controller.complete(controller);
          },
          markers: Set<Marker>.of(<Marker>{_createMarker()}),
        );
      },
    );
  }

  Future<LocationSettingResult> checkLocationSetting() async {
    final serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      print('Location services are disabled.');
      return Future.value(LocationSettingResult.serviceDisabled);
    }
    var permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
        print('Location permissions are denied.');
        return Future.value(LocationSettingResult.permissionDenied);
      }
    }

    if (permission == LocationPermission.deniedForever) {
      print('Location permissions are permanently denied.');
      return Future.value(LocationSettingResult.permissionDeniedForever);
    }
    return Future.value(LocationSettingResult.enabled);
  }

  Future<void> recoverLocationSettings(
      BuildContext context, LocationSettingResult locationResult) async {
    if (locationResult == LocationSettingResult.enabled) {
      return;
    }
    final result = await showOkCancelAlertDialog(
      context: context,
      okLabel: 'OK',
      cancelLabel: 'キャンセル',
      title: 'xxxxxxx',
      message: 'xxxxxxxxxxxx',
    );
    if (result == OkCancelResult.cancel) {
      print('Cancel recover location settings.');
    } else {
      locationResult == LocationSettingResult.serviceDisabled
          ? await Geolocator.openLocationSettings()
          : await Geolocator.openAppSettings();
    }
  }
}

enum LocationSettingResult {
  serviceDisabled,
  permissionDenied,
  permissionDeniedForever,
  enabled,
}



0 Answers
Related