Flutter google map

라디·2021년 7월 21일
0
return GoogleMap(
      gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>[
        new Factory<OneSequenceGestureRecognizer>(
              () => new EagerGestureRecognizer(),
        ),
      ].toSet(),
      mapType: MapType.normal,
      initialCameraPosition: CameraPosition(target: centerLatLng, zoom: 1.0),   //지도 생성직후의 카메라 위치.
      markers: Set.from(marker),
      //  polylines: _polyLines,
      onMapCreated: (GoogleMapController controller) async {  //initialCameraPosition 이후 보여줄 카메라 위치
    
        controller.animateCamera(
          CameraUpdate.newLatLngBounds(
            bounds,
            32.0,
          ),
        );

        BitmapDescriptor departureMarker =
        await _bitmapDescriptorFromSvgAsset(context, 'images/icons8-point-w-m-fill.svg');

        BitmapDescriptor stopMarker = await _bitmapDescriptorFromSvgAsset(
            context, 'images/icons8-point-stop-m-fill.svg');

        BitmapDescriptor destinationMarker =
        await _bitmapDescriptorFromSvgAsset(context, 'images/icons8-goal-w-m-fill.svg');

        /*** ㅁㅁㅁ 마커 ***/
        marker.addAll([

              Marker(
              icon: departureMarker,
              markerId: MarkerId(pickUpLatLng.longitude.toString()),
              position: pickUpLatLng,
              infoWindow: pickUpInfoWindow(context)),

          /*** ㅁㅁㅁ 마커 ***/
            Marker(
              //anchor: Offset(5.0, 5.0),  마커가 고정되지 않음
                markerId:
                MarkerId(dropOffLatLng.longitude.toString()),
                position: dropOffLatLng,
                icon: destinationMarker,
                infoWindow: dropOffInfoWindow(context))
        ]);

        if (widget.snapshot.data.stopPlaces != null) {
          /*** ㅁㅁㅁ 마커들 ***/
          marker.addAll([
            for (int index = 0; index < widget.snapshot.data.stopPlaces.length; index++)
              Marker(
                  icon: stopMarker,
                  markerId:
                      MarkerId('${widget.snapshot.data.stopPlaces[index].stopOriginAddress1}'),
                  position: LatLng(widget.snapshot.data.stopPlaces[index].stopOriginLatitude,
                      widget.snapshot.data.stopPlaces[index].stopOriginLongitude),
                  infoWindow: stopInfoWindow(context))
          ]);
        }

        setState(() {
          marker = marker;
          //  _polyLines.add(polyline);
        });
      },
    );

initialCameraPosition

      initialCameraPosition: CameraPosition(target: centerLatLng, zoom: 1.0) //지도 생성직후의 카메라 위치.

지도 생성 직후에 카메라가 위치하는 곳이다.

LatLngBounds

    controller.animateCamera(
      CameraUpdate.newLatLngBounds(
        bounds,
         32.0,
      ),
    );

여기서 bounds 는 LatLngBounds이다.
값으로는 북동좌표와 남서좌표를 가진다.
위 두 좌표를 통하여 사각형을 그리고 googleMap이 사각형에 딱 맞도록 움직인다.

여기서 발견한 추가 팁?!

marker.addAll([
            for (int index = 0; index < widget.snapshot.data.stopPlaces.length; index++)
              Marker(
                  ~~~
                  )
          ]);

addAll 은 인자값으로 주어진 리스트를 전부 추가하는데,
리스트 안에 for문 작성이 가능하다..!

위 코드로 인한 구글맵은
생성된 후 initailCamera위치에서 시작해서
bounds로 설정된 카메라 크기만큼 확대된다.

profile
피아노 배우고 싶다

0개의 댓글