地理位置标记如何添加到地图?

时间:2019-01-30 20:32:01

标签: google-maps dart flutter

我制作了一个带有地图的屏幕,该屏幕上曾经是我的位置标记和我在地图上选择坐标的标记。如何在地图上标记我的位置?

class MapsDemo extends StatefulWidget {
  @override
  State createState() => MapsDemoState();
}

class MapsDemoState extends State<MapsDemo> {
var currentLocation;

  GoogleMapController mapController;
  _getLocation() async {
    var location = new Location();
    try {
      currentLocation = await location.getLocation();

      print("locationLatitude: ${currentLocation["latitude"]}");
      print("locationLongitude: ${currentLocation["longitude"]}");
      setState(
              () {
              mapController.addMarker(
                MarkerOptions(
                  position: LatLng(37.421971, -122.084056),
                  icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed),
                ),

              );
              mapController.addMarker(
                MarkerOptions(
                  position: LatLng(currentLocation["latitude"],currentLocation["longitude"]),
                  icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed),
                ),
              );});
    } on Exception {
       currentLocation = null;
    }
  }

  @override
  void initState() {
    _getLocation();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GoogleMap(
    options:GoogleMapOptions(
                  myLocationEnabled: true,
              ),
    );
  }
}

显示带有我选择的坐标的标记,但是没有显示带有我的位置的标记。

非常感谢您的协助。

1 个答案:

答案 0 :(得分:0)

使用位置package来检测将为您提供经度坐标的位置,并且您可以像在代码中一样添加标记。

相关问题