可能重复:
Using PHP and google Maps Api to work out distance between 2 post codes (UK)
Google Maps API: get two locations distance and plot on map
我创建了一个界面,用户可以在其中插入开始和结束地址,提交后我想获取两个地址的纬度和经度,然后计算它们之间的距离。到目前为止,我已经粘贴了以下代码:
<html>
<head>
</head>
<body>
<form method="post" action ="gMap.php?showDistance=1" >
<table>
<tr><td>Start:</td><td><input id="start" type="text" name='start'/> </td></tr></br>
<tr><td>Finish:</td><td><input id="finish" type="text" name='finish'/></td></tr></br>
<tr><td></td><td><input type="submit" value="Calculate Distance" /></td></tr></br>
</table>
</form>
<!--
<br/><br/>
<div id="map" style="width: 500px; height: 350px"></div>
<div id="results" style="width: 500px;"></div>
-->
</body>
</body>
</html>
<?php
if(isset($_GET['showDistance'])) {
if(!empty($_POST['start'])&& !empty($_POST['finish'])){
$start='';
$finish='';
$start=$_POST['start'];
$finish=$_POST['finish'];
// I want to send start address via this url;
$startUrl = 'http://maps.google.com/maps/api.............';
$startData = @file_get_contents($startUrl);
// I want to send finish address via this url;
$finishUrl = 'http://maps.google.com/maps/api............';
$finishData = @file_get_contents($finishUrl);
$startResult = explode(",", $startData);
$finishResult = explode(",", $finishData);
echo "Information about start address:"."<br/>";
echo $startResult[0]."<br/>"; // status code
echo $startResult[1]."<br/>"; // accuracy
echo $startResult[2]."<br/>"; // latitude
echo $startResult[3]."<br/>";; // longitude
echo "Information about finish address:"."<br/>";
echo $finishResult[0]."<br/>"; // status code
echo $finishResult[1]."<br/>"; // accuracy
echo $finishResult[2]."<br/>"; // latitude
echo $finishResult[3]."<br/>";; // longitude
} else {
echo "You forget to insert address";
}
}
?>
现在我面临的问题是我不知道如何使用谷歌地图api url从谷歌地图获取所需信息。 $ startUrl和$ finishUrl是我关注的两个变量。顺便说一句,我没有编写距离计算代码,因为我还没有得到尊重的纬度和经度值。