PHP谷歌地图地理编码XML加载问题
这真的很奇怪,让我很困惑。我有一个PHP脚本,它从地址输入表单收集数据,为googlemaps地理编码服务格式化数据,然后加载该服务返回的xml并从中提取lat/lng。奇怪的是,某些地址返回“url not loading”(url not loading)die消息,但当我复制脚本发送的确切字符串并将其粘贴到浏览器中时,实际上我得到了xml文件。有人知道这里发生了什么吗 这是回显出来的字符串。也许有人能告诉我PHP不喜欢这个URL的什么 湖、+IL、+60020和传感器=真或假 以下是脚本:PHP谷歌地图地理编码XML加载问题,php,xml,google-maps,geocoding,Php,Xml,Google Maps,Geocoding,这真的很奇怪,让我很困惑。我有一个PHP脚本,它从地址输入表单收集数据,为googlemaps地理编码服务格式化数据,然后加载该服务返回的xml并从中提取lat/lng。奇怪的是,某些地址返回“url not loading”(url not loading)die消息,但当我复制脚本发送的确切字符串并将其粘贴到浏览器中时,实际上我得到了xml文件。有人知道这里发生了什么吗 这是回显出来的字符串。也许有人能告诉我PHP不喜欢这个URL的什么 湖、+IL、+60020和传感器=真或假 以下是脚本:
//This section sends HTTP geocode to google and recieves the XML
$googlekey = "ABQIAAAAZe-W-lkccZSM2qC_Ci4JnBS97eShVNVCdPw2dK9jd_Ls-9Sg7hSJTMXMmezqrmN9bDDbO_CSRwChtw";
$mapAddress = urlencode($barAddress).",+".$barCity.",+".$barState.",+".$barZip;
echo $mapAddress."<br />";
$googleURL = "http://maps.google.com/maps/geo?output=xml&key=".$googlekey."&q=".$mapAddress."&sensor=true_or_false";
echo $googleURL."<br />";
$googlexml = simplexml_load_file($googleURL) or die("url not loading<br />");
$status = $googlexml - > Response - > Status - > code;
if (strcmp($status, "200") == 0) {
echo "barID:".$barID;
// Successful geocode
$coordinates = $googlexml - > Response - > Placemark - > Point - > coordinates;
$coordinatesSplit = split(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $coordinatesSplit[1];
$lng = $coordinatesSplit[0];
echo "lat:".$lat." lon:".$lng;
$xmlUpdate = "/".$barID."/".$barID.".xml";
//This is the final query that updates
$updateQuery = sprintf("UPDATE userBars "." SET lat = '%s', lng = '%s', barXML = '%s' "." WHERE barID = '%s';", mysql_real_escape_string($lat), mysql_real_escape_string($lng), mysql_real_escape_string($xmlUpdate), mysql_real_escape_string($barID));
$updateResult = mysql_query($updateQuery);
if ($updateResult) {
echo "Successfully Geocoded!<br />";
} else {
echo "Geocode Not Successful!";
}
} else {
echo "Geocode Failed.";
}
}
} else {
echo "Error retrieving barID. Files and entries were not created successfully.<br />";
}
//此部分向google发送HTTP地理代码并接收XML
$googlekey=“abqiaaaze-W-lkccZSM2qC_ci4jnbs97eshvvcdpw2dk9jd_Ls-9sg7hsjtmxmmezqrmmn9bddbo_CSRwChtw”;
$mapAddress=urlencode($barAddress)。“,+”$barCity.,+“$barState.,+”$barZip;
echo$mapAddress。“
”;
$googleURL=”http://maps.google.com/maps/geo?output=xml&key=“$googlekey.”&q=“..$mapAddress.”&sensor=true\u或false”;
echo$googleURL.“
”;
$googlexml=simplexml\u加载文件($googleURL)或死亡(“url未加载
”);
$status=$googlexml->Response->status->code;
如果(strcmp($status,“200”)==0){
echo“barID:”.$barID;
//成功的地理编码
$coordinates=$googlexml->Response->Placemark->Point->coordinates;
$CoordinationsSplit=拆分(“,”,$Coordinations);
//格式:经度、纬度、高度
$lat=$CoordinationsSplit[1];
$lng=$coordinatesSplit[0];
回声“lat:.$lat.”lon:.$lng;
$xmlUpdate=“/”$barID./“$barID..xml”;
//这是更新的最终查询
$updateQuery=sprintf(“更新用户栏”。“设置lat='%s',lng='%s',barXML='%s'”。“其中barID='%s'”,mysql\u real\u escape\u string($lat),mysql\u real\u escape\u string($lng),mysql\u real\u escape\u string($xmlUpdate),mysql\u real\u escape\u string($barID));
$updateResult=mysql\u查询($updateQuery);
如果($updateResult){
echo“已成功地理编码!
”;
}否则{
echo“地理编码不成功!”;
}
}否则{
echo“地理编码失败。”;
}
}
}否则{
echo“检索barID时出错。未成功创建文件和条目。
”;
}
Doh!在这个论坛上发布这个链接只是让我意识到,这是因为我不是这个城市,任何一个名字不同的城市都会把它扔掉。解决了的!只是一个旁注:使用shell脚本刮取坐标在没有google键的情况下工作。