从Google maps api v2更改为v3 [英] Changing from Google maps api v2 to v3
问题描述
我一直在使用谷歌地理编码v3约6个月,但突然它停止工作(我得到一个610错误)。它只是在上周左右停止。
然后我遇到了这个(请参阅页面顶部的粉红色框!): https://developers.google.com/maps/documentation/geocoding/v2/我已经阅读了所有文档,不知道从哪里开始!
我希望这是一个小小的改变,因为需要很长时间才能达到这个目标,任何人都可以提供帮助吗?
$ b
更新:
require(database.php);
//打开一个到MySQL服务器的连接
$ con = mysql_connect(localhost,$ username,$ password);
if(!$ con)
{
die('Could not connect:'。mysql_error());
}
mysql_select_db(teamwork_poh,$ con);
$ company = get_the_title();
$ address = get_field('address_line_1');
$ city = get_field('town _ / _ city');
$ post_code = get_field('post_code');
$ link = get_permalink();
$ type = get_field('kind_of_organisation');
$ sql = sprintf(select count('x')as cnt from marker where`name` ='%s',mysql_real_escape_string($ company));
$ row_dup = mysql_fetch_assoc(mysql_query($ sql,$ con));
if($ row_dup ['cnt'] == 0){
mysql_query(INSERT INTO markers(`name`,`address`,`lat`,`lng`,`type`,`` VALUE('。$ company。','。。$ address。,。$ city。,。$ post_code。','0.0','0.0','。$ type 。','。$ link。'));
}
wp_reset_query();
require(database.php);
define(MAPS_HOST,maps.googleapis.com);
define(KEY,(my key));
//打开一个到MySQL服务器的连接
$ connection = mysql_connect(localhost,$ username,$ password);
if(!$ connection){
die(Not connected:。mysql_error());
}
//设置活动的MySQL数据库
$ db_selected = mysql_select_db($ database,$ connection);
if(!$ db_selected){
die(不能使用db:。mysql_error());
}
$ query =SELECT * FROM markers WHERE 1;
$ result = mysql_query($ query);
if(!$ result){
die(Invalid query:。mysql_error());
}
//初始化地理编码速度延迟
$ delay = 0;
$ base_url =http://。 MAPS_HOST。 ?/地图/ API /地理编码/ JSON地址=;
while($ row = @mysql_fetch_assoc($ result)){
if(!($ row ['lat'] * 1)){
$ geocode_pending = true;
while($ geocode_pending){
$ address = $ row [address];
$ id = $ row [id];
$ request_url = $ base_url。 。 urlencode($ address)。& sensor = false;
sleep(0.1);
$ json = file_get_contents($ request_url);
$ json_decoded = json_decode($ json);
$ status = $ json_decoded->状态;
if(strcmp($ json_decoded-> status,OK)== 0){
$ geocode_pending = false;
$ lat = $ json_decoded-> results [0] - > geometry-> location-> lat;
$ lng = $ json_decoded-> results [0] - > geometry-> location-> lng;
// echo'here';
$ query = sprintf(UPDATE markers。
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 $ id;
if(!$ update_result){
die(Invalid query:。mysql_error());
}
}
else {
//地理编码失败
$ geocode_pending = false;
回显地址。 $地址。 未能进行地理编码。
回显接收状态。 $状态。 \\\
;
}
// usleep($ delay);
require(dbinfo.php); //您的数据库参数
// Connect到数据库
$ dbh = new PDO(mysql:host = $ host; dbname = $ database,$ username,$ password);
$ dbh-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
try {
//准备查询
$ name =%。$ company。%; //通配符为PDO参数
$ countSql =SELECT COUNT(* )FROM标记WHERE`name` LIKE?;
$ countStmt = $ dbh-> prepare($ countSql);
//赋值参数
$ countStmt-> bindParam(1,$ name);
//执行查询
$ countStmt-> execute();
//检查行数
if($ countStmt-> fetchColumn()== 0){#1 EDIT改变> 0为== 0
echo查询。 //编辑从行
$ q = $ address。','。$ city。','。$ post_code。',UK';
回声\\\
;
$ 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)或die(url not loading);
if($ xml-> status ==OK){#2
//成功地理编码
$ lat = $ xml-> result-> geometry->位置 - > LAT;
$ lng = $ xml-> result->几何体 - >位置 - > lng;
$ insertSql =INSERT INTO标记(`name`,`address`,`lat`,`lng`,`type`,`link`)VALUES(?,?,?,?,?,?) ;
$ insertStmt = $ dbh-> prepare($ insertSql);
//赋值参数
$ insertStmt-> bindParam(1,$ company);
$ insertStmt-> bindParam(2,$ address);
$ insertStmt-> bindParam(3,$ lat);
$ insertStmt-> bindParam(4,$ lng);
$ insertStmt-> bindParam(5,$ type);
$ insertStmt-> bindParam(6,$ link);
//执行查询
$ insertStmt-> execute();
}#2
else {
没有行插入。;
}#2
}#1
else {#1
echo行匹配查询。; //编辑从无行
}#1
} //结束尝试
catch(PDOException $ e){
echoI'm对不起,我怕你不能那样做。 $ e-> getMessage(); //在测试后删除或修改
file_put_contents('PDOErrors.txt',date('[Ymd H:i:s]')。,myFile.php。 $ e-> getMessage()。\r\\\
,FILE_APPEND);
$ / code>
我已将您的代码转换为PDO,因为建议停止使用 mysql _ 的功能与此相反。
我已经为您解决了如何处理地理编码而不返回坐标。您还可以查看并处理以下状态代码:
OVER_QUERY_LIMIT
ZERO_RESULTS
REQUEST_DENIED
INVALID_REQUEST
请参阅 pastebin 为了实现状态代码
I've been using Google geocoding v3 for about 6 months but all of a sudden it's stopped working (I get a 610 error). It's only stopped in the last week or so.
Then I came across this (see the pink box at the top of the page!): https://developers.google.com/maps/documentation/geocoding/v2/
I've read through all the documentation and not sure where to start!
I'm hoping it's a small change as it's taken a long time to get this far, can anyone help?
[See the full site here][1]
UPDATE:
require("database.php");
// Opens a connection to a MySQL server
$con = mysql_connect("localhost", $username, $password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("teamwork_poh", $con);
$company = get_the_title();
$address = get_field('address_line_1');
$city = get_field('town_/_city');
$post_code = get_field('post_code');
$link = get_permalink();
$type = get_field('kind_of_organisation');
$sql = sprintf("select count('x') as cnt from markers where `name` = '%s'", mysql_real_escape_string($company));
$row_dup = mysql_fetch_assoc(mysql_query($sql,$con));
if ($row_dup['cnt'] == 0) {
mysql_query("INSERT INTO markers (`name`, `address`, `lat`, `lng`, `type`, `link`) VALUES ('".$company."', '".$address.", ".$city.", ".$post_code."', '0.0', '0.0', '".$type."', '".$link."')");
}
wp_reset_query();
require("database.php");
define("MAPS_HOST", "maps.googleapis.com");
define("KEY", "(my key)");
// Opens a connection to a MySQL server
$connection = mysql_connect("localhost", $username, $password);
if (!$connection) {
die("Not connected : " . mysql_error());
}
// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die("Can\'t use db : " . mysql_error());
}
$query = "SELECT * FROM markers WHERE 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/json?address=";
while ($row = @mysql_fetch_assoc($result)) {
if (!($row['lat'] * 1)) {
$geocode_pending = true;
while ($geocode_pending){
$address = $row["address"];
$id = $row["id"];
$request_url = $base_url . "" . urlencode($address) ."&sensor=false";
sleep(0.1);
$json = file_get_contents($request_url);
$json_decoded = json_decode($json);
$status = $json_decoded->status;
if (strcmp($json_decoded->status, "OK") == 0) {
$geocode_pending = false;
$lat = $json_decoded->results[0]->geometry->location->lat;
$lng = $json_decoded->results[0]->geometry->location->lng;
// echo 'here';
$query = sprintf("UPDATE markers " .
" 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 $id;
if (!$update_result) {
die("Invalid query: " . mysql_error());
}
}
else {
// failure to geocode
$geocode_pending = false;
echo "Address " . $address . " failed to geocode. ";
echo "Received status " . $status . "\n";
}
// usleep($delay);
}
}
}
As you are storing the coordinates in database it would be best to geocode when you insert new record. ie
require("dbinfo.php");//Your database parameters
//Connect to database
$dbh = new PDO("mysql:host=$host;dbname=$database", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
//Prepare query
$name = "%".$company."%";//Wildcard for PDO paramerter
$countSql = "SELECT COUNT(*) FROM markers WHERE `name` LIKE ?";
$countStmt = $dbh->prepare($countSql);
// Assign parameter
$countStmt->bindParam(1,$name);
//Execute query
$countStmt->execute();
// check the row count
if ($countStmt->fetchColumn() == 0) { #1 EDIT changed >0 to ==0
echo "No row matched the query."; //EDIT From Row
$q =$address.','.$city.','.$post_code.',UK';
echo "\n";
$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"){#2
// Successful geocode
$lat = $xml->result->geometry->location->lat;
$lng = $xml->result->geometry->location->lng;
$insertSql ="INSERT INTO markers (`name`, `address`, `lat`, `lng`, `type`, `link`) VALUES (?,?,?,?,?,?)";
$insertStmt = $dbh->prepare($insertSql);
// Assign parameter
$insertStmt->bindParam(1,$company);
$insertStmt->bindParam(2,$address);
$insertStmt->bindParam(3,$lat);
$insertStmt->bindParam(4,$lng);
$insertStmt->bindParam(5,$type);
$insertStmt->bindParam(6,$link);
//Execute query
$insertStmt->execute();
} #2
else{
"No rows inserted.";
}#2
} #1
else {#1
echo "Rows matched the query."; //EDIT From No row
} #1
}// End try
catch(PDOException $e) {
echo "I'm sorry I'm afraid you can't do that.". $e->getMessage() ;// Remove or modify after testing
file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]').", myFile.php, ". $e->getMessage()."\r\n", FILE_APPEND);
}
I have converted your code to PDO as it is advisable to stop using mysql_functions as these a deprecated.
I have left you to implement how you will deal with geocoding not returning coordinates. You can also check and deal with the following status codes
OVER_QUERY_LIMIT
ZERO_RESULTS
REQUEST_DENIED
INVALID_REQUEST
See pastebin For status code implementation
这篇关于从Google maps api v2更改为v3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!