gpt4 book ai didi

javascript - 使用 json 从数据库检索纬度经度到 php 页面

转载 作者:行者123 更新时间:2023-11-28 06:15:16 25 4
gpt4 key购买 nike

我正在尝试将数据库(mysql)中的纬度和经度绘制到 php 页面。到目前为止,我已经能够使用代码显示没有 json 的标记:

<?
$dbname ='insert mysql database name'; //Name of the database
$dbuser ='insert mysql user name'; //Username for the db
$dbpass ='insert mysql password'; //Password for the db
$dbserver ='insert mysql database server address'; //Name of the mysql server

$dbcnx = mysql_connect ("$dbserver", "$dbuser", "$dbpass");
mysql_select_db("$dbname") or die(mysql_error());
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Map API V3 with markers</title>
<style type="text/css">
body { font: normal 10pt Helvetica, Arial; }
#map { width: 350px; height: 300px; border: 0px; padding: 0px; }
</style>
<script src="http://maps.google.com/maps/api/js?v=3&sensor=false" type="text/javascript"></script>
<script type="text/javascript">
//Sample code written by August Li
var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
new google.maps.Size(32, 32), new google.maps.Point(0, 0),
new google.maps.Point(16, 32));
var center = null;
var map = null;
var currentPopup;
var bounds = new google.maps.LatLngBounds();
function addMarker(lat, lng, info) {
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
var marker = new google.maps.Marker({
position: pt,
icon: icon,
map: map
});
var popup = new google.maps.InfoWindow({
content: info,
maxWidth: 300
});
google.maps.event.addListener(marker, "click", function() {
if (currentPopup != null) {
currentPopup.close();
currentPopup = null;
}
popup.open(map, marker);
currentPopup = popup;
});
google.maps.event.addListener(popup, "closeclick", function() {
map.panTo(center);
currentPopup = null;
});
}
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(0, 0),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
}
});
<?
$query = mysql_query("SELECT * FROM poi_example");
while ($row = mysql_fetch_array($query)){
$name=$row['name'];
$lat=$row['lat'];
$lon=$row['lon'];
$desc=$row['desc'];
echo ("addMarker($lat, $lon,'<b>$name</b><br/>$desc');\n");
}
?>
center = bounds.getCenter();
map.fitBounds(bounds);

}
</script>
</head>
<body onload="initMap()" style="margin:0px; border:0px; padding:0px;">
<div id="map"></div>
</html>

因此我尝试使用 json 但它没有显示任何内容,下面是我尝试过的代码:

<?
$dbname ='insert mysql database name'; //Name of the database
$dbuser ='insert mysql user name'; //Username for the db
$dbpass ='insert mysql password'; //Password for the db
$dbserver ='insert mysql database server address'; //Name of the mysql server

$dbcnx = mysql_connect ("$dbserver", "$dbuser", "$dbpass");
mysql_select_db("$dbname") or die(mysql_error());
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Map API V3 with markers</title>
<style type="text/css">
body { font: normal 10pt Helvetica, Arial; }
#map { width: 350px; height: 300px; border: 0px; padding: 0px; }
</style>
<script src="http://maps.google.com/maps/api/js?v=3&sensor=false" type="text/javascript"></script>
<script type="text/javascript">
//Sample code written by August Li
var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
new google.maps.Size(32, 32), new google.maps.Point(0, 0),
new google.maps.Point(16, 32));
var center = null;
var map = null;
var currentPopup;
var bounds = new google.maps.LatLngBounds();
function addMarker(lat, lng, info) {
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
var marker = new google.maps.Marker({
position: pt,
icon: icon,
map: map
});
var popup = new google.maps.InfoWindow({
content: info,
maxWidth: 300
});
google.maps.event.addListener(marker, "click", function() {
if (currentPopup != null) {
currentPopup.close();
currentPopup = null;
}
popup.open(map, marker);
currentPopup = popup;
});
google.maps.event.addListener(popup, "closeclick", function() {
map.panTo(center);
currentPopup = null;
});
}
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(0, 0),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
}
});


