使用PHP和MySQL处理Spatial数据的问题

时间:2014-06-04 12:54:25

标签: php mysql geometry spatial

我正试图获得Latitude&从其地址开始的经度。 当我只使用一个点时,一切都工作得很好但是当我在循环中使用它并试图将它用于多个点时,事情会变得很难看。

我的代码如下所示:

<?php
$con = mysql_connect("localhost", "root", "");
mysql_select_db("ppialakm_db", $con);
function get_latlong($address){
        $prepAddr = str_replace(' ','+',$address);

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

        $output= json_decode($geocode);

        $lat = $output->results[0]->geometry->location->lat;
        $long = $output->results[0]->geometry->location->lng;

        return array("latitude"=> $lat, "longitude"=> $long);
    }
$queryyyy = mysql_query("SELECT broker_property_id, sector, locality, city FROM property_requirement");
while($array = mysql_fetch_assoc($queryyyy)){
$address = $array['sector'].",".$array['locality'].",".$array['city'].",Maharashtra";
$ahfaho = get_latlong($address);
$latlong = "POINT(".$ahfaho['latitude']." ".$ahfaho['longitude'].")";

$query = mysql_query("UPDATE property_requirement SET latlong=GeomFromText('".$latlong."') WHERE broker_property_id='".$array['broker_property_id']."'");
//echo $array['broker_property_id'].'<br/>';
}
echo "Success."
?>

问题是许多latlong字段仍然是空的,并且在我运行此脚本后也是随机顺序。 我无法解决问题。谁能帮我吗?我正在使用MySQL 5.5

1 个答案:

答案 0 :(得分:0)

验证您从地理编码中获得的内容可能会有所帮助:

function get_latlong($address){
        $prepAddr = str_replace(' ','+',$address);

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

        $output= json_decode($geocode);

        if ( 'OK' == $output->status ) {
            $lat = $output->results[0]->geometry->location->lat;
            $long = $output->results[0]->geometry->location->lng;

            return array("latitude"=> $lat, "longitude"=> $long);
        }
        else { return false; }
    }

if ( $ahfaho = get_latlong($address) ) {
    $latlong = "POINT(".$ahfaho['latitude']." ".$ahfaho['longitude'].")";

    $query = mysql_query("UPDATE property_requirement SET latlong=GeomFromText('".$latlong."') WHERE broker_property_id='".$array['broker_property_id']."'");
}
else { echo "Failed geocode."; }