我正在开发一个 flutter 应用程序,该应用程序应该有一个适用于网络和移动设备的通用代码库。
我的应用程序将有一个谷歌地图,据我所知,没有一个包可以满足所有平台。
google_maps_flutter - seems to work only for mobile (IOS / Android)
google_maps_flutter_web - seems to work only for web
Run Code Online (Sandbox Code Playgroud)
因此,很可能我必须使用这些单独的包创建两个单独的 MapWidget,一个用于网络,一个用于移动设备。
对于移动设备:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class MapSample extends StatefulWidget {
MapSample({Key? key}) : super(key: key);
@override
State<MapSample> createState() => MapSampleState();
}
class MapSampleState extends State<MapSample> {
final Completer<GoogleMapController> _controller = Completer();
static const CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(37.42796133580664, -122.085749655962),
zoom: 14.4746,
);
@override
Widget build(BuildContext context) {
return GoogleMap(
mapType: MapType.hybrid,
initialCameraPosition: …
Run Code Online (Sandbox Code Playgroud) 我正在尝试向谷歌地图添加虚线折线,但模式属性似乎不起作用,或者我做错了。
下面你可以看到创建折线的方法,图案设置为dash
5px gap
,但它仍然显示为一条实线。它有什么问题还是只是一个颤振错误?
谢谢。
包:google_maps_flutter
...
_addPollyline(int index, Color color) {
final String polylineIdVal = 'polyline_id_$_polylineIdCounter';
_polylineIdCounter++;
final PolylineId polylineId = PolylineId(polylineIdVal);
final Polyline polyline = Polyline(
polylineId: polylineId,
consumeTapEvents: true,
color: color,
patterns: <PatternItem>[PatternItem.dash(5), PatternItem.gap(5)],
width: 5,
points: _createRoute(index),
);
setState(() {
_mapPolylines[polylineId] = polyline;
});
}
...
Run Code Online (Sandbox Code Playgroud)
更新
模式在 android 上运行良好。我测试过他们上的像素3仿真器和两个dash
和dot
模式工作。
该问题仅存在于 iOS 设备上
试图用Sukhi修复该错误,但他无法重现该错误。我的同事们都可以复制它,但是无法修复它:P。还有其他人设法复制它吗?
还用我的代码创建了一个github存储库,并更新了“到目前为止我尝试过的”
我的问题仅发生在调试和发布的真实设备(而非模拟器)上的IO上。
我的确使用带有某些标记的google Maps小部件。轻按“标记”会打开另一个屏幕,其中包含更多信息,另一个Google Maps小部件以及通过URL启动器进行导航的可能性。(我将屏幕缩小为仅显示导致问题的小部件)
从该屏幕离开应用程序(例如,点击开始导航或转到IOs主屏幕),然后返回该应用程序会导致问题。
如果我再次返回到应用程序主屏幕,则仅显示白色屏幕。
将Google Maps Widget包装在Flex Widget(列或行)的信息屏幕中,甚至会导致更糟的行为。返回到Apps HomeScreen时,Flex窗口小部件的其他内容(例如带有文本的容器)将保持可见。
安装google_maps_flutter 0.5.20 + 1和url_launcher:5.0.3(我知道它不是最新的,但是那不是问题)
在不稳定的项目中复制CodeSnippet并在IO上构建。
点击标记
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:io';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root …
Run Code Online (Sandbox Code Playgroud) 我使用下面的代码创建了一个带有标记和折线的 Google 地图小部件。
一切工作正常,除了当我在两个坐标之间绘制多段线并稍后尝试删除它时,多段线不会被删除(至少不会永久删除)。当我使用下面的方法尝试删除旧的折线并添加新的折线时,我得到两条折线:
_polyLine.clear()
_polyLine.remove("id")
polylines.removeWhere((m) => m.polylineId.value == 'polyId$_randPolyId')
Run Code Online (Sandbox Code Playgroud)
它被删除,然后除了新的之外又被添加。结果如下图所示:
void showPinsOnMap() {
// _polylines.removeWhere(
// (m) => m.polylineId.value == 'polyId$_randPolyId');
// _polylines.clear()
_polylines = {};
print("\n\n\nPolyLines Lenght: ${_polylines.length}\n\n\n");
var pinPosition = LatLng(_currentLocation.latitude,_currentLocation.longitude);
var destPosition = LatLng(_destinationLocation.latitude,
_destinationLocation.longitude);
_markers.add(Marker(
markerId: MarkerId('sourcePin'),
position: pinPosition,
icon: _sourceIcon
));
_markers.add(Marker(
markerId: MarkerId('destPin'),
position: destPosition,
icon: _destinationIcon,
));
setPolylines();
notifyListeners();
}
Run Code Online (Sandbox Code Playgroud)
...
...
void setPolylines() async {
List<PointLatLng> result = await _polylinePoints.getRouteBetweenCoordinates(
_googleAPIKey,
_currentLocation.latitude,
_currentLocation.longitude,
_destinationLocation.latitude,
_destinationLocation.longitude).catchError((onError){
print("\n\n\n An Error Occured …
Run Code Online (Sandbox Code Playgroud) 我正在尝试打开带有当前位置的谷歌地图。为此,我使用有状态的小部件。但地图需要很长时间才能加载,有时甚至根本无法加载。这是因为Future
函数(用于获取当前位置)还是我的实现错误?我正在使用google_maps_flutter地图插件。这是我的代码:
@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: <Widget>[
FutureBuilder(
future: _getCurrentLocation(),
builder: (context, snapshot) {
if (snapshot.hasData) {
print("map loaded");
Placemark placemark = snapshot.data;
var json = placemark.toJson();
selectedLocation = TheLocation.fromJson(json);
_markers.add(
Marker(
markerId: MarkerId('homeMarker'),
position: LatLng(
selectedLocation.latitude, selectedLocation.longitude),
icon: BitmapDescriptor.defaultMarker,
infoWindow: InfoWindow(title: selectedLocation.name),
draggable: false,
),
);
return GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(placemark.position.latitude,
placemark.position.longitude),
zoom: 18,
),
markers: _markers,
onMapCreated: onMapCreated,
onCameraMove: ((_position) => _updatePosition(_position)),
);
} else {
print("loading map"); …
Run Code Online (Sandbox Code Playgroud) 我已经在颤振中实现了折线
Map<PolylineId, Polyline> _mapPolylines = {};
int _polylineIdCounter = 1;
void _add() {
final String polylineIdVal = 'polyline_id_$_polylineIdCounter';
_polylineIdCounter++;
final PolylineId polylineId = PolylineId(polylineIdVal);
final Polyline polyline = Polyline(
polylineId: polylineId,
consumeTapEvents: true,
color: Colors.red,
width: 5,
jointType: JointType.round,
points: points, //_createPoints(),
);
setState(() {
_mapPolylines[polylineId] = polyline;
});
}
Run Code Online (Sandbox Code Playgroud)
并将其显示在地图上
return new GoogleMap(
mapType: MapType.normal,
markers: Set.from(Markers),
initialCameraPosition: _kGooglePlex,
polylines: Set<Polyline>.of(_mapPolylines.values),
onMapCreated: (GoogleMapController controller) async {
_controller.complete(controller);
});
Run Code Online (Sandbox Code Playgroud)
我正在使用 google_maps_flutter 包,我试图指示我的路线方向。
我在 Flutter 中使用谷歌地图时遇到问题,
https://pub.dev/packages/google_maps_flutter
我的代码如下:
GoogleMap(
mapType: MapType.normal,
myLocationEnabled: true,
myLocationButtonEnabled: true,
onMapCreated: _onMapCreated,
padding: EdgeInsets.only(top: ScreenUtil().setHeight(100)),
zoomControlsEnabled: false,
initialCameraPosition: CameraPosition(
target: _currentPosition ?? Constants.DEFAULT_POSITION,
zoom: 17,
),
)
Run Code Online (Sandbox Code Playgroud)
第一次运行时,出现位置权限对话框,当我接受权限时,我的位置按钮没有出现。
当我第二次打开googlemap页面时(此时已经授予权限),我的位置显示正常。
谢谢
我想根据圆的半径缩放谷歌地图缩放级别。从技术上讲,我希望用户以最佳缩放级别在地图上看到圆圈。我试图获得边界并调整相机位置,但圆没有任何这样的边界参数..
GoogleMap(
onMapCreated: (GoogleMapController controller) {
controller.showMarkerInfoWindow(MarkerId("0"));
},
circles: Set.from([
Circle(
circleId: CircleId("0"),
center: LatLng(...),
radius: 1000,
)
]),
initialCameraPosition: CameraPosition(
target: LatLng(..),
**zoom: 15, // I want to scale zoom based on radius of circle.**
),
markers: {
Marker(
markerId: MarkerId("0"),
position: LatLng(..),
infoWindow: InfoWindow(title: ...)
)
},
),
),
Run Code Online (Sandbox Code Playgroud)
提前致谢。
嘿,当将 GoogleMaps 添加到我的项目时,似乎每当拖动地图或移动相机时,都会出现大量日志垃圾邮件
I/Counters( 4961): exceeded sample count in FrameTime
Run Code Online (Sandbox Code Playgroud)
这似乎是此处报告的 flutter 中 GoogleMaps 的常见问题。
PS设置myLocationEnabled: false
,不会解决问题,因为手动移动地图时仍然会发生这种情况
class MyMap extends StatelessWidget {
const MyMap({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return GoogleMap(
initialCameraPosition: CameraPosition(
target: state.location ?? const LatLng(0, 0),
zoom: 15.0,
),
mapType: MapType.normal,
myLocationEnabled: false,
zoomGesturesEnabled: true,
scrollGesturesEnabled: true,
tiltGesturesEnabled: false,
rotateGesturesEnabled: true,
zoomControlsEnabled: false,
);
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道是否有解决此问题的最佳方法的更新?谢谢
我正在我当前正在开发的 Flutter 项目中使用 google_maps_flutter: ^2.1.3 包,我想在应用程序的地图上显示活动区域。我尝试使用多边形显示地图上的活动区域,但无法将这些区域之外的部分绘制为不同的颜色。实际上,正是我在地图上想要的,到处都是灰色的(明确它不活跃),但某些区域将是白色的(明确它是活跃的)并被边界包围。看下图你就能明白我想要的是什么。这样一来,地图上就会出现不止一个活动区域。
注意:正如我之前提到的,我可以使用多边形获取此图像,但我无法将多边形的外部设为灰色。