使用v3的Google地理编码不会显示错误 [英] Google geocoding using v3 not showing error

查看:103
本文介绍了使用v3的Google地理编码不会显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在下面的代码上提供帮助。发生的事情是,当我点击一个按钮时,我得到了一个地方名称的经纬度。但是,最近它不起作用。它会打印出地址x未通过地理编码,收到状态请注意,x是给定的地址,并且不会给出状态码。

Kindly help on the code below. What happens is that I get the latitude and longitude of the name of a place when I click on a button. However, of late, it is not working. It prints that "Address x failed to Geocode. Received status " Note that x is a given address and no status code is given.

    $id=$_REQUEST['id'];
    define("MAPS_HOST", "maps.googleapis.com");
    define("KEY", "xxxxxxx");

    $query = "SELECT * FROM markers WHERE mid = $id LIMIT 1";
    $result = mysql_query($query);
    if (!$result) {
      die("Invalid query: " . mysql_error());
    }

    // Initialize delay in geocode speed
    $delay = 0;
    $base_url = "http://" . MAPS_HOST . "/maps/api/geocode/xml?address=";

    if($row = @mysql_fetch_assoc($result)){
     $geocode_pending = true;
     $address = $row["address"];
     while ($geocode_pending) {
     $request_url = $base_url . urlencode($address) . "&sensor=false&key=" . KEY;
        $xml = simplexml_load_file($request_url) or die("url not loading");

        $status = $xml->Response->Status->code;
        if (strcmp($status, "200") == 0) {
          // Successful geocode
          $geocode_pending = false;
          $coordinates = $xml->Response->Placemark->Point->coordinates;
          //$coordinatesSplit = split(",", $coordinates);
          // Format: Longitude, Latitude, Altitude
          //$lat = $coordinatesSplit[1];
          //$lng = $coordinatesSplit[0];
          list($lat,$lng) = explode(",",$coordinates);
          $query = sprintf("UPDATE markers " .
                 " SET lat = '%s', lng = '%s' " .
                 " WHERE mid = '%s' LIMIT 1;",
                 mysql_real_escape_string($lng),
                 mysql_real_escape_string($lat),
                 mysql_real_escape_string($id));
          $update_result = mysql_query($query);
          if (!$update_result) {
            die("Invalid query: " . mysql_error());
          }else{
                echo "$lat;$lng";
          }
        } else if (strcmp($status, "620") == 0) {
          // sent geocodes too fast
          $delay += 100000;
        } else {
          // failure to geocode
          $geocode_pending = false;
          echo "Address " . $address . " failed to geocode. ";
          echo "Received status " . $status . "
    \n";
        }
        usleep($delay);
     }
    }


推荐答案

I 'm不知道你的代码是基于哪个API的,但是当前API的响应不适用于这些行:

I'm not sure on which API your code is based on, but the response of the current API will not work with these lines:

$status = $xml->Response->Status->code;
if (strcmp($status, "200") == 0) {

不是元素响应,状态代码,此外响应将不包含数字状态码。

There are no elements Response,Status or code, furthermore the response will not contain a numeric status-code.

使用它代替:

use this instead:

$status = $xml->status;
if (strcmp($status, "OK") == 0) {

修复其余的代码,请看看返回的XML的结构,也没有元素 Placemark Point 坐标

To fix the rest of the code please take a look at the structure of the returned XML, there are also no elements Placemark,Point and coordinates.

应该是:

it should be:

$lat = $xml->result->geometry->location->lat;
$lng = $xml->result->geometry->location->lng;

这篇关于使用v3的Google地理编码不会显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