简体   繁体   English

如何在 Flutter 中的 Google Map 上设置正确大小的标记?

[英]How to set marker in correct size on Google Map in Flutter?

Can anyone help me to get a marker on GoogleMap in correct size, when I load the marker on Map it gets very bigger and it is ugly.谁能帮我在 GoogleMap 上找到正确大小的标记,当我在 Map 上加载标记时,它会变得非常大而且很丑。 so I have to crop the marker image in correct width and height manually.所以我必须手动以正确的宽度和高度裁剪标记图像。

But I need it to be done automatically inside the code.但我需要它在代码中自动完成。 I have attached the screenshot of my output and also my code below.我附上了我的 output 的屏幕截图以及下面的代码。 Thanks in advance.提前致谢。

在此处输入图像描述

Here is my code这是我的代码

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

class PlaceAutoComplete extends StatefulWidget {
  const PlaceAutoComplete({Key? key}) : super(key: key);

  @override
  _PlaceAutoCompleteState createState() => _PlaceAutoCompleteState();
}

class _PlaceAutoCompleteState extends State<PlaceAutoComplete> {

//GPS permissions
  bool servicestatus = false;
  bool haspermission = false;
  late LocationPermission permission;

  GoogleMapController? _controller;
  Circle? circle; //current location circle
  Marker? marker; //pin marker on circle

  //initial location object
  static const CameraPosition initialLocation = CameraPosition(
      target: LatLng(37.42796133580664, -122.085749655962),
      zoom: 14.4746);


  @override
  void initState() {
    // TODO: implement initState
    checkGps();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Google Places Auto Complete'),
        centerTitle: true,
      ),
      body: SafeArea(
        child: Stack(
          children: [
            GoogleMap(
              initialCameraPosition: initialLocation,
              onMapCreated: (GoogleMapController controller) {
                _controller = controller;
              },
              markers: Set.of((marker != null) ? [marker!] : []),
              circles: Set.of((circle != null) ? [circle!] : []),
            ),
          ],
        ),
      ),
    );
  }

  Future<Position> getCurrentLocationCoordinates() async {
    Position currentLocationCoordinates = await Geolocator.getCurrentPosition(
      desiredAccuracy: LocationAccuracy.high,
    );
    return currentLocationCoordinates;
  }

  void animateCurrentLocation() async {
    Uint8List imageData = await getMarker();
  var  currentLocation = await getCurrentLocationCoordinates();
  _controller!.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
    target: LatLng(currentLocation.latitude, currentLocation.longitude),
    zoom: 15.0,
  ),),);
    updateMarkerAndCircle(currentLocation, imageData);

  }

  checkGps() async {
    servicestatus = await Geolocator.isLocationServiceEnabled();
    if(servicestatus){
      permission = await Geolocator.checkPermission();

      if (permission == LocationPermission.denied) {
        permission = await Geolocator.requestPermission();
        if (permission == LocationPermission.denied) {
          print('Location permissions are denied');
        }else if(permission == LocationPermission.deniedForever){
          print("'Location permissions are permanently denied");
        }else{
          haspermission = true;
        }
      }else{
        haspermission = true;
      }

      if(haspermission){
        setState(() {
          //refresh the UI
        });
        animateCurrentLocation();
      }
    }else{
      print("GPS Service is not enabled, turn on GPS location");
    }

    setState(() {
      //refresh the UI
    });
  }

  Future<Uint8List> getMarker() async {
    ByteData byteData = await DefaultAssetBundle.of(context).load("assets/originicon.png");
    return byteData.buffer.asUint8List();
  }

  void updateMarkerAndCircle(Position newLocalData, Uint8List imageData) {
    LatLng latLng = LatLng(newLocalData.latitude , newLocalData.longitude );
    setState(() {
      marker = Marker(
        markerId: MarkerId("home"), //marker ID is mandate to differentiate from the other markers
        position: latLng,
        rotation: newLocalData.heading ,
        draggable: false,
        zIndex: 2, //we want to place car image on above the circle
        flat: true, // it will stick to map,even when u tilt the camera angle on map (basicaaly it will appear as it is stick on road)
        anchor: Offset(0.5, 0.5), //it will make image to be center on the circle
        icon:  BitmapDescriptor.fromBytes(imageData), //image of car
      );
      circle = Circle(
        circleId: CircleId("car"),
        radius: newLocalData.accuracy ,
        zIndex: 1, //we want to place circle below the car image
        strokeColor: Colors.blue,
        center: latLng,
        fillColor: Colors.blue.withAlpha(70),
      );
    });
  }
}

replace your method getMarker with this code.用此代码替换您的方法 getMarker。

import 'dart:ui' as ui;

Future<Uint8List> getMarker(int width) async {
  ByteData data = await rootBundle.load("assets/originicon.png");
  ui.Codec codec = await ui.instantiateImageCodec(data.buffer.asUint8List(),  targetWidth: width);
  ui.FrameInfo fi = await codec.getNextFrame();
  return (await fi.image.toByteData(format: ui.ImageByteFormat.png)).buffer.asUint8List();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM