从纬度到纵向的地址

时间:2018-10-24 01:45:45

标签: javascript php html mysql gps

有人可以帮助我从数据库中上传经纬度的地址文本吗?另外,我需要将地址及其相应的坐标重新插入到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']. '  ';

}

?>

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

2 个答案:

答案 0 :(得分:0)

您可以使用google maps api,以前它们允许匿名访问其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);
}

?>

答案 1 :(得分:0)

您要执行的操作称为反向地理编码,即将地图上的位置转换为人类可读的地址。

如果您使用JavaScript进行此操作,则需要纬度和经度(您已经提到过)和API key。有了这些,您基本上可以使用下面的URL以及所需的经纬度和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[] ...

有关更多详细信息,请查看此link