【问题标题】:Get latitude and longitude automatically using php, API使用php、API自动获取经纬度
【发布时间】:2012-01-27 20:20:35
【问题描述】:

在我的一个 php 应用程序中,我必须从地址中找出该地点的纬度和经度。

我试过这个代码:

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};

但它显示以下错误:

警告:file_get_contents(http://maps.google.com/maps/api/geocode/json?address=technopark, Trivandrun, kerala,India&sensor=false&region=IND) [function.file-get-contents]: 失败打开流:HTTP 请求失败!第 4 行 D:\Projects\lon.php 中的 HTTP/1.0 400 错误请求

请帮我解决这个问题。

【问题讨论】:

标签: php google-maps-api-3


【解决方案1】:

两个想法:

  • 地址和地区是URL Encoded吗?
  • 也许您运行代码的计算机不允许 http 访问。尝试加载另一个页面(如“http://www.google.com”),看看是否有效。如果这也不起作用,那么 PHP 设置有问题。

【讨论】:

    【解决方案2】:
    //将urlencode添加到您的地址 $address = urlencode("technopark, Trivandrun, kerala, India"); $region = "IND"; $json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region"); 回声 $json; $decoded = json_decode($json); print_r($decoded);

    【讨论】:

    • 网址应该是http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region
    • @ThomasOrlita 谢谢。问题是 &reg 改成不同的字符,所以用 &region 代替
    【解决方案3】:
    $address = str_replace(" ", "+", $address);
    

    在file_get_content 之前使用上面的代码。 意思是,使用下面的代码

    $address = str_replace(" ", "+", $address);
    
    $json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
    $json = json_decode($json);
    
    $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
    $long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
    

    它肯定会起作用。 由于地址不支持空格,它只支持 + 符号代替空格。

    【讨论】:

    • 查看内置 php 函数:urlencode() 和 rawurlencode();
    • @CodeLღver 我想从地址获取邮政编码,纬度,长。
    • @Manwal 你的意思是说邮政编码......还是代码?我可以吗
    • @Manwal 您可以检查 api 并通过 print $json 获取它返回的数据。希望你能找到所有数据。
    【解决方案4】:

    使用curl 代替file_get_contents

    $address = "India+Panchkula";
    $url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=India";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    curl_close($ch);
    $response_a = json_decode($response);
    echo $lat = $response_a->results[0]->geometry->location->lat;
    echo "<br />";
    echo $long = $response_a->results[0]->geometry->location->lng;
    

    【讨论】:

    • 有谁知道指定 CURLOPT_PROXYPORT 3128 的目的吗??
    • 哎呀 - 评论编辑超过了 5 分钟的限制......我对使用代理一点也不熟悉,但从我对这个主题的阅读来看,我希望未指定代理 server,则代理 port 将被忽略。然而,我看到的大多数关于使用 curl 访问谷歌地图的帖子都指定了这个没有服务器的端口。这只是复制粘贴污染,还是有目的?
    • 您好,我想根据ip地址获取当前位置。有没有办法得到这个?
    • 大多数时候它给出空响应。可能是什么问题。
    【解决方案5】:

    ...不要忘记“$region”以使代码正常工作:

    $address = "Salzburg";
    $address = str_replace(" ", "+", $address);
    $region = "Austria";
    
    $json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
    $json = json_decode($json);
    
    $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
    $long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
    echo $lat."</br>".$long;
    

    【讨论】:

      【解决方案6】:
      $address = str_replace(" ", "+", $address);
      
      $json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false&region=$region");
      $json = json_decode($json);
      
      $lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
      $long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
      

      【讨论】:

        【解决方案7】:

        我认为您的 apache 服务器上的 allow_url_fopen 已禁用。你需要打开它。

        请将 allow_url_fopen = 0 更改为 allow_url_fopen = 1

        更改后不要忘记重新启动您的 Apache 服务器。

        【讨论】:

          猜你喜欢
          • 2014-10-19
          • 2016-12-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-22
          相关资源
          最近更新 更多