要引入地图插件并在Flutter应用程序中显示自己的位置,可以使用Flutter地图插件如google_maps_flutter或flutter_map。以下是一个简单的步骤:

  1. 在pubspec.yaml文件中添加插件依赖项。例如,要使用google_maps_flutter插件,可以添加以下依赖项:
dependencies:
  flutter:
    sdk: flutter
  google_maps_flutter: ^1.0.6
  1. 在Flutter应用程序中导入插件:
import 'package:google_maps_flutter/google_maps_flutter.dart';
  1. 在应用程序中创建一个GoogleMap小部件,并在其中设置apiKey,以便访问Google地图API:
GoogleMap(
  initialCameraPosition: CameraPosition(
    target: LatLng(37.77483, -122.41942),
    zoom: 12,
  ),
  markers: _markers,
  onMapCreated: (GoogleMapController controller) {
    _controller.complete(controller);
  },
  myLocationEnabled: true,
  myLocationButtonEnabled: true,
  mapType: MapType.normal,
  compassEnabled: true,
  tiltGesturesEnabled: true,
  zoomGesturesEnabled: true,
  ),
  1. 设置myLocationEnabled和myLocationButtonEnabled属性以启用自己的位置,并在应用程序中添加一个标记(_markers)以标识自己的位置:
Marker(
  markerId: MarkerId("myLocation"),
  position: LatLng(position.latitude, position.longitude),
  infoWindow: InfoWindow(title: "My Location"),
),
  1. 使用Geolocator插件获取自己的位置:
Position position = await Geolocator.getCurrentPosition(
  desiredAccuracy: LocationAccuracy.high,
);
  1. 在onMapCreated回调中初始化GoogleMapController,并使用animateCamera方法将地图移动到自己的位置:
onMapCreated: (GoogleMapController controller) {
    _controller.complete(controller);
    controller.animateCamera(
      CameraUpdate.newCameraPosition(
        CameraPosition(
          target: LatLng(position.latitude, position.longitude),
          zoom: 15,
        ),
      ),
    );
},

这样,当应用程序启动时,它将在地图上显示自己的位置。

flutter中 如何引入地图插件 并且显示自己所在位置

原文地址: http://www.cveoy.top/t/topic/bUlT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录