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

发布于 1周前 作者 songsunli 来自 Flutter

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

General Information(通用信息)

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

这个库受到了google_maps_webservice包的启发。

现在支持空安全。

API Key(API密钥)

要使用此库,你需要一个Web API密钥。按照这些步骤获取与你的特定Dart应用程序相关的密钥。

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

Availables API(可用的API)

Usage(使用方法)

Geocoding(地理编码)

import "package:google_maps_webservice_ex/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");

Places(地点)

import "package:google_maps_webservice_ex/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");

Timezone(时区)

import "package:google_maps_webservice_ex/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');

Static Map(静态地图)

import 'package:google_maps_webservice_ex/static_map.dart';

void main() {
  final 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',            // 缩放比例
  );

  // 获取地图URL
  String url = mapStatic.getUrl();

  // 使用Image.network加载地图
  print(url); // 输出地图URL
}

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

1 回复

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


google_maps_webservice 是一个用于与 Google Maps Web Services 进行交互的 Flutter 插件。它允许你在 Flutter 应用中使用 Google Maps 的 API,如 Places API、Geocoding API、Directions API 等。

安装插件

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

dependencies:
  flutter:
    sdk: flutter
  google_maps_webservice: ^0.0.20

然后运行 flutter pub get 来安装依赖。

获取 Google Maps API 密钥

在使用 google_maps_webservice 之前,你需要在 Google Cloud Platform 上启用相应的 API 并获取 API 密钥。

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

使用插件

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

1. Geocoding API

Geocoding API 用于将地址转换为地理坐标(纬度/经度),或者将地理坐标转换为地址。

import 'package:google_maps_webservice/geocoding.dart';
import 'package:google_maps_webservice/utils/network_error.dart';

void main() async {
  final geocoding = GoogleMapsGeocoding(apiKey: 'YOUR_API_KEY');

  try {
    var response = await geocoding.searchByAddress('1600 Amphitheatre Parkway, Mountain View, CA');

    if (response.isOkay) {
      var location = response.results[0].geometry.location;
      print('Latitude: ${location.lat}, Longitude: ${location.lng}');
    } else {
      print('Error: ${response.errorMessage}');
    }
  } on NetworkError catch (e) {
    print('Network error: $e');
  }
}

2. Places API

Places API 用于搜索地点、获取地点详情等。

import 'package:google_maps_webservice/places.dart';
import 'package:google_maps_webservice/utils/network_error.dart';

void main() async {
  final places = GoogleMapsPlaces(apiKey: 'YOUR_API_KEY');

  try {
    var response = await places.searchNearbyWithRadius(
      Location(lat: 37.4219999, lng: -122.0840575),
      500,
    );

    if (response.isOkay) {
      for (var place in response.results) {
        print('Place: ${place.name}, Location: ${place.geometry.location}');
      }
    } else {
      print('Error: ${response.errorMessage}');
    }
  } on NetworkError catch (e) {
    print('Network error: $e');
  }
}

3. Directions API

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

import 'package:google_maps_webservice/directions.dart';
import 'package:google_maps_webservice/utils/network_error.dart';

void main() async {
  final directions = GoogleMapsDirections(apiKey: 'YOUR_API_KEY');

  try {
    var response = await directions.directionsWithLocation(
      Location(lat: 37.4219999, lng: -122.0840575),
      Location(lat: 34.052235, lng: -118.243683),
    );

    if (response.isOkay) {
      var route = response.routes[0];
      print('Distance: ${route.legs[0].distance.text}');
      print('Duration: ${route.legs[0].duration.text}');
    } else {
      print('Error: ${response.errorMessage}');
    }
  } on NetworkError catch (e) {
    print('Network error: $e');
  }
}
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!