栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Flutter中的两个Google地图标记之间缩放

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Flutter中的两个Google地图标记之间缩放

要在Google地图中的两个Lat Lng边界之间缩放,请执行以下操作:

首先,在pubspec.yaml中的库下面导入,否则对于较旧的版本,您可能无法使用Google Map Controller看到“
getVisibleRegion()”方法。

google_maps_flutter:^ 0.5.12

import 'dart:async';import 'package:flutter/material.dart';import 'package:google_maps_flutter/google_maps_flutter.dart';void main() => runApp(MyApp());class MyApp extends StatefulWidget {  @override  _MyAppState createState() => _MyAppState();}class _MyAppState extends State<MyApp> {  Completer<GoogleMapController> _controller = Completer();  GoogleMapController mapController;  LatLng _lastMapPosition = _center;  static const LatLng _center = const LatLng(45.521563, -122.677433);  final Set<Marker> _markers = {};  void _onMapCreated(GoogleMapController controller) {    mapController = controller;    _controller.complete(controller);    LatLng latLng_1 = LatLng(40.416775, -3.70379);    LatLng latLng_2 = LatLng(41.385064, 2.173403);    LatLngBounds bound = LatLngBounds(southwest: latLng_1, northeast: latLng_2);    setState(() {      _markers.clear();      addMarker(latLng_1, "Madrid", "5 Star Rating");      addMarker(latLng_2, "Barcelona", "7 Star Rating");    });    CameraUpdate u2 = CameraUpdate.newLatLngBounds(bound, 50);    this.mapController.animateCamera(u2).then((void v){      check(u2,this.mapController);    });  }  void addMarker(LatLng mLatLng, String mTitle, String mDescription){    _markers.add(Marker(      // This marker id can be anything that uniquely identifies each marker.      markerId: MarkerId((mTitle + "_" + _markers.length.toString()).toString()),      position: mLatLng,      infoWindow: InfoWindow(        title: mTitle,        snippet: mDescription,      ),      icon: BitmapDescriptor.defaultMarker,    ));  }  void check(CameraUpdate u, GoogleMapController c) async {    c.animateCamera(u);    mapController.animateCamera(u);    LatLngBounds l1=await c.getVisibleRegion();    LatLngBounds l2=await c.getVisibleRegion();    print(l1.toString());    print(l2.toString());    if(l1.southwest.latitude==-90 ||l2.southwest.latitude==-90)      check(u, c);  }  void _onCameraMove(CameraPosition position) {    _lastMapPosition = position.target;  }  @override  Widget build(BuildContext context) {    return MaterialApp(      home: Scaffold(        appBar: AppBar(          title: Text('Maps Sample App'),          backgroundColor: Colors.green[700],        ),        body: GoogleMap(          markers: _markers,          onMapCreated: _onMapCreated,          initialCameraPosition: CameraPosition( target: _center, zoom: 11.0,          ),          onCameraMove: _onCameraMove,        ),      ),    );  }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/371050.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号