我正在嘗試為我的應用程式實作 Google 地圖示記功能。通過使用坐標。
它作業正常。但是地圖中未顯示標記/指標。只有空地圖在全屏上呈現
有什么辦法可以解決這個問題??
完整代碼
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class GoogleMapScreen extends StatefulWidget {
const GoogleMapScreen({Key? key}) : super(key: key);
@override
_GoogleMapScreenState createState() => _GoogleMapScreenState();
}
class _GoogleMapScreenState extends State<GoogleMapScreen> {
late GoogleMapController mapController;
Set<Marker> markers = {};
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
void onMapcreated(GoogleMapController controller) {
setState(() {
markers.add(const Marker(
markerId: MarkerId('1'),
position: LatLng(13.007488, 77.598656),
infoWindow: InfoWindow(
title: 'Marker Title Second ',
snippet: 'My Custom Subtitle',
),
));
markers.add(const Marker(
markerId: MarkerId('2'),
position: LatLng(12.999595, 77.585856),
infoWindow: InfoWindow(
title: 'Marker Title Third ',
snippet: 'My Custom Subtitle',
),
));
markers.add(const Marker(
markerId: MarkerId('3'),
position: LatLng(13.001916, 77.588849),
infoWindow: InfoWindow(
title: 'Marker Title Fourth ',
snippet: 'My Custom Subtitle',
),
));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
extendBody: true,
extendBodyBehindAppBar: true,
body: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: GoogleMap(
initialCameraPosition: const CameraPosition(
target: LatLng(36.736847, 4.045026),
zoom: 15,
),
mapToolbarEnabled: true,
buildingsEnabled: false,
myLocationButtonEnabled: true,
mapType: MapType.normal,
markers: markers,
onMapCreated: onMapcreated,
),
),
);
}
}
輸出

我為 Google API 使用了 Empty API 密鑰。
而且我還需要隱藏地圖上的加號、減號和著作權
uj5u.com熱心網友回復:
- 您需要擁有Google Map API Key 否則您將看到一個空白螢屏。
我用谷歌 api 密鑰嘗試了你的代碼機器,它作業正常。這是我使用的代碼(嘗試復制粘貼并檢查)
2.用于隱藏縮放按鈕使用zoomControlsEnabled: true,
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class GoogleMapScreen extends StatefulWidget {
const GoogleMapScreen({Key? key}) : super(key: key);
@override
_GoogleMapScreenState createState() => _GoogleMapScreenState();
}
class _GoogleMapScreenState extends State<GoogleMapScreen> {
late GoogleMapController mapController;
Set<Marker> markers = {};
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
void onMapcreated(GoogleMapController controller) {
setState(() {
markers.add(const Marker(
markerId: MarkerId('1'),
position: LatLng(13.007488, 77.598656),
infoWindow: InfoWindow(
title: 'Marker Title Second ',
snippet: 'My Custom Subtitle',
),
));
markers.add(const Marker(
markerId: MarkerId('2'),
position: LatLng(13.007481, 77.598651),
infoWindow: InfoWindow(
title: 'Marker Title Third ',
snippet: 'My Custom Subtitle',
),
));
markers.add(const Marker(
markerId: MarkerId('3'),
position: LatLng(13.001916, 77.588849),
infoWindow: InfoWindow(
title: 'Marker Title Fourth ',
snippet: 'My Custom Subtitle',
),
));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
extendBody: true,
extendBodyBehindAppBar: true,
body: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: GoogleMap(
initialCameraPosition: const CameraPosition(
target: LatLng(13.007481, 77.598656),
zoom: 10,
),
mapToolbarEnabled: true,
buildingsEnabled: false,
myLocationButtonEnabled: true,
mapType: MapType.normal,
markers: markers,
onMapCreated: onMapcreated,
zoomControlsEnabled: true,
),
),
);
}
}
注意: 我猜問題是您的目標 LatLng指的是不同的位置,另一個原因可能是您沒有提供 API 密鑰。
您之前的目標是36.736847, 4.045026我更改為13.007488, 77.598656最接近您的標記,包括從 15 到 10 的縮放值
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/427064.html
上一篇:地圖未顯示在Laravel專案中
下一篇:谷歌地圖-遍歷陣列以獲取折線
