Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用zipcode在两个位置之间限定距离(英里)_Php_Geolocation_Zipcode - Fatal编程技术网

Php 使用zipcode在两个位置之间限定距离(英里)

Php 使用zipcode在两个位置之间限定距离(英里),php,geolocation,zipcode,Php,Geolocation,Zipcode,我们的系统中有100个用户,在注册时他们已经输入了他们的zipcode,现在我需要的是,如果我输入任何zipcode,它应该给我输入的zipcode和其他100个用户zipcode之间的距离的结果 如果有人知道解决方案,可以帮我吗?我会分两部分来做: 一个地理编码脚本,运行一次,结果存储在持久缓存(如数据库)中。这样可以避免达到速率限制并加快最终查找 用于计算距离的脚本,需要时运行或缓存该脚本,以构建一个查找表,存储每个邮政编码和所有其他邮政编码之间的距离。因为您只有100个拉链,所以这个查找

我们的系统中有100个用户,在注册时他们已经输入了他们的zipcode,现在我需要的是,如果我输入任何zipcode,它应该给我输入的zipcode和其他100个用户zipcode之间的距离的结果


如果有人知道解决方案,可以帮我吗?

我会分两部分来做:

  • 一个地理编码脚本,运行一次,结果存储在持久缓存(如数据库)中。这样可以避免达到速率限制并加快最终查找
  • 用于计算距离的脚本,需要时运行或缓存该脚本,以构建一个查找表,存储每个邮政编码和所有其他邮政编码之间的距离。因为您只有100个拉链,所以这个查找表不会很大
地理编码
有一个邮政编码API可以做到这一点-。

使用zipcode查找将每个邮政编码转换为lat/long坐标对;然后用Haversine或Vincenty计算距离——有成千上万的博客和教程在回答这个问题
<?php
// Script to geocode each ZIP code. This should only be run once, and the
// results stored (perhaps in a DB) for subsequent interogation.
// Note that google imposes a rate limit on its services.

// Your list of zipcodes
$zips = array(
    '47250', '43033', '44618'
    // ... etc ...
);

// Geocode each zipcode
// $geocoded will hold our results, indexed by ZIP code
$geocoded = array();
$serviceUrl = "http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:%s&sensor=false";
$curl = curl_init();
foreach ($zips as $zip) {
    curl_setopt($curl, CURLOPT_URL, sprintf($serviceUrl, urlencode($zip)));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $data = json_decode(curl_exec($curl));
    $info = curl_getinfo($curl);
    if ($info['http_code'] != 200) {
        // Request failed
    } else if ($data->status !== 'OK') {
        // Something happened, or there are no results
    } else {
        $geocoded[$zip] =$data->results[0]->geometry->location;
    }
}