【问题标题】:Dynamic relation between GOOGLE Maps API v3 and MySQLGOOGLE Maps API v3 和 MySQL 之间的动态关系
【发布时间】:2013-03-27 06:40:20
【问题描述】:

我正在设计我自己的第一个网页。这是一个房地产页面,一些用户将添加开放房屋信息,而其他用户可以提取信息。 数据库和网络的代码非常好(两个月后......)并且插入和获取数据很甜蜜...... 现在是谷歌地图。我正在尝试拥有类似 trulia 甚至是非常原始的 housingmaps.com 的东西(似乎他们开始了混搭,对吧?)。 crimereports.com 也非常好。

目标:从数据库中提取地址,从地理编码中获取经纬度,将其插入表中,在谷歌地图中显示。另外:当人们平移地图时,新信息会弹出到地图中。

这是检索地址、地理代码以及将 lat 和 lng 添加回数据库的代码。

<?php
//require("phpsqlajax_dbinfo.php");

// Opens a connection to a MySQL server
$username = "abc"; //personal info changed to abc
$password = "abc";
$hostname = "abc"; 
$database = "abc";
$connection = mysqli_connect($hostname, $username, $password);
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//if (!$connection) {
  //die("Not connected : " . mysql_error());
//}

define("MAPS_HOST", "maps.google.com");
define("KEY", "my .............key........code");



// Set the active MySQL database
$db_selected = mysqli_select_db($connection, $database);
if (!$db_selected) {
  die("Can\'t use db : " . mysql_error());
}

// Select all the rows in the markers table
$query = "SELECT * FROM brokerstour.property WHERE 1";
$result = mysqli_query($connection, $query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}

// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://maps.googleapis.com/maps/api/geocode/output?parameters";

// Iterate through the rows, geocoding each address
while ($row = @mysqli_fetch_assoc($result)) {
  $geocode_pending = true;

  while ($geocode_pending) {
    $address = $row["address"];
    $id = $row["id"];
    $request_url = $base_url . "&q=" . urlencode($address);
    $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];

      $query = sprintf("UPDATE property " .
             " SET lat = '%s', lng = '%s' " .
             " WHERE id = '%s' LIMIT 1;",
             mysql_real_escape_string($lat),
             mysql_real_escape_string($lng),
             mysql_real_escape_string($id));
      $update_result = mysql_query($query);
      if (!$update_result) {
        die("Invalid query: " . mysql_error());
      }
    } else if (strcmp($status, "620") == 0) {
      // sent geocodes too fast
      $delay += 100000;
    } else {
      // failure to geocode
      $geocode_pending = false;
      echo "Address " . $address . " failed to geocoded. ";
      echo "Received status " . $status . "
\n";
    }
    usleep($delay);
  }
}
?>

这是我对 Google 教程所做的更改(原始代码): 1) 更新 php: mysqli 以避免错误。没有得到更多的php错误。 2) 将 url 从 "http://" 更改。 MAPS_HOST 。 “/maps/geo?output=xml”。 “&键=”。钥匙; 到 “http://maps.googleapis.com/maps/api/geocode/output?parameters” 尝试将代码更新到 API v3。

如果我使用旧网址,我会收到错误 610。 有了新的网址,我得到:


( ! ) SCREAM: 忽略错误抑制 (!)警告:simplexml_load_file():I / O警告:无法在C中加载外部实体“maps.googleapis.com/maps/api/geocode/output?parameters&q=4200+Park+Blvd+”:....第 57 行 .....

网址未加载

完全披露:我很新,也许上面的内容甚至没有意义。但是,我已经在这个问题上停留了一周。 (顺便说一下,对于那些刚接触谷歌地图的人来说,《Beginning Google API 3》这本书是必须的。虽然它不处理数据库,但很好地解释了谷歌地图 API)。

有人可以提供任何提示,书籍,教程吗?任何帮助都是很大的帮助。 感谢您的时间。

第 57 行是 $xml = simplexml_load_file($request_url) 或 die("url 未加载");

