gpt4 book ai didi

google-maps - 如何在 flutter 中在两个谷歌地图标记之间缩放

转载 作者:IT王子 更新时间:2023-10-29 06:57:11 31 4
gpt4 key购买 nike

我正在使用 google_maps_flutter 包,我正在尝试想办法在两个已知位置的已放置标记之间缩放相机。任何指针或示例代码将不胜感激。

最佳答案

要在 google map 中的两个 Lat Lng 边界之间缩放,您可以执行以下操作:

首先在 pubspec.yaml 中导入下面的库,否则对于旧版本,您可能无法通过谷歌看到“getVisibleRegion()”方法 map Controller 。

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,
),
),
);
}
}

关于google-maps - 如何在 flutter 中在两个谷歌地图标记之间缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55989773/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com