【问题标题】:How can I get tapped location in flutter_map?如何在flutter_map中获得点击位置?
【发布时间】:2021-05-10 02:32:22
【问题描述】:

我正在我的代码中实现一个地图。到目前为止,这就是我所拥有的:

import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_tappable_polyline/flutter_map_tappable_polyline.dart';
import 'package:latlong/latlong.dart';
import 'package:geolocator/geolocator.dart';

class MapPicker extends StatefulWidget {
  MapPicker({Key key}) : super(key: key);

  @override
  _MapPicker createState() {
    return _MapPicker();
  }
}

class _MapPicker extends State<MapPicker> {
  Position _position;

  void getLocation() async {
    var position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    print(position);
    setState(() {
      _position = position;
    });
  }

  @override
  Widget build(BuildContext context) {
    if(_position == null)
      getLocation();
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text('Escoger Localización'),
      ),
      body: _position != null ? FlutterMap(
        options: MapOptions(
          center: LatLng(18.0119098, -66.6159138), //Change to _position
          zoom: 15.0
        ),
        layers: [
          TileLayerOptions(
              urlTemplate:
              "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
              subdomains: ['a', 'b', 'c']),
          MarkerLayerOptions(
            markers: [],
          ),
          TappablePolylineLayerOptions(
            onTap: (TaggedPolyline polyline) => print(polyline.tag)
          )
        ],
      )
          :
      CircularProgressIndicator(),
    );
  }
}

我现在使用一组 LatLon 进行测试,但我的想法是使用 _position 在打开地图时使用当前位置。但是,我的问题如下:我希望能够点击地图中的任何地方并获得我点击的地方的坐标。这可能吗?

【问题讨论】:

    标签: flutter dart fluttermap


    【解决方案1】:

    这可以通过我认为MapOptions() 中的onTap() 回调来实现。它返回被点击的位置。

    【讨论】:

      【解决方案2】:
      options: new MapOptions(
                  onTap: (tapPosition, point) => {
                    print(point.toString()),
                  },
                  center: LatLng(0, 0),
                  zoom: 1,
                  maxZoom: 19,
                ),
      

      【讨论】:

      • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      • 2015-06-04
      • 2021-06-11
      • 2021-06-10
      • 2021-08-12
      • 1970-01-01
      相关资源
      最近更新 更多