简体   繁体   English

使用谷歌地图 api,如何从纬度和经度获取国家、state 和城市?

[英]using google maps api, how to get country, state, and city from lat and long?

I my react app, i am using the libraries @react-google-maps/api and react-places-autocomplete .我是我的 React 应用程序,我正在使用库@react-google-maps/apireact-places-autocomplete

How do I get the country, state, city given lat and long coordinates?我如何获得国家,state,给定纬度和经度坐标的城市?

How to get the formatted address, place, give lat and long?如何获取格式化的地址、地点、经纬度?

  1. Use react-native-geocoding package for Geocoding and Reverse Geocoding.使用 react-native-geocoding package 进行地理编码和反向地理编码。
  2. install react-native-geocoding安装 react-native-geocoding

npm install --save react-native-geocoding npm 安装 --save react-native-geocoding

  1. For Geocoding and Reverse Geocoding in your app you import Geocoder API from react-native-geocoding package like this对于您应用程序中的地理编码和反向地理编码,您可以像这样从react-native-geocoding package 导入Geocoder API

import Geocoder from 'react-native-geocoding';从“react-native-geocoding”导入地理编码器;

  1. Geocoder.init("API key"); Geocoder.init("API 密钥"); // initialize module // 初始化模块
  2. And use this code respectively for Geocoding and reverse Geocoding并分别使用此代码进行地理编码和反向地理编码

Geocoder.from(41.89, 12.49).then(json => { var addressComponent = >json.results[0].address_components[0]; Geocoder.from(41.89, 12.49).then(json => { var addressComponent =>json.results[0].address_components[0];

console.log(addressComponent);控制台日志(地址组件); }).catch(error => console.warn(error)); }).catch(error => console.warn(error)); // location object Geocoder.from({ latitude: 41.89, longitude: 12.49 }); // 位置 object Geocoder.from({ latitude: 41.89, longitude: 12.49 });

 // latlng object Geocoder.from({ lat: 41.89, lng: 12.49 }); // array Geocoder.from([41.89, 12.49]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM