Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

dart - Flutter image not showing on google marker

I am showing an image instead of the normal marker but don't know why it's not showing, I am doing same way on other map page and its working -_- mean the same way I am doing as I am doing here its showing simple red marker on this page and another page I can see my image marker.

This is my page code where the issue is

class _TripRouteScreenState extends State<TripRouteScreen> {
  var start_currentPostion;
  var end_currentPostion;
  BitmapDescriptor pinLocationIcon;

  Map<MarkerId, Marker> setmarkers = {};
  List listMarkerIds = List(); // For store data of your markers

  @override
  void initState() {
    super.initState();
    setCustomMapPin();
    working();
  }

  void setCustomMapPin() async {
    pinLocationIcon = await BitmapDescriptor.fromAssetImage(
        ImageConfiguration(devicePixelRatio: 2.5), 'images/pin.png');
  }

  working() {
    double start_latitude = widget.data['start']['lat'].toDouble();
    double start_longitude = widget.data['start']['lon'].toDouble();

    double end_latitude = widget.data['end']['lat'].toDouble();
    double end_longitude = widget.data['end']['lon'].toDouble();

    start_currentPostion = LatLng(start_latitude, start_longitude);
    end_currentPostion = LatLng(end_latitude, end_longitude);

    setState(() {
      MarkerId markerId1 = MarkerId("1");
      MarkerId markerId2 = MarkerId("2");

      listMarkerIds.add(markerId1);
      listMarkerIds.add(markerId2);

      Marker marker1 = Marker(
        markerId: markerId1,
        position: LatLng(start_latitude, start_longitude),
        icon: pinLocationIcon,
      );

      Marker marker2 = Marker(
        markerId: markerId2,
        position: LatLng(end_latitude, end_longitude),
        icon: pinLocationIcon, // you can change the color of marker
      );

      setmarkers[markerId1] =
          marker1; // I Just added here markers on the basis of marker id
      setmarkers[markerId2] = marker2;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          leading: GestureDetector(
              onTap: () {
                Navigator.pop(context);
              },
              child: Icon(Icons.arrow_back)),
          centerTitle: true,
          flexibleSpace: Container(
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage('images/nav.jpg'),
                fit: BoxFit.cover,
              ),
            ),
          ),
          backgroundColor: Colors.transparent,
          title: Text(
            'Route Location',
            style: TextStyle(fontFamily: 'UbuntuBold'),
          ),
          actions: [
            Padding(
              padding: const EdgeInsets.only(right: 15),
              child: Icon(
                Icons.notifications_none,
                size: 33,
              ),
            )
          ]),
      body: GoogleMap(
        mapType: MapType.normal,
        initialCameraPosition: CameraPosition(
          target: start_currentPostion,
          zoom: 15,
        ),
        markers: Set.of(setmarkers.values), // YOUR MARKS IN MAP
      ),
    );
  }
}

And this is the page where image is showing on marker

class _MapScreenState extends State<MapScreen> {
  Completer<GoogleMapController> _controller = Completer();
  Set<Marker> _markers;
  bool loading = true;
  var currentPostion;
  Map<MarkerId, Marker> markers = <MarkerId, Marker>{}; // CLASS MEMBER, MAP OF MARKS
  BitmapDescriptor pinLocationIcon;


  @override
  void initState() {
    super.initState();
    _markers = Set<Marker>();

    setCustomMapPin();
    getImi();
  }
  void setCustomMapPin() async {
    pinLocationIcon = await BitmapDescriptor.fromAssetImage(
        ImageConfiguration(devicePixelRatio: 2.5),
        'images/pin.png');
  }

  getImi() async {
    final storage = new FlutterSecureStorage();

    String imi = await storage.read(key: "imei");
    print('showimi');
    print(imi);

    var map = new Map<String, dynamic>();

    var url =
        'http://api.igiinsurance.com.pk:8888/drive_api/location.php?imei=${imi}';
    print(url);
    http.Response res = await http.get(
      url,
      headers: <String, String>{
        'token': 'c66026133e80d4960f0a5b7d418a4d08'
      },
    );
    var data = json.decode(res.body.toString());
    print(data);

    if (data['status'].toString() == "success") {
      print(data["data"]["location"]["lon"]);
      print(data["data"]["location"]["gc"]["rd"]);
      print(data["data"]["location"]["gc"]["sb"]);

      var tit = data["data"]["location"]["gc"]["rd"];
      var snip = data["data"]["location"]["gc"]["sb"];

      print(data['lon']);
      print(data['lat']);
      double latitude = data['lat'].toDouble();
      double longitude = data['lon'].toDouble();

      print(longitude);


      currentPostion = LatLng(latitude, longitude);
      print(currentPostion);

      final Map<String, Marker> _markers = {};

      setState(() {
        final MarkerId markerId = MarkerId('1');

        // creating a new MARKER
        final Marker marker = Marker(
          markerId: markerId,
          icon: pinLocationIcon,

          position: currentPostion,
          infoWindow: InfoWindow(title: tit, snippet: snip),

        );

        setState(() {
          // adding a new marker to map
          markers[markerId] = marker;
        });
        loading = false;
      }
      );
    }
  }


  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
          leading: GestureDetector(
              onTap: () {
                Navigator.pop(context);
              }, child: Icon(Icons.arrow_back)),
          centerTitle: true,
          flexibleSpace: Container(
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage('images/nav.jpg'),
                fit: BoxFit.cover,
              ),
            ),
          ),
          backgroundColor: Colors.transparent,
          title: Text(
            'My Car Location', style: TextStyle(fontFamily: 'UbuntuBold'),),
          actions: [
            Padding(
              padding: const EdgeInsets.only(right: 15),
              child: Icon(
                Icons.notifications_none,
                size: 33,
              ),
            )
          ]),
      body: loading == false ? GoogleMap(
        mapType: MapType.normal,
        initialCameraPosition: CameraPosition(
          target: loading == false ? currentPostion : LatLng(1.0, 1.0),
          zoom: 18,
        ),
        markers: Set<Marker>.of(markers.values), // YOUR MARKS IN MAP

      ) :
        Center(
            child:  SpinKitWave(color: Color(0xff00abb5), type: SpinKitWaveType.center))

    );
  }

I have double-check code but don't get issue what I am doing wrong why it's working on one page only zzzz

question from:https://stackoverflow.com/questions/65921695/flutter-image-not-showing-on-google-marker

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

enter image description here

You can add a custom icon using BitmapDescriptor.fromAsset

Marker marker1 = Marker(
    markerId: markerId1,
    position: LatLng(start_latitude, start_longitude),
    icon: BitmapDescriptor.fromAsset("images/pin.png"),
  );


  Marker marker2 = Marker(
    markerId: markerId2,
    position: LatLng(end_latitude, end_longitude),
    icon: BitmapDescriptor.fromAsset("images/pin.png"),  // you can change the color of marker
  );

I hope this will work for you


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...