Home > Software design >  Setting up a static marker for google maps flutter
Setting up a static marker for google maps flutter

Time:05-26

I've created a map widget that uses the google_maps_flutter package.

                           SizedBox(
                            height:MediaQuery.of(context).size.height * 0.9,
                            width: MediaQuery.of(context).size.width,
                            child: GoogleMap(
                              onMapCreated: _onMapCreated,
                              initialCameraPosition: CameraPosition(
                                target: LatLng(double.parse(latitude),
                                    double.parse(longitude)),
                                zoom: 15,
                              ),
                            ),
                          )

I have fixed values for longitude and latitude in the state.Right now the camera of the map focuses to that location but what I want is to indicate the location using a marker.How can I achieve this when the longitude and latitude is given?

CodePudding user response:

Found the way to do it.Created a Set of type marker and assigned it to markers property in GooglMap.

 GoogleMap(
                              onMapCreated: _onMapCreated,
                              markers:_markers,
                              initialCameraPosition: CameraPosition(
                                target: LatLng(double.parse(latitude),
                                    double.parse(longitude)),
                                zoom: 15,
                              ),
                            ),

Then assigned it the marker

 _markers.add( Marker(markerId: MarkerId("1"),position:LatLng(double.parse(latitude), double.parse(longitude))));

CodePudding user response:

Please refer to below code

class MapDemo extends StatelessWidget {
  const MapDemo({Key key}) : super(key: key);

 /*Controller*/
  GoogleMapController mapController;

  /*Variable declaration*/
  bool customFlag = false;
  BitmapDescriptor customIcon1;


 /*Custom Marker for Maps*/
  createMarker(context) {
    if (customIcon1 == null) {
      ImageConfiguration configuration = createLocalImageConfiguration(context);
      BitmapDescriptor.fromAssetImage(configuration, AssetImagePaths) /* Add asset image in place of AssetImagePaths */
          .then((icon) {
        customIcon1 = icon;
      });
    }
  }

 

  @override
  Widget build(BuildContext context) {
    createMarker(context);
    return Scaffold(
      body: Column(
        children: [
          LocationMaps(
            mapController: mapController,
            customFlag: customFlag,
            customIcon1: customIcon1,
            longitudeValues: 85,
            latitudeValues: 12.0,
          ),
          
        ],
      ),
    );
  }
}

class LocationMaps extends StatelessWidget {
  GoogleMapController mapController;
  bool customFlag;
  final BitmapDescriptor customIcon1;
  final longitudeValues;
  final latitudeValues;

  LocationMaps({
    Key key,
    this.mapController,
    this.customFlag,
    this.customIcon1,
    this.longitudeValues,
    this.latitudeValues,
  }) : super(key: key);

  double locationLatitude = 0.0;
  double locationLongitude = 0.0;

  LatLng _center;
  List<Marker> _markers = <Marker>[];

  void _onMapCreated(GoogleMapController controller) {
    mapController = controller;
  }

  @override
  Widget build(BuildContext context) {
    locationLatitude = double.parse(latitudeValues);
    locationLongitude = double.parse(longitudeValues);
    _center = LatLng(locationLatitude, locationLongitude);
    _markers.add(Marker(
        icon: customFlag == true ? customIcon1 : null,
        markerId: MarkerId(''),
        position: LatLng(locationLatitude, locationLongitude),
        infoWindow: InfoWindow(title: '')));
    return ClipRRect(
      borderRadius: BorderRadius.circular(8.0),
      child: Container(
        color: AppColors.creamColor,
        width: ScreenUtil().screenWidth,
        child: Column(
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(8.0),
              child: Container(
                width: ScreenUtil().screenWidth,
                height: ScreenUtil().setHeight(120.0),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(8.0),
                ),
                child: GoogleMap(
                  myLocationButtonEnabled: (Platform.isAndroid) ? true : false,
                  onMapCreated: _onMapCreated,
                  initialCameraPosition: CameraPosition(
                    target: _center,
                    zoom: 13.0,
                  ),
                  mapType: MapType.normal,
                  markers: Set<Marker>.of(_markers),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

  • Related