Flutter:点击打开Google Maps上的Mapbox符号 [英] Flutter : Mapbox symbol on click open google maps
问题描述
我有一个项目使用
逻辑源代码
onMapClick :(点,纬度){如果(latlng.latitude ==纬度&& latlng.longitude ==经度){launchGoogleMaps(latitude:纬度,经度:经度);}打印(来自地图$ {latlng.latitude} | $ {latlng.latitude} \ n来自服务器$ latitude || $经度\ n \ n);},
我认为,当单击地图上的点彼此靠近时,我可以直接打开Google Maps应用程序.我该怎么做?
完整源代码
class ExampleMapBox扩展了StatefulWidget {@override_ExampleMapBoxState createState()=>_ExampleMapBoxState();}类_ExampleMapBoxState扩展了State< ExampleMapBox>{MapboxMapController mapController;经度,纬度;@override无效的initState(){super.initState();纬度= -6.192461941069894;经度= 106.97593586545025;}void _onMapCreated(MapboxMapController mapboxMapController){mapController = mapboxMapController;}@override窗口小部件build(BuildContext context){返回 MapboxMap(onMapCreated:_onMapCreated,initialCameraPosition:CameraPosition(目标:LatLng(纬度,经度),缩放:10),onStyleLoadedCallback: () =>addSymbol(mapController),onMapClick :(点,纬度){如果(latlng.latitude ==纬度&& latlng.longitude ==经度){launchGoogleMaps(latitude:纬度,经度:经度);}打印(来自地图$ {latlng.latitude} | $ {latlng.latitude} \ n来自服务器$ latitude || $经度\ n \ n);},);}void addSymbol(MapboxMapController mapBoxController){mapBoxController.addSymbol(SymbolOptions(几何:LatLng(纬度,经度),iconImage:"assets/images/custom-icon.png",iconSize:2),);}void launchGoogleMaps({@要求双纬,@要求双经度})异步{字符串googleUrl ='https://www.google.com/maps/search/?api=1&query=$latitude,$longitude';如果(await canLaunch(googleUrl)){等待启动(googleUrl);} 别的 {抛出无法打开地图";}}}
通过单击地图来生成精确到15位精度的完全相同的坐标是不可能的.
我建议计算纬度和经度的偏差,并在偏差低于某个阈值时触发launchGoogleMaps函数.请尝试以下类似的方法:
deviation_lat =(latlng.latitude-纬度)*(latlng.latitude-纬度);iation_lon =(纬度-经度)*(纬度-经度);如果(deviation_lat< 1&&偏差_lon< 1){launchGoogleMaps(latitude:纬度,经度:经度);}
I have project using MapBox to show address location in map. I have latitude and longitude (-6.192461941069894,106.97593586545025) like this , i want if i click right on marker, i want open Google Maps Apps based on latitude and longitude i have. But the problem , i can't open google maps after click symbol because latitude&longitude after click maps not same with latitude&longitude i have.
Logic Source Code
onMapClick: (point, latlng) {
if (latlng.latitude == latitude && latlng.longitude == longitude) {
launchGoogleMaps(latitude: latitude, longitude: longitude);
}
print(
"From Map ${latlng.latitude} |${latlng.latitude} \nFrom Server $latitude||$longitude \n\n");
},
I think when clicking points on the map are close to each other, I can directly open the Google Maps application. How i can do this ?
Full Source Code
class ExampleMapBox extends StatefulWidget {
@override
_ExampleMapBoxState createState() => _ExampleMapBoxState();
}
class _ExampleMapBoxState extends State<ExampleMapBox> {
MapboxMapController mapController;
double latitude, longitude;
@override
void initState() {
super.initState();
latitude = -6.192461941069894;
longitude = 106.97593586545025;
}
void _onMapCreated(MapboxMapController mapboxMapController) {
mapController = mapboxMapController;
}
@override
Widget build(BuildContext context) {
return MapboxMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(target: LatLng(latitude, longitude), zoom: 10),
onStyleLoadedCallback: () => addSymbol(mapController),
onMapClick: (point, latlng) {
if (latlng.latitude == latitude && latlng.longitude == longitude) {
launchGoogleMaps(latitude: latitude, longitude: longitude);
}
print(
"From Map ${latlng.latitude} |${latlng.latitude} \nFrom Server $latitude||$longitude \n\n");
},
);
}
void addSymbol(MapboxMapController mapBoxController) {
mapBoxController.addSymbol(
SymbolOptions(
geometry: LatLng(latitude, longitude),
iconImage: "assets/images/custom-icon.png",
iconSize: 2,
),
);
}
void launchGoogleMaps({@required double latitude, @required double longitude}) async {
String googleUrl = 'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude';
if (await canLaunch(googleUrl)) {
await launch(googleUrl);
} else {
throw 'Could Not Open The Map';
}
}
}
Generating the exact same coordinates up to a 15 digit precision by clicking on a map is far from possible.
I would recommend to compute the deviation in latitude and longitude and fire the launchGoogleMaps function when the deviation is below a certain threshold. Please try something like the below:
deviation_lat = (latlng.latitude - latitude) * (latlng.latitude - latitude);
deviation_lon = (latlng.longitude - longitude) * (latlng.longitude - longitude);
if (deviation_lat < 1 && deviation_lon < 1) {
launchGoogleMaps(latitude: latitude, longitude: longitude);
}
这篇关于Flutter:点击打开Google Maps上的Mapbox符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!