【问题标题】:Using ajax and json to ge the driving distance between two addresses使用ajax和json获取两个地址之间的行车距离
【发布时间】:2014-10-14 22:12:20
【问题描述】:

是否可以使用 jQuery ajax 函数和 Google Directions API 来异步计算两个给定地址之间的距离(以英里为单位)?我找到了一些示例,展示了如何使用 PHP 执行此操作,但没有什么不需要重新加载页面

【问题讨论】:

  • 为什么不在 PHP 中使用 AJAX 从您的 PHP 中获取结果?

标签: jquery api google-maps distance directions


【解决方案1】:

是的,只需在 php 中实现您的计算,并使用 ajax 异步调用您的脚本:

$.ajax({
    url: "calcdist.php?address1="+addr1+"&address2="+addr2,
    success: function(result){
        alert("Distance is "+result+" miles);
    }
});

在你的脚本中(calcdist.php):

<?php
$a1 = $_GET['address1'];
$a2 = $_GET['address2'];

$ll1 = getLatLong($a1);
$ll2 = getLatLong($a2);

$dist = haversine($ll1, $ll2);

echo $dist;
?>

你只需要在你的 php 中实现 hasrsine 和 getLatLong 函数

请参阅Haversine formula with php + http://code.google.com/p/gogeocode/ 以获得有关这些方面的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    相关资源
    最近更新 更多