Flutter:如何在Flutter Google Maps中使用纬度来获取地点名称 [英] Flutter: How to get Name of a Place using Lat Long in Flutter Google Maps

查看:116
本文介绍了Flutter:如何在Flutter Google Maps中使用纬度来获取地点名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用警报应用程序,然后发送Google Maps链接,以打开用户在Google Maps Application中的位置.

I'm working on an Alert app then sends a link of Google Maps that will open up user's location in Google Maps Application.

我还希望通过链接在String中发送该地点的地址.

Along with the link I want to send Address of that place in String as well.

例如

纬度:33.038484&长:66.384858

Lat : 33.038484 & Long: 66.384858

让我们说这些是纽约Square Times的坐标

Lets say that these are the co-ordinates of Square Times, New York

我想发送 https://maps.google.com/q=33.038484,66.384858纽约时报广场

是否可以使用纬度长来获取名称/地址?

Is there a way to get the Name/Address using Lat Long?

推荐答案

您可以使用geolocator软件包.这是我使用的示例代码,您应该可以从中得出您的版本:

You can use the geolocator package. Here is example code that I use, you should be able to work out your version from this:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:pedantic/pedantic.dart';
import 'package:rflutter_alert/rflutter_alert.dart';
import 'package:person/generated/i18n.dart';
import 'package:person/models/location_data.dart';
import 'package:person/scoped_models/main.dart';
import 'package:person/shared/global_config.dart' as globals;
import 'package:person/widgets/helpers/dialogue_helper.dart';
import 'package:scoped_model/scoped_model.dart';

final Geolocator _geolocator = Geolocator();
LocationData _currentLocationData;
Position _currentPosition;
String _isoCountryCode = "";

/// This method is used to get the current location of the person.
///
Future<LocationData> getCurrentLocation(
  BuildContext context, {
  Position currentPosition,
  // Will be overridden with false if called from PersonProfilePage and a new
  // user is being added.
  bool updateDatabase = true,
}) async {
  MainModel _model = ScopedModel.of(context);
  String _address = "";
  _currentLocationData = null;
  try {
    // Android shows a dialogue which automatically updates the location
    // permissions settings.
    assert(globals.configFromDatabase != null);
    // Creates a new 'location' object ie. a new instance of this class in the geoloc package
    _currentPosition = await _geolocator.getCurrentPosition(
        desiredAccuracy: globals.configFromDatabase.locationAccuracy);
    _address = await _getLocationAddress(
        _currentPosition.latitude, _currentPosition.longitude);
    _currentLocationData = LocationData(
      latitude: _currentPosition.latitude,
      longitude: _currentPosition.longitude,
      address: _address,
      isoCountryCode: _isoCountryCode,
    );
    // Update the person's current location unless this is a new
    // user being created in the PersonProfilePage. In that case the form data
    // created from this method will be used when the save button is pressed.
    if (updateDatabase) {
      assert(_currentLocationData != null);
      unawaited(_model.updateCurrentLocation(_currentLocationData));
    }
    return _currentLocationData;
  } catch (error) {
    unawaited(
      showAlertDialogue(
        <Your dialogue>
      ),
    );
    return null;
  }
}

/// This method retrieves placemark data based on a latitude and longitude
/// value pair. It then assembles the data items into an address suitable for
/// display in the app and retains the ISO3166-2 Country code in a separate
/// variable for later storage on the database against the person.
///
/// ! The Geolocator package provides this data for free so there is no need
/// ! to pay for the Google Geocoding API.
///
Future<String> _getLocationAddress(double latitude, double longitude) async {
  List<Placemark> newPlace =
      await _geolocator.placemarkFromCoordinates(latitude, longitude);
  Placemark placeMark = newPlace[0];
  String name = placeMark.name;
  // String subLocality = placeMark.subLocality;
  String locality = placeMark.locality;
  String administrativeArea = placeMark.administrativeArea;
  // String subAdministrativeArea = placeMark.administrativeArea;
  String postalCode = placeMark.postalCode;
  String country = placeMark.country;
  // String subThoroughfare = placeMark.subThoroughfare;
  String thoroughfare = placeMark.thoroughfare;
  _isoCountryCode = placeMark.isoCountryCode;
  return "$name, $thoroughfare, $locality, $administrativeArea, $postalCode, $country";
}

这篇关于Flutter:如何在Flutter Google Maps中使用纬度来获取地点名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