【问题讨论】:

    标签: php mysql database google-maps-api-3


    【解决方案1】:

    $base_url = "http://maps.googleapis.com/maps/api/geocode/output?parameters"; 你没有指定输出。它应该是xml 或jason。此外,您传递的参数是地址和sensor = false 或sensor = true

    编辑

    删除

    $geocode_pending = true;
    while ($geocode_pending) {
    

    结束编辑

    $q = $row["address"];
    $base_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=";
    $request_url = $base_url.urlencode($q)."&sensor=false";
    $xml = simplexml_load_file($request_url) or die("url not loading");
    if($xml->status=="OK"){
        // Successful geocode
        $lat = $xml->result->geometry->location->lat;
        $lng = $xml->result->geometry->location->lng; 
    

    编辑

        echo $lat.","$lng."<br>";//
        } 
        }
    

    删除其余代码并确保在逐步添加其余代码之前可以正常工作

    【讨论】:

    • 感谢您的宝贵时间!错误消息现在似乎更接近解决方案。首先,我用你的 8 行代码替换了 (url $base_url = "maps.googleapis.com/maps/api/geocode/xml?address=";) 行。我是新手......这就是我现在得到的:注意:未定义的变量:C 中的行: ...在第 48 行(这是您的第一行)。我也收到此错误:(注意:尝试在 C 中获取非对象的属性:....第 67 行(第 67 行:$status = $xml ->Response->Status->code;)。最后,我收到这条消息:“地址...未能进行地理编码。收到状态”。它会读取每个地址!
    • $q = $row["address"]; 有什么用,试试echo $q;
    • 感谢您的帮助。错误消息:“未定义的变量:C:\wamp\www\test\geocode.php 中的行”。我尝试打印“q”,没有任何反应。该代码读取所有地址行并返回相同的消息:“尝试获取 C:\wamp\www\test\... 中非对象的属性”。我猜代码和mysql很好。问题是与谷歌的连接,似乎 url 是主要障碍。我用密钥尝试了 Google V2 代码,它给出了 610 错误。借助 V3 代码和您的帮助,我得到了上面的这些错误。比以前更好,甚至没有读取 mysql。再次感谢您的帮助!
    • 如果您在浏览器中键入它会提供数据的地址,我的答案中的 url 是正确的。您的其余代码一定是导致问题的原因。尝试将查询限制为 LIMIT 1,10 。注释掉while ($geocode_pending) { 及其结束} 注释掉$query = sprintf("UPDATE property " . 到最后。并回显$lat &amp; $lng。让这段代码工作并逐渐建立
    • 谢谢。我会尝试。我试图消除延迟和其他一些部分,以查看问题出在哪里。它读取表格并正确输入地址。但它不会拉 lat 和 lnt。我会试试你的建议。再次感谢。
    【解决方案2】:

    这是部分成功。 此代码完成: a) 从表中获取地址; b) 从谷歌获取 lat 和 lnt; c) 将数据插入表中。

    我仍然需要处理延迟问题。此外,我是一个 3 个月大的程序员......为了完成上述工作,我花了两个星期的时间......我把延迟留在那里仅供参考(这是来自谷歌的教程,其中包含网络上其他人的更改。 )

    我试图投票赞成 Strachan 先生的答案,但我在堆栈溢出中缺乏声誉......无法做到。该修复也有效。感谢您的时间。

    <?php
    
    $conn = mysql_connect('localhost', 'rxxt', 'xxxxxxx');
    if(!$conn)
     {
       echo "Database Error.";
     }
     else
     {
       echo "Connected.";
       mysql_select_db('YOUR TABLE', $conn);
       $query = "SELECT * from YOUR TABLE";
       $result = mysql_query($query, $conn) or die($query.mysql_error());
        while($row = mysql_fetch_array($result))
        { 
    
          $address = $row['address'];
          $id = $row['id'];
    
          $loc_address = str_replace(" ", "+", $address); //to replace spaces by '+' signs
              $location = $loc_address;
          echo $location."<br>";
          $delay = 0;  // this delay is here just to avoid errors - 
              $url = "http://maps.google.com/maps/api/geocode/json?address=".$location."&sensor=false";
              echo $url;
                $output=file_get_contents($url);
                $out= json_decode($output);
                $lat = $out->results[0]->geometry->location->lat;
                $lng = $out->results[0]->geometry->location->lng;
                echo '<br>Lat is '. $lat;
                echo '<br>Long is '. $lng;
    
    
           $query = sprintf("UPDATE YOUR TABLE " .
                 " SET lat = '%s', lng = '%s' " .
                 " WHERE id = '%s' LIMIT 1;",
                 mysql_real_escape_string($lat),
                 mysql_real_escape_string($lng),
                 mysql_real_escape_string($id));
               $update_result = mysql_query($query);
    
          echo "Successfully inserted.";
        // Below is just for reference - they say we need the delay. The may problem: does not get the status to compare to 200, 620, etc.   
        /*$status = $xml->Response->Status->code;  
        if (strcmp($status, "200") == 0) {
                // Successful geocode
                echo '<br />successfull geocode</strong><br />';
                $geocode_pending = false;
        }
        else if (strcmp($status, "620") == 0) {
             // sent geocodes too fast
             $delay += 100000;
                } else {
             // failure to geocode
             $geocode_pending = false;
             echo "Address " . $address . " failed to geocoded. ";
             echo "Received status " . $status . "
            \n";
              }*/
        usleep($delay); 
        }
     }
    mysql_close($conn);
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-09
      • 2012-09-13
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多