如何从Google Maps中获取位置的经纬度坐标?

问题描述 投票:0回答:3

我正在使用Flutter 1.3 **版本,在markers中未定义GoogleMap()语法

GoogleMap(
  onMapCreated: _onMapCreated,
  markers: markers,
)

所以我使用下面的代码尝试从地图上获取经度和纬度

 void _onMapCreated(GoogleMapController controller){
    this._controller = controller;
    controller.onMarkerTapped.add(_onMarkerTapped);
          }

  void _onMarkerTapped(Marker marker) {
         ...
           }

Widget build(BuildContext context) {
   GoogleMap(
        onMapCreated: _onMapCreated,
        options: GoogleMapOptions(
          ...
        ));
      }

我的问题是如何使用MarkerOptions及以上方法将经纬度转换为字符串值?

google-maps flutter geolocation location google-maps-markers
3个回答
0
投票
此代码可能对您有帮助。

import 'dart:async'; import 'package:flutter/material.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Google Maps Demo', home: MapSample(), ); } } class MapSample extends StatefulWidget { @override State<MapSample> createState() => MapSampleState(); } class MapSampleState extends State<MapSample> { Completer<GoogleMapController> _controller = Completer(); static final CameraPosition _kGooglePlex = CameraPosition( target: LatLng(37.42796133580664, -122.085749655962), zoom: 14.4746, ); static final CameraPosition _kLake = CameraPosition( bearing: 192.8334901395799, target: LatLng(37.43296265331129, -122.08832357078792), tilt: 59.440717697143555, zoom: 19.151926040649414); @override Widget build(BuildContext context) { return new Scaffold( body: GoogleMap( mapType: MapType.hybrid, initialCameraPosition: _kGooglePlex, onMapCreated: (GoogleMapController controller) { _controller.complete(controller); }, ), floatingActionButton: FloatingActionButton.extended( onPressed: _goToTheLake, label: Text('To the lake!'), icon: Icon(Icons.directions_boat), ), ); } Future<void> _goToTheLake() async { final GoogleMapController controller = await _controller.future; controller.animateCamera(CameraUpdate.newCameraPosition(_kLake)); } }


0
投票
LocationData currentLocation; getLocation() async { var location = new Location(); location.onLocationChanged().listen(( currentLocation) { print(currentLocation.latitude); print(currentLocation.longitude); setState(() { latLng = LatLng(currentLocation.latitude, currentLocation.longitude); }); print("getLocation:$latLng"); loading = false; }); }
通过以上代码,您只需获得纬度和经度并在上使用此latLng变量

initialCameraPosition: CameraPosition( target: latLng, zoom: 17.4746, ),

GoogleMap中的此方法

0
投票
您可以使用Marker类的onTap方法获取所需的内容。

希望这会有所帮助!

Map<MarkerId, Marker> markers = <MarkerId, Marker>{}; MarkerId selectedMarker; int _markerIdCounter = 1; void _add() { final int markerCount = markers.length; final String markerIdVal = 'marker_id_$_markerIdCounter'; _markerIdCounter++; final MarkerId markerId = MarkerId(markerIdVal); final Marker marker = Marker( markerId: markerId, position: LatLng( center.latitude, center.longitude ), onTap: () { _onMarkerTapped(markerId); }, ); setState(() { markers[markerId] = marker; }); } void _onMarkerTapped(MarkerId markerId) { final Marker tappedMarker = markers[markerId]; if (tappedMarker != null) { setState(() { if (markers.containsKey(selectedMarker)) { print(selectedMarker.position.toString()); } } }

© www.soinside.com 2019 - 2024. All rights reserved.