gpt4 book ai didi

php - php错误传递给google map api的地址坐标

转载 作者:行者123 更新时间:2023-12-03 08:11:48 25 4
gpt4 key购买 nike

我收到以下错误

    Notice: Trying to get property of non-object in xxx/index.php on line 22

当我使用此代码时:
    $lon = $_GET['lon'];
$lat = $_GET['lat'];
$address = $_GET['address'];
$range = $_GET['range'];

if($address != 'false') {
$address = str_replace(" ", "+", $address); // replace all the white space with "+" sign to match with google search pattern
$url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=$address";
$response = file_get_contents($url);
$json = json_decode($response,TRUE); //generate array object from the response from the web

//these are the latitude and longitude definitions
$lat = $response->results->geometry->location->lat;
$lon = $response->results->geometry->location->lng;
}

我很不熟悉php。谁能给我一些指导吗?
谢谢您的帮助

**编辑(显示完整代码):
2010年1月由TECFA的Daniel K. Schneider制作。这是免费软件。
将连接到MySQL数据库,执行SQL语句并
然后将结果作为有效的XML返回。
*/
error_reporting(E_ALL);
      // ---------------------------  Configuration section
$lon = $_GET['lon'];
$lat = $_GET['lat'];
$address = $_GET['address'];
$range = $_GET['range'];

if($address != 'false') {
$address = str_replace(" ", "+", $address); // replace all the white space with "+" sign to match with google search pattern
$url = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=$address";
$response = file_get_contents($url);
$json = json_decode($response); //generate array object from the response from the web

//these are the latitude and longitude definitions
$lat = $json->results->geometry->location->lat;
$lon = $json->results->geometry->location->lng;
}
// Fill in according to your db settings (example is for a local and locked "play" WAMP server)
// Make sure to keep the ; and the ""
$host = "x";
$user = "x";
$pass = "x";
$database = "x";

// Replace by a query that matches your database
$SQL_query =
"SELECT 3956 * 2 * ASIN(SQRT(POWER(SIN(('".$lat."' -abs(lat)) * pi()/180 / 2),2) + COS('".$lat."' * pi()/180 ) * COS(abs(lat) *pi()/180) * POWER(SIN(('".$lon."' - `long`) *pi()/180 / 2), 2) ))as distance FROM wp_places_locator dest having distance < '".$range."' ORDER BY distance limit 10;";

// Optional: add the name of XSLT file.
// $xslt_file = "mysql-result.xsl";

// -------------------------- no changes needed below

$DB_link = mysql_connect($host, $user, $pass) or die ("Could not find or access the database.");
mysql_select_db($database, $DB_link) or die ("Could not find or access the database.");
$result = mysql_query($SQL_query, $DB_link) or die ("Data not found. Your SQL query didn't work... ");

// we produce XML
header("Content-type: text/xml");
$XML = "<?xml version=\"1.0\"?>\n";
//if ($xslt_file) $XML .= "<?xml-stylesheet href=\"$xslt_file\" type=\"text/xsl\"
// root node
$XML .= "<result>\n";
// rows
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$XML .= "\t<row>\n";
$i = 0;
// cells
foreach ($row as $cell) {
// Escaping illegal characters - not tested actually ;)
$cell = str_replace("&", "&amp;", $cell);
$cell = str_replace("<", "&lt;", $cell);
$cell = str_replace(">", "&gt;", $cell);
$cell = str_replace("\"", "&quot;", $cell);
$col_name = mysql_field_name($result,$i);
// creates the "<tag>contents</tag>" representing the column
$XML .= "\t\t<" . $col_name . ">" . $cell . "</" . $col_name . ">\n";
$i++;
}
$XML .= "\t</row>\n";
}
$XML .= "</result>\n";

// output the whole XML string
echo $XML;
?>

最佳答案

您收到错误消息是因为您将json编码为一个关联数组(您将true作为第二个参数传递),并且试图使用另一个变量名将其作为一个对象进行访问,因此请删除第二个参数

//$json = json_decode($response,TRUE); //generate array object from the response from the web
//Don't pass true as second parameter
$json = json_decode($response);

并使用正确的对象变量
$lat = $json->results->geometry->location->lat;
$lon = $json->results->geometry->location->lng;

关于php - php错误传递给google map api的地址坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22516218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com