在php中将地址转换为纬度和经度
Posted
技术标签:
【中文标题】在php中将地址转换为纬度和经度【英文标题】:convert address to latitude and longitude in php 【发布时间】:2014-12-08 09:44:27 【问题描述】:我有以下代码将地址转换为纬度和经度,但它仅在输入以单个输入给出时才有效,但是我有一个表单允许用户在不同行中输入地址、城市、州和国家,谁能告诉我如何将给定的地址转换并保存为纬度和经度
<?php
ob_start();
$con=mysqli_connect("localhost","root","","db");
// Check connection
if (mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
if($_POST)
$data_arr = geocode($_POST['address']); // get latitude, longitude and formatted address
if($data_arr)// if able to geocode the address
$latitude = $data_arr[0];
$longitude = $data_arr[1];
$formatted_address = $data_arr[2];
$address = mysqli_real_escape_string($con, $_POST['address']);
$sql="INSERT INTO register_office (co_address,latitude,longitude) VALUES ('$address', '$latitude', '$longitude')";
if (!mysqli_query($con,$sql))
die('Error: ' . mysqli_error($con));
mysqli_close($con);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
</head>
<body >
<form action="" method="post">
<input type='text' name='addressline1' placeholder='address line 1' />
<input type='text' name='addressline2' placeholder='address line 2' />
<input type='text' name='addressline3' placeholder='address line 3' />
<input type='text' name='city' placeholder='city' />
<input type='text' name='state' placeholder='state' />
<input type='text' name='country' placeholder='country' />
<input type='submit' value='Geocode!' />
</form>
<?php
// function to geocode address, it will return false if unable to geocode address
function geocode($address)
// url encode the address
$address = urlencode($address);
// google map geocode api url
$url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=$address";
// get the json response
$resp_json = file_get_contents($url);
// decode the json
$resp = json_decode($resp_json, true);
// response status will be 'OK', if able to geocode given address
if($resp['status']='OK')
// get the important data
$lati = $resp['results'][0]['geometry']['location']['lat'];
$longi = $resp['results'][0]['geometry']['location']['lng'];
$formatted_address = $resp['results'][0]['formatted_address'];
// verify if data is complete
if($lati && $longi && $formatted_address)
// put the data in the array
$data_arr = array();
array_push(
$data_arr,
$lati,
$longi,
$formatted_address
);
return $data_arr;
else
return false;
else
return false;
?>
</body>
</html>
【问题讨论】:
【参考方案1】:你最好也检查一下这些值是否为空...然后,使用逗号,
将它们连接起来,如下所示:
$addressFields = array(
$_POST['addressline1'],
$_POST['addressline2'],
$_POST['addressline1'],
$_POST['city'],
$_POST['state'],
$_POST['country'],
);
$addressData = implode(', ', array_filter($addressFields));
在这个 sn-p 中,array_filter()
用于过滤数组中的所有空字符串。 implode()
使用逗号 ,
作为“胶水”将数组项连接到一个字符串中。
之后,使用您的示例中的geocode()
方法:
$data_arr = geocode($addressData);
【讨论】:
【参考方案2】:将地址部分添加在一起形成一行,然后将其提交给 Google Maps API。
$address_parts = array($addressline1, $addressline2, $addressline3, $city, $state, $country);
$address = implode(", ", $address_parts); // join the parts together divided by comma
【讨论】:
【参考方案3】:听起来您应该能够将您的地址部分连接在一起然后发送?
例如
$address = $_POST['addressline1'] .','. $_POST['addressline2'] <<snip>> $_POST['country'];
$data_arr = geocode($address);
如果 google 在重复逗号时遇到问题,您可能需要构建一些 if
语句以仅放入实际设置的位(此时构建一个数组并内爆它可能会更好)
【讨论】:
以上是关于在php中将地址转换为纬度和经度的主要内容,如果未能解决你的问题,请参考以下文章