1

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

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在打开地图时使用当前位置。但是,我的问题如下:我希望能够点击地图中的任何地方并获得我点击的地方的坐标。这可能吗?

4

2 回答 2

1

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

于 2021-07-28T12:11:16.633 回答
0
options: new MapOptions(
            onTap: (tapPosition, point) => {
              print(point.toString()),
            },
            center: LatLng(0, 0),
            zoom: 1,
            maxZoom: 19,
          ),
于 2022-01-06T07:30:09.973 回答