我是网络开发新手,我需要帮助。 这是我的代码,它假设用数据库中的获取坐标更新标记位置
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=false"></script>
<script type="text/javascript">
Markers();
function Markers() {
var myvar = <?php echo json_encode($lat); ?>;
var myvar2 = <?php echo json_encode($long); ?>;
document.write(myvar ,myvar2 );
markers = [
{
"title": 'Aksa Beach',
"lat": myvar,
"lng": myvar2,
"description": 'Driver Name: Mohamed'
},
{
"title": 'Juhu Beach',
"lat": '19.0883595',
"lng": '72.82652380000002',
"description": 'Juhu Beach is one of favourite tourist attractions situated in Mumbai.'
},
{
"title": 'Girgaum Beach',
"lat": '18.9542149',
"lng": '72.81203529999993',
"description": 'Girgaum Beach commonly known as just Chaupati is one of the most famous public beaches in Mumbai.'
},
{
"title": 'Jijamata Udyan',
"lat": '18.979006',
"lng": '72.83388300000001',
"description": 'Jijamata Udyan is situated near Byculla station is famous as Mumbai (Bombay) Zoo.'
},
{
"title": 'Sanjay Gandhi National Park',
"lat": '19.2147067',
"lng": '72.91062020000004',
"description": 'Sanjay Gandhi National Park is a large protected area in the northern part of Mumbai city.'
}
];
//return markers;
}
window.onload = function () {
LoadMap();
};
var map;
var marker;
function LoadMap() {
var mapOptions = {
center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
SetMarker(0);
};
//while loop to call to update lan/long
function SetMarker(position) {
//Remove previous Marker.
if (marker != null) {
marker.setMap(null);
}
//Set Marker on Map.
var data = markers[position];
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title
});
//Create and open InfoWindow.
var infoWindow = new google.maps.InfoWindow();
infoWindow.setContent("<div style = 'width:200px;min-height:40px'>" + data.description + "</div>");
infoWindow.open(map, marker);
};
</script>
<div>
<label for="rbMarker0">
<input type="radio" id="rbMarker0" name="rbMarker" value="0" onclick="SetMarker(this.value)"
checked="checked" />Driver Name: Mohamed </label><br />
<label for="rbMarker1">
<input type="radio" id="rbMarker1" name="rbMarker" value="1" onclick="SetMarker(this.value)" />Juhu
Beach</label><br />
<label for="rbMarker2">
<input type="radio" id="rbMarker2" name="rbMarker" value="2" onclick="SetMarker(this.value)" />Girgaum
Beach</label><br />
<label for="rbMarker3">
<input type="radio" id="rbMarker3" name="rbMarker" value="3" onclick="SetMarker(this.value)" />Jijamata
Udyan</label><br />
<label for="rbMarker4">
<input type="radio" id="rbMarker4" name="rbMarker" value="4" onclick="SetMarker(this.value)" />Sanjay
Gandhi National Park</label>
</div>
<hr />
<div id="dvMap" style="width: 400px; height: 500px">
</div>
<?php
include_once(dirname(__FILE__).'/connection.php');
while (1)
{
$sql = "SELECT * FROM `test2` WHERE `ID` = 298";
$res = $mysqli->query($sql) or die($mysqli->error.__LINE__);
while( $row = $res->fetch_row() ){
$long = $row[1];
$lat = $row[2] ;
}
echo "<script> Markers(); </script>";
}
?>
有两个问题:
myvar
,myvar2
始终为null
,但我从数据库中获取数据。答案 0 :(得分:0)
PHP是一种脚本语言,目标是接收请求,处理它,生成输出并完成脚本。如果您没有完成脚本,浏览器将始终认为您的脚本未完成,并将显示页面仍在加载。
使用while(true)或while(1)的绘图循环,您将在php中看不到。 并且你在脚本末尾声明了变量$ long和$ lat,它们的值在开头就是null,因为你以前从未声明它们。
要创建更干净的代码,请尝试分离php,html和javascript。尽量避免在函数范围之外更改内容,并在函数中更改参数时声明它。
<?php
include_once(dirname(__FILE__).'/connection.php'); //there is also __DIR__
//while (1)
//{
$sql = "SELECT * FROM `test2` WHERE `ID` = 298";
$res = $mysqli->query($sql) or die($mysqli->error.__LINE__);
while( $row = $res->fetch_row() ){
$long = $row[1];
$lat = $row[2] ;
}
//echo "<script> Markers(); </script>";
//}
?>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=false"></script>
<script type="text/javascript">
var myvar = <?php echo $lat;?>;
// we dont' need json encode, because we want the value directly echo json_encode($lat);
var myvar2 = <?php echo $long;?>;
//json_encode($long);
//what should happen here? like this you replace the whole html document with myvar and myvar2 document.write(myvar ,myvar2 );
markers = [
{
"title": 'Aksa Beach',
"lat": myvar,
"lng": myvar2,
"description": 'Driver Name: Mohamed'
},
{
"title": 'Juhu Beach',
"lat": '19.0883595',
"lng": '72.82652380000002',
"description": 'Juhu Beach is one of favourite tourist attractions situated in Mumbai.'
},
{
"title": 'Girgaum Beach',
"lat": '18.9542149',
"lng": '72.81203529999993',
"description": 'Girgaum Beach commonly known as just Chaupati is one of the most famous public beaches in Mumbai.'
},
{
"title": 'Jijamata Udyan',
"lat": '18.979006',
"lng": '72.83388300000001',
"description": 'Jijamata Udyan is situated near Byculla station is famous as Mumbai (Bombay) Zoo.'
},
{
"title": 'Sanjay Gandhi National Park',
"lat": '19.2147067',
"lng": '72.91062020000004',
"description": 'Sanjay Gandhi National Park is a large protected area in the northern part of Mumbai city.'
}
];
var map;
var marker;
window.onload = function () {
map = LoadMap();
};
function LoadMap() {
var mapOptions = {
center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
zoom: 10,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
SetMarker(0, map);
return map; // try to not change variables outside the function scope
};
//while loop to call to update lan/long
function SetMarker(position, map) {
//Remove previous Marker.
if (marker != null) {
marker.setMap(null);
}
//Set Marker on Map.
var data = markers[position];
var myLatlng = new google.maps.LatLng(data.lat, data.lng);
marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: data.title
});
//Create and open InfoWindow.
var infoWindow = new google.maps.InfoWindow();
infoWindow.setContent("<div style = 'width:200px;min-height:40px'>" + data.description + "</div>");
infoWindow.open(map, marker);
};
</script>
<div>
<label for="rbMarker0">
<input type="radio" id="rbMarker0" name="rbMarker" value="0" onclick="SetMarker(this.value, map)"
checked="checked" />Driver Name: Mohamed </label><br />
<label for="rbMarker1">
<input type="radio" id="rbMarker1" name="rbMarker" value="1" onclick="SetMarker(this.value, map)" />Juhu
Beach</label><br />
<label for="rbMarker2">
<input type="radio" id="rbMarker2" name="rbMarker" value="2" onclick="SetMarker(this.value, map)" />Girgaum
Beach</label><br />
<label for="rbMarker3">
<input type="radio" id="rbMarker3" name="rbMarker" value="3" onclick="SetMarker(this.value, map)" />Jijamata
Udyan</label><br />
<label for="rbMarker4">
<input type="radio" id="rbMarker4" name="rbMarker" value="4" onclick="SetMarker(this.value, map)" />Sanjay
Gandhi National Park</label>
</div>
<hr />
<div id="dvMap" style="width: 400px; height: 500px">
</div>