地址从经纬度转化为地址
address from latitude and longitud into address
谁能帮我从纬度和经度中获取地址文本,都是从数据库上传的?我还需要将地址及其各自的坐标重新插入到数据库中。
<?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']. ' ';
}
?>
有没有办法获取地址并在此期间插入它,以便它获取地址并立即将其发送回去?
任何帮助,谢谢!
您可以使用 google 映射 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);
}
?>
您想要做的是所谓的反向地理编码,将地图上的位置转换为人类可读的地址。
如果您在 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}`;
上面的查询会return一个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。
谁能帮我从纬度和经度中获取地址文本,都是从数据库上传的?我还需要将地址及其各自的坐标重新插入到数据库中。
<?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']. ' ';
}
?>
有没有办法获取地址并在此期间插入它,以便它获取地址并立即将其发送回去? 任何帮助,谢谢!
您可以使用 google 映射 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);
}
?>
您想要做的是所谓的反向地理编码,将地图上的位置转换为人类可读的地址。
如果您在 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}`;
上面的查询会return一个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。