如何在颤振中从谷歌地图获取位置的经纬度坐标?
Posted
技术标签:
【中文标题】如何在颤振中从谷歌地图获取位置的经纬度坐标?【英文标题】:How to get lat and long coordinates on location from google maps in flutter? 【发布时间】:2019-09-27 22:29:26 【问题描述】:我使用的是 Flutter 1.3** 版本,它没有在 GoogleMap()
中定义 markers
语法
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
及以上方法将经纬度转换为字符串值?
【问题讨论】:
【参考方案1】: 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 中的这个方法
【讨论】:
【参考方案2】:你可以使用 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());
【讨论】:
【参考方案3】:此代码可能对您有所帮助。
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));
【讨论】:
它不能回答我的问题,这个答案只显示标记不能选择位置,不能选择经纬度。以上是关于如何在颤振中从谷歌地图获取位置的经纬度坐标?的主要内容,如果未能解决你的问题,请参考以下文章