<?
$query = mysql_query("SELECT * FROM poi_example");
while ($row = mysql_fetch_array($query)){
$name=$row['name'];
$lat=$row['lat'];
$lon=$row['lon'];
$desc=$row['desc'];
$latlong = $lat.", ". $lon;
array_push($new, $latlong);
//echo ("addMarker($lat, $lon,'<b>$name</b><br/>$desc');\n");
}
echo json_encode($new);

?>

var arr = <?php echo json_encode($new) ?>;
for(var i=0; i<arr.length;i++)
{
var lat = Number(arr[i].split(",")[0].trim());
var lon = Number(arr[i].split(",")[0].trim());
var four = new google.maps.LatLng(lat,lon);
var marker=new google.maps.Marker({
position:four,
map:map
});
}
center = bounds.getCenter();
map.fitBounds(bounds);

}
</script>
</head>
<body onload="initMap()" style="margin:0px; border:0px; padding:0px;">
<div id="map"></div>
</html>

最佳答案

我坚信你应该将两端的逻辑分开。

你的 PHP 逻辑。当您的前端逻辑 JS 独立工作时,它就可以独立工作。这样就更容易解决和理清问题所在。

以下是我认为您应该查看的解决方案。我没有你的数据库可以玩。希望您理解我建议您应该做什么才能使其正常工作。

第 1 步

PHP 代码 - PHP 文件 - yourphpfile.php

<?php
$dbname ='u769748933_tr'; //Name of the database
$dbuser ='u769748933_ta'; //Username for the db
$dbpass ='adamas'; //Password for the db
$dbserver ='mysql.1freehosting.com'; //Name of the mysql server

$dbcnx = mysql_connect ("$dbserver", "$dbuser", "$dbpass");
mysql_select_db("$dbname") or die(mysql_error());

$q = mysql_("SELECT * FROM poi_example");
while ($row = mysql_fetch_array($q)){
$name=$row['name'];
$lat=$row['lat'];
$lon=$row['lon'];
$desc=$row['desc'];
$latlong = $lat.", ". $lon;
array_push($new, $latlong);

}

echo $new; // raw data, not json encoded


?>

当您在浏览器上指向上面的代码时,它应该显示您的 JSON 编码数据。

第 2 步

您的 Javascript 文件

mymap.js 文件

     //Sample code written by August Li
var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
new google.maps.Size(32, 32), new google.maps.Point(0, 0),
new google.maps.Point(16, 32));
var center = null;
var map = null;
var currentPopup;
var bounds = new google.maps.LatLngBounds();
function addMarker(lat, lng, info) {
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
var marker = new google.maps.Marker({
position: pt,
icon: icon,
map: map
});
var popup = new google.maps.InfoWindow({
content: info,
maxWidth: 300
});
google.maps.event.addListener(marker, "click", function() {
if (currentPopup != null) {
currentPopup.close();
currentPopup = null;
}
popup.open(map, marker);
currentPopup = popup;
});
google.maps.event.addListener(popup, "closeclick", function() {
map.panTo(center);
currentPopup = null;
});
}
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(0, 0),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
}
});

var arr = 'youphpfile.php';// or url
for(var i=0; i<arr.length;i++)
{
var lat = Number(arr[i].split(",")[0].trim());
var lon = Number(arr[i].split(",")[0].trim());
var four = new google.maps.LatLng(lat,lon);
var marker=new google.maps.Marker({
position:four,
map:map
});
}
center = bounds.getCenter();
map.fitBounds(bounds);

}

第 3 步HTML 文件。

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Map API V3 with markers</title>
<style type="text/css">
body { font: normal 10pt Helvetica, Arial; }
#map { width: 350px; height: 300px; border: 0px; padding: 0px; }
</style>
<script src="http://maps.google.com/maps/api/js?v=3&sensor=false" type="text/javascript"></script>
<script src="mymap.js"></script>
</head>
<body onload="initMap()" style="margin:0px; border:0px; padding:0px;">
<div id="map"></div>
</html>

看看我建议你做什么。您不需要对 PHP 文件中的数据进行编码。然后,接下来将数据输入到 JS 端。

我希望这会有所帮助。

关于javascript - 使用 json 从数据库检索纬度经度到 php 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36033535/

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