Flutter谷歌地图网络服务插件rebi_google_maps_webservice的使用

Flutter谷歌地图网络服务插件google_maps_webservice的使用


Build Status codecov Star on Github Buy Me A Coffee

google_maps_webservice

一般信息

这是Google Maps Web服务的Dart库。 你可以在这里找到Google Maps平台文档,但如果你是新手,你可能想从这里开始。

API密钥

要使用此库,你需要一个Web API密钥。请遵循这些步骤来获取适用于你的特定Dart应用程序的密钥。

这些密钥不能单独用作Android或iOS API密钥,而是用于你的Dart应用程序。

可用的API

使用

地理编码

import "package:google_maps_webservice/geocoding.dart";

final geocoding = GoogleMapsGeocoding(apiKey: "<API_KEY>");
final geocoding = GoogleMapsGeocoding(apiKey: "<API_KEY>", httpClient: BrowserClient());
final geocoding = GoogleMapsGeocoding(baseUrl: "http://myProxy.com");

GeocodingResponse response = await geocoding.searchByAddress("1600 Amphitheatre Parkway, Mountain View, CA");

地点

import "package:google_maps_webservice/places.dart";

final places = GoogleMapsPlaces(apiKey: "<API_KEY>");
final places = GoogleMapsPlaces(apiKey: "<API_KEY>", httpClient: BrowserClient());
final places = GoogleMapsPlaces(baseUrl: "http://myProxy.com");

PlacesSearchResponse response = await places.searchNearbyWithRadius(Location(lat: 31.0424, lng: 42.421), 500);
PlacesSearchResponse response = await places.searchNearbyWithRankby(Location(lat: 31.0424, lng: 42.421), "distance");
PlacesSearchResponse response = await places.searchByText("123 Main Street");

PlacesDetailsResponse response = await places.getDetailsByPlaceId("PLACE_ID");
PlacesDetailsResponse response = await places.getDetailsByReference("REF");

时区

import "package:google_maps_webservice/timezone.dart";

final timezone = GoogleMapsTimezone(apiKey: "<API_KEY>");
final timezone = GoogleMapsTimezone(apiKey: "<API_KEY>", httpClient: BrowserClient());
final timezone = GoogleMapsTimezone(baseUrl: "http://myProxy.com");

TimezoneResponse response = await timezone.getByLocation(Location(lat: 31.0424, lng: 42.421));
TimezoneResponse response = await timezone.getByLocation(Location(lat: 31.0424, lng: 42.421), timestamp: DateTime.utc(2019, 4, 24));
TimezoneResponse response = await timezone.getByLocation(Location(lat: 31.0424, lng: 42.421), timestamp: DateTime.utc(2019, 4, 24), language: 'es');

静态地图

import "package:google_maps_webservice/static_map.dart";

String apiKey = "<API_KEY>";

StaticMap mapStatic = StaticMap(
  apiKey,
  markers: List.from([
    Location(lat: 23.721160, lng: 90.394435),
    Location(lat: 23.732322, lng: 90.385142),
  ]),
  path: Path(
    enc: 'svh~F`j}uOusC`bD',
    color: 'black',
  ),
  scale: 'false'
);

String url = mapStatic.getUrl();

Image.network(url);

更多关于Flutter谷歌地图网络服务插件rebi_google_maps_webservice的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter谷歌地图网络服务插件rebi_google_maps_webservice的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


rebi_google_maps_webservice 是一个Flutter插件,用于与Google Maps Web服务进行交互。它允许你在Flutter应用中访问Google Maps的各种Web服务,如地理编码、反向地理编码、地点搜索、路线规划等。

安装插件

首先,你需要在 pubspec.yaml 文件中添加 rebi_google_maps_webservice 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  rebi_google_maps_webservice: ^latest_version

然后运行 flutter pub get 来安装插件。

获取API密钥

在使用Google Maps Web服务之前,你需要在Google Cloud Console中启用相应的API,并获取一个API密钥。

  1. 访问 Google Cloud Console
  2. 创建一个新项目或选择一个已有项目。
  3. 启用所需的API(如Geocoding API、Places API、Directions API等)。
  4. 在“凭据”页面中创建一个API密钥。

使用插件

以下是一些常见的使用示例:

1. 初始化插件

在使用插件之前,你需要使用你的API密钥进行初始化。

import 'package:rebi_google_maps_webservice/rebi_google_maps_webservice.dart';

final googleMaps = GoogleMapsWebservice(apiKey: 'YOUR_API_KEY');

2. 地理编码(Geocoding)

地理编码是将地址转换为经纬度的过程。

void geocodeAddress() async {
  final response = await googleMaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA');

  if (response.isOkay) {
    final location = response.results.first.geometry.location;
    print('Latitude: ${location.lat}, Longitude: ${location.lng}');
  } else {
    print('Error: ${response.errorMessage}');
  }
}

3. 反向地理编码(Reverse Geocoding)

反向地理编码是将经纬度转换为地址的过程。

void reverseGeocode() async {
  final response = await googleMaps.reverseGeocode(Location(lat: 37.4219999, lng: -122.0840575));

  if (response.isOkay) {
    final address = response.results.first.formattedAddress;
    print('Address: $address');
  } else {
    print('Error: ${response.errorMessage}');
  }
}

4. 地点搜索(Places Search)

地点搜索允许你根据关键字搜索地点。

void searchPlaces() async {
  final response = await googleMaps.places.search('restaurant in New York');

  if (response.isOkay) {
    for (var place in response.results) {
      print('Place: ${place.name}, Address: ${place.formattedAddress}');
    }
  } else {
    print('Error: ${response.errorMessage}');
  }
}

5. 路线规划(Directions)

路线规划用于获取两个地点之间的路线信息。

void getDirections() async {
  final origin = Location(lat: 37.4219999, lng: -122.0840575);
  final destination = Location(lat: 34.052235, lng: -118.243683);

  final response = await googleMaps.directions(origin, destination);

  if (response.isOkay) {
    final route = response.routes.first;
    print('Distance: ${route.legs.first.distance.text}');
    print('Duration: ${route.legs.first.duration.text}');
  } else {
    print('Error: ${response.errorMessage}');
  }
}
回到顶部