Flutter谷歌地图网络服务插件google_maps_webservice的使用
Flutter谷歌地图网络服务插件google_maps_webservice的使用
General Information
这是用于Google Maps Web服务的Dart库。 你可以在这里找到Google Maps平台文档,但如果你是新手,你可能想从这里开始。
API Key
要使用此库,你需要一个Web API密钥。请遵循这些步骤来获取与你的特定Dart应用相关的密钥。
这些密钥不能单独用作Android或iOS API密钥,而是应该在你的Dart应用中使用。
Availables API
- ✅ Geocoding
- ❌ Places
- ✅ nearby search
- ✅ text search
- ✅ details
- ❌ add
- ❌ delete
- ✅ photo
- ✅ autocomplete
- ✅ queryautocomplete
- ✅ Directions
- ✅ Distance Matrix
- ❌ Geolocation
- ❌ Elevation
- ❌ Roads
- ✅ Timezone
- ✅ Static Map
Usage
Geocoding
import "package:google_maps_webservice/geocoding.dart";
final geocoding = new GoogleMapsGeocoding(apiKey: "<API_KEY>");
final geocoding = new GoogleMapsGeocoding(apiKey: "<API_KEY>", httpClient: new BrowserClient());
final geocoding = new GoogleMapsGeocoding(baseUrl: "http://myProxy.com");
GeocodingResponse response = await geocoding.searchByAddress("1600 Amphitheatre Parkway, Mountain View, CA");
Places
import "package:google_maps_webservice/places.dart";
final places = new GoogleMapsPlaces(apiKey: "<API_KEY>");
final places = new GoogleMapsPlaces(apiKey: "<API_KEY>", httpClient: new BrowserClient());
final places = new GoogleMapsPlaces(baseUrl: "http://myProxy.com");
PlacesSearchResponse response = await places.searchNearbyWithRadius(new Location(31.0424, 42.421), 500);
PlacesSearchResponse response = await places.searchNearbyWithRankby(new Location(31.0424, 42.421), "distance");
PlacesSearchResponse response = await places.searchByText("123 Main Street");
PlacesDetailsResponse response = await places.getDetailsByPlaceId("PLACE_ID");
PlacesDetailsResponse response = await places.getDetailsByReference("REF");
Timezone
import "package:google_maps_webservice/timezone.dart";
final timezone = new GoogleMapsTimezone(apiKey: "<API_KEY>");
final timezone = new GoogleMapsTimezone(apiKey: "<API_KEY>", httpClient: new BrowserClient());
final timezone = new GoogleMapsTimezone(baseUrl: "http://myProxy.com");
TimezoneResponse response = await timezone.getByLocation(new Location(31.0424, 42.421));
TimezoneResponse response = await timezone.getByLocation(new Location(31.0424, 42.421), timestamp: DateTime.utc(2019, 4, 24));
TimezoneResponse response = await timezone.getByLocation(new Location(31.0424, 42.421), timestamp: DateTime.utc(2019, 4, 24), language: 'es');
Static Map
import "package:google_maps_webservice/static_map.dart";
// 创建一个静态地图对象
StaticMap mapStatic = StaticMap(
apiKey,
markers: List.from([
Location(23.721160, 90.394435),
Location(23.732322, 90.385142),]
),
path: Path(
enc: 'svh~F`j}uOusC`bD',
color: 'black',
),
scale: 'false'
);
// 获取静态地图的URL
String url = mapStatic.getUrl();
// 显示静态地图
Image.network(url)
更多关于Flutter谷歌地图网络服务插件google_maps_webservice的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter谷歌地图网络服务插件google_maps_webservice的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
google_maps_webservice
是 Flutter 中用于与 Google Maps Web 服务进行交互的一个插件。它允许你在 Flutter 应用中使用 Google Maps 提供的各种网络服务,例如地理编码、反向地理编码、地点搜索、距离矩阵计算等。
以下是如何在 Flutter 项目中使用 google_maps_webservice
插件的基本步骤:
1. 添加依赖
首先,在 pubspec.yaml
文件中添加 google_maps_webservice
插件的依赖:
dependencies:
flutter:
sdk: flutter
google_maps_webservice: ^X.X.X # 使用最新版本
然后运行 flutter pub get
来安装依赖。
2. 获取 API 密钥
在使用 Google Maps Web 服务之前,你需要在 Google Cloud Console 中创建一个项目,并启用所需的 API(例如 Geocoding API、Places API 等)。然后生成一个 API 密钥。
3. 使用插件
地理编码(Geocoding)
地理编码是将地址转换为经纬度的过程。
import 'package:google_maps_webservice/geocoding.dart';
void main() async {
final geocoding = GoogleMapsGeocoding(apiKey: 'YOUR_API_KEY');
var response = await geocoding.searchByAddress('1600 Amphitheatre Parkway, Mountain View, CA');
if (response.isOkay) {
var location = response.results.first.geometry.location;
print('Latitude: ${location.lat}, Longitude: ${location.lng}');
} else {
print('Error: ${response.errorMessage}');
}
}
反向地理编码(Reverse Geocoding)
反向地理编码是将经纬度转换为地址的过程。
import 'package:google_maps_webservice/geocoding.dart';
void main() async {
final geocoding = GoogleMapsGeocoding(apiKey: 'YOUR_API_KEY');
var response = await geocoding.searchByLocation(Location(lat: 37.4219999, lng: -122.0840575));
if (response.isOkay) {
var address = response.results.first.formattedAddress;
print('Address: $address');
} else {
print('Error: ${response.errorMessage}');
}
}
地点搜索(Places Search)
地点搜索允许你根据关键字或类型搜索地点。
import 'package:google_maps_webservice/places.dart';
void main() async {
final places = GoogleMapsPlaces(apiKey: 'YOUR_API_KEY');
var response = await places.searchByText('restaurants in San Francisco');
if (response.isOkay) {
response.results.forEach((place) {
print('Place: ${place.name}, Location: ${place.geometry.location}');
});
} else {
print('Error: ${response.errorMessage}');
}
}
距离矩阵(Distance Matrix)
距离矩阵服务用于计算多个地点之间的距离和时间。
import 'package:google_maps_webservice/distance.dart';
void main() async {
final distance = GoogleMapsDistance(apiKey: 'YOUR_API_KEY');
var response = await distance.distanceWithLocation(
origins: [Location(lat: 37.4219999, lng: -122.0840575)],
destinations: [Location(lat: 34.052235, lng: -118.243683)],
);
if (response.isOkay) {
var distance = response.rows.first.elements.first.distance.text;
var duration = response.rows.first.elements.first.duration.text;
print('Distance: $distance, Duration: $duration');
} else {
print('Error: ${response.errorMessage}');
}
}