flutter 在谷歌Map上放置小部件以获得位置抖动

mwkjh3gx  于 2023-01-27  发布在  Flutter
关注(0)|答案(1)|浏览(116)

hi there和早上好,什么是最好的方式来放置一个标记在屏幕上,并移动谷歌Map到该标记,以获得用户所需的位置
比如送餐应用和拼车应用,有没有人有主意?

dddzy1tm

dddzy1tm1#

应该是这样的:

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

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

  @override
  State<MyMap> createState() => _MyMapState();
}

class _MyMapState extends State<MyMap> {
  CameraPosition initialCameraPosition = const CameraPosition(
    target: LatLng(37.789682, -122.3901086),
    zoom: 14,
  );
  final Set<Marker> _markers = {};
  LatLng _lastMapPosition = const LatLng(37.789682, -122.3901086);
  GoogleMapController? googleMapController;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: true,
      body: Stack(
        children: [
          GoogleMap(
            onMapCreated: (GoogleMapController controller) {
              googleMapController = controller;
            },
            markers: _markers,
            mapType: MapType.normal,
            initialCameraPosition: initialCameraPosition,
            compassEnabled: true,
            mapToolbarEnabled: true,
            onCameraMove: _onCameraMove,
          ),
          Center(
            child: Container(
              child: const Icon(Icons.circle_outlined,
                  color: Colors.blue, size: 25),
            ),
          ),
        ],
      ),
    );
  }

  void _onCameraMove(CameraPosition p) {
    // this is the position of the marker on the top of you Map
    // TODO : change print to what you went to do with the new location.
    print("New Loctaion ${p.target.longitude}, ${p.target.latitude}");
    _lastMapPosition = p.target;
  }
}

**注意:**记住要将Google Map API添加到您的项目中,才能使其工作。

相关问题