【问题标题】:PHP Geolocation (Street Address -> Lat-Long) [closed]PHP地理位置(街道地址->经纬度)[关闭]
【发布时间】:2013-08-16 21:27:43
【问题描述】:

我需要某种类型的 PHP 东西,以便我可以输入一些街道地址
(如: 123假街, 法克敦,FA, 98765) 然后它会给我这些地址的坐标(纬度和经度)。
我需要它是完全动态的(因为我将从数据库中获取这些地址)。

我在哪里可以找到可以做到这一点的东西?

【问题讨论】:

    标签: php geolocation latitude-longitude street-address


    【解决方案1】:

    获取地址并将其转换为 lat/lng 的过程称为“地理编码”。我会看看 Google Geocode API。

    https://developers.google.com/maps/documentation/geocoding/

    【讨论】:

    • 注意 google 的 TOS,它要求您使用坐标为“用户”显示地图。
    【解决方案2】:

    我为地址验证 API 提供商 SmartyStreets 工作。我们的LiveAddress API 可以满足您的需求。可以免费设置一个帐户,每月可进行 250 次查找。这里有一些sample PHP code

    <?php
    
    // Customize this (get ID/token values in your SmartyStreets account)
    $authId = urlencode("raw ID here");
    $authToken = urlencode("raw token here");
    
    // Address input
    $input1 = urlencode("3785 s las vegs av.");
    $input2 = urlencode("los vegas,");
    $input3 = urlencode("nevada");
    
    // Build the URL
    $req = "https://api.smartystreets.com/street-address/?street={$input1}&city={$input2}&state={$input3}&auth-id={$authId}&auth-token={$authToken}";
    
    // GET request and turn into associative array
    $result = json_decode(file_get_contents($req),true);
    
    echo "<pre>";
    print_r($result);
    echo "</pre>";
    
    ?>
    

    结果如下:

    {
        "input_index": 0,
        "candidate_index": 0,
        "delivery_line_1": "3785 Las Vegas Blvd S",
        "last_line": "Las Vegas NV 89109-4333",
        "delivery_point_barcode": "891094333992",
        "components": {
            "primary_number": "3785",
            "street_name": "Las Vegas",
            "street_postdirection": "S",
            "street_suffix": "Blvd",
            "city_name": "Las Vegas",
            "state_abbreviation": "NV",
            "zipcode": "89109",
            "plus4_code": "4333",
            "delivery_point": "99",
            "delivery_point_check_digit": "2"
        },
        "metadata": {
            "record_type": "H",
            "zip_type": "Standard",
            "county_fips": "32003",
            "county_name": "Clark",
            "carrier_route": "C024",
            "congressional_district": "01",
            "building_default_indicator": "Y",
            "rdi": "Commercial",
            "elot_sequence": "0119",
            "elot_sort": "A",
            "latitude": 36.10357,
            "longitude": -115.17295,
            "precision": "Zip9"
        },
        "analysis": {
            "dpv_match_code": "D",
            "dpv_footnotes": "AAN1",
            "dpv_cmra": "N",
            "dpv_vacant": "N",
            "active": "Y",
            "footnotes": "B#H#L#M#"
        }
    }
    

    【讨论】:

    • 当您说“每月 250 次查找”时,这是否意味着如果有 250 人访问我的网站并且我的网站有 100 次查找,那么它只适用于 2.5 人?或者我的 100 次查询可以被查看 250 次吗?我正在做的是设置已经标记了 100 个位置的地图,访问者只需查看这些位置。没有定期添加新位置,但我拥有的 100 个位置可能会随着时间的推移而增加。
    • 好的,在您的情况下,您可能会使用 100 次查找并缓存结果,只要您觉得舒服(地址数据可以更改)。当您想添加其他地理编码/地址时,您将为每个地址执行一个请求并缓存结果以显示在您的站点上。我们的大多数用户都允许他们的用户实时执行查找,但在您的情况下听起来这不是必需的。只需在后台进行查找并将缓存结果部署到您的站点即可。
    猜你喜欢
    • 2015-12-06
    • 1970-01-01
    • 2010-09-14
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多