gpt4 book ai didi

php - 尝试用 MySQL、PHP 和谷歌地图做商店定位器

转载 作者:行者123 更新时间:2023-11-30 23:03:08 26 4
gpt4 key购买 nike

我正在尝试使用 Google map 将页面安装到我的移动和 Web 应用程序中,使用 PHP 从 MySQL 数据库中提取坐标。我试过教程,一个将信息放入 XML 中,另一个将信息放入 JSON 中。但是,这两个教程(包括他们的表格)都没有向我返回任何信息。我什至尝试过非 PHP5 的代码。我返回的最多的是

<markers>
</markers>

有人有什么建议吗?

我遵循的 2 个教程是;

提前致谢宝拉

JSON 代码

<?php
session_start();
header("Content-type: text/xml");
$dbhost = '';
$dbuser = '';
$dbpass = '';
$db = '';

function die_with_error($error) {
$ret = array(
"status" => "Failed",
"error" => $error
);
die(json_encode($ret));
}

$center_lat = ( isset( $_GET["lat"] ) ? $_GET["lat"] : 37 ); # You could replace these "0"s with the
$center_lng = ( isset( $_GET["lng"] ) ? $_GET["lng"] : -122 ); # Lat/Lng of a default location.
$radius = ( isset( $_GET["radius"] ) ? $_GET["radius"] : 10 ); # Again, default
if (!$center_lat || !$center_lng)
die_with_error("invalid parameters");


mysql_connect($dbhost, $dbuser, $dbpass)
or die_with_error(mysql_error());
mysql_select_db($db) or die_with_error(mysql_error());
mysql_set_charset('utf8');

$query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));
$result = mysql_query($query);

if (! $result)
die_with_error(mysql_error());
$result_array = array();
while ($row = mysql_fetch_assoc($result)) {
array_push($result_array, array(
"lat" => $row['lat'],
"lng" => $row['lng'],
"address" => $row['address'],
"distance" => $row['distance']
));
}

$ret = array(
"status" => "OK",
"data" => $result_array);
die(json_encode($ret));
?>

对于 XML 来说是

function parseToXML($htmlStr)  
{
$xmlStr=str_replace('<','&lt;',$htmlStr);
$xmlStr=str_replace('>','&gt;',$xmlStr);
$xmlStr=str_replace('"','&quot;',$xmlStr);
$xmlStr=str_replace("'",'&#39;',$xmlStr);
$xmlStr=str_replace("&",'&amp;',$xmlStr);
return $xmlStr;
}
$center_lat = ( isset( $_GET["lat"] ) ? $_GET["lat"] : 37 ); # You could replace these "0"s with the
$center_lng = ( isset( $_GET["lng"] ) ? $_GET["lng"] : -122 ); # Lat/Lng of a default location.
$radius = ( isset( $_GET["radius"] ) ? $_GET["radius"] : 10 ); # Again, default

$connection=mysql_connect ('mysql2017int.cp.blacknight.com', $dbuser, $dbpass);
if (!$connection) {
die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db($db, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}

$query = sprintf("SELECT 'address', 'name', 'lat', 'lng', ( 3959 * acos( cos( radians('%s') ) * cos( radians( 'lat' ) ) * cos( radians( 'lng' ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( 'lat' ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
mysql_real_escape_string($center_lat),
mysql_real_escape_string($center_lng),
mysql_real_escape_string($center_lat),
mysql_real_escape_string($radius));
$result = mysql_query($query);

if (!$result) {
die('Invalid query: ' . mysql_error());
}

echo "<markers>\n";
while ($row = mysql_fetch_assoc($result)){
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'address="' . parseToXML($row['address']) . '" ';
echo 'lat="' . $row['lat'] . '" ';
echo 'lng="' . $row['lng'] . '" ';
echo 'distance="' . $row['distance'] . '" ';
echo "/>\n";
}

echo "</markers>\n";

最佳答案

教程中提供的半径不够。如果将它增加到 25000 而不是 25,它就起作用了。

希望这对以后的其他人有所帮助。

关于php - 尝试用 MySQL、PHP 和谷歌地图做商店定位器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23069643/

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