从纬度和经度到地址的地址

tyu7yeag  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(313)

有谁能帮我从纬度和经度获取地址文本,两者都是从数据库上传的?此外,我需要插入到de数据库的地址回其各自的坐标。

<?php    
// Opens a connection to a MySQL server

$con = new mysqli($servername, $username, $password, $database);

$result = mysqli_query($con,'SELECT * FROM table_name ORDER BY id DESC');

while ($row = @mysqli_fetch_assoc($result)){

  $latitude=  $row['latitude']. '  ';
  $longitude=  $row['longitude']. '  ';

}

?>

有没有一种方法可以获取地址并在这段时间内插入它,这样它就可以获取地址并立即发送回去?任何帮助,谢谢!

qjp7pelc

qjp7pelc1#

你可以使用googlemapsapi,以前他们允许匿名访问他们的api,但现在你必须获得api密钥。https://developers.google.com/maps/documentation/geocoding/intro#limits

<?php    
// Opens a connection to a MySQL server

$con = new mysqli($servername, $username, $password, $database);

$result = mysqli_query($con,'SELECT * FROM table_name ORDER BY id DESC');

while ($row = @mysqli_fetch_assoc($result)){

  $latitude=  $row['latitude']. '  ';
  $longitude=  $row['longitude']. '  ';

  $geolocation = $latitude.','.$longitude;

  // be sure to replace API key
  $request = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.$geolocation.'&sensor=false'.'key=YOUR_API_KEY'; 

  // retrieve data from url, GET REQUEST
  $file_contents = file_get_contents($request);

  // decode json object
  $json_decode = json_decode($file_contents);

  // return the json object of the first record, navigate to the address from here
  var_dump($json_decode);
}

?>
57hvy0tb

57hvy0tb2#

你要做的是所谓的反向地理编码,将Map上的一个位置转换成一个人类可读的地址。
如果您是用javascript来实现这一点的,那么您将需要lat和long(您已经提到了)以及api键。通过这些,您基本上可以使用下面的url和所需的lat、long和api键来执行get请求。

const lat = 'LAT_COORDS_STRING';
const lng = 'LNG_COORDS_STRING';
const apiKey = 'MY_API_KEY_STRING'

const url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&key=${apiKey}`;

上面的查询将返回一个json(见下文),您可以解析和提取所需的信息(可能是字段) formatted_address 是你需要的)。

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "277",
               "short_name" : "277",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Bedford Avenue",
               "short_name" : "Bedford Ave",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Williamsburg",
               "short_name" : "Williamsburg",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Brooklyn",
               "short_name" : "Brooklyn",
               "types" : [ "sublocality", "political" ]
            },
            {
               "long_name" : "Kings",
               "short_name" : "Kings",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "New York",
               "short_name" : "NY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "11211",
               "short_name" : "11211",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "277 Bedford Avenue, Brooklyn, NY 11211, USA",
         "geometry" : {
            "location" : {
               "lat" : 40.714232,
               "lng" : -73.9612889
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 40.7155809802915,
                  "lng" : -73.9599399197085
               },
               "southwest" : {
                  "lat" : 40.7128830197085,
                  "lng" : -73.96263788029151
               }
            }
         },
         "place_id" : "ChIJd8BlQ2BZwokRAFUEcm_qrcA",
         "types" : [ "street_address" ]
      },

  ... Additional results[] ...

有关更多详细信息,请查看此链接。

相关问题