我想发送纬度和经度数据,以便可以在Google Map上进行搜索。这些数据必须从数据库中选择,但是我无法一键搞清楚该怎么做。
这是我的代码。
<?php
$con = mysqli_connect("localhost","root","","project");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM table1");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Latitude</th>
<th>Longitude</th>
<th>Date</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['latitude'] . "</td>";
echo "<td>" . $row['longitude'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><a href='./clicktest.php?id=".$row['id']."'>View Location</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
如您所见,它会将我想要的行的id值发送到另一个页面
<?php
$con=mysqli_connect("localhost","root","","project");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_GET['id'])) {
$result = "SELECT * FROM table1 WHERE id='".$_GET['id']."'" ;
$show = mysqli_query($con, $result);
while($row = mysqli_fetch_array($show))
{
$latitude = $row['latitude'];
$longitude = $row['longitude'];
echo "<a href='http://www.google.com/maps/place/".$latitude.",".$longitude."'>click here</a>";
}
mysqli_close($con);
}
?>
最后可以在此页面上运行指向Google Map的链接。但是我想要的是单击第一页上的“查看位置”,然后跳转到包含该行数据的google map页面,而不必经历另一页和另一次单击。
我做了一些研究,也许这与AJAX有关?还有没有不用AJAX的方法吗?(因为我以前从未使用过它)
谢谢
答案 0 :(得分:2)
我认为您需要直接链接到Google地图页面,并在其上使用“ target = _blank”属性。例如:
echo '<a href="http://www.google.com/maps/place/' . $row['latitude'] . ',' . $row['longitude'] . '" target="_blank">View Location</a>';
答案 1 :(得分:0)
您可以根据需要向锚标记添加尽可能多的参数,例如,像这样添加lat
和long
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['latitude'] . "</td>";
echo "<td>" . $row['longitude'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><a href='./clicktest.php?id=".$row['id']
.'&lat=' . $row['latitude']
. '&long' . $row['longitude']"'>View Location</a></td>";
echo "</tr>";
}