Google Earth API链接.setHref在使用变量时不起作用
Google Earth API link.setHref not working when using variable
变量正在获取正确的数据,但在href参数中不起作用。我添加了一个带有变量的按钮,以便在浏览器中查看它。如果我把硬代码的值,它是有注释的,它是有效的。
<?php
@session_start();
$idcoord = $_GET['search_fd0'];
$kmlpath = "http://nonprasa.t15.org/kml/PR" . $idcoord . "/doc.kml";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Earth API Sample</title>
<script src="http://www.google.com/jsapi?key=ABQIAAAAuPsJpk3MBtDpJ4G8cqBnjRRaGTYH6UMl8mADNa0YKuWNNa8VNxQCzVBXTx2DYyXGsTOxpWhvIG7Djw" type="text/javascript"></script>
<script type="text/javascript">
function addSampleButton(caption, clickHandler) {
var btn = document.createElement('input');
btn.type = 'button';
btn.value = caption;
if (btn.attachEvent)
btn.attachEvent('onclick', clickHandler);
else
btn.addEventListener('click', clickHandler, false);
// add the button to the Sample UI
document.getElementById('sample-ui').appendChild(btn);
}
function addSampleUIHtml(html) {
document.getElementById('sample-ui').innerHTML += html;
}
</script>
<script type="text/javascript">
var ge;
google.load("earth", "1");
function init() {
// var kmlfile = ''"<?php echo $kmlpath; ?>'"';
var kmlfile = '<?php echo $kmlpath;?>';
google.earth.createInstance('map3d', initCallback, failureCallback);
addSampleButton(kmlfile, buttonClick);
}
function initCallback(instance) {
ge = instance;
ge.getWindow().setVisibility(true);
// add a navigation control
ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO);
// add some layers
ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true);
ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true);
createNetworkLink();
document.getElementById('installed-plugin-version').innerHTML =
ge.getPluginVersion().toString();
}
function failureCallback(errorCode) {
}
function createNetworkLink() {
var networkLink = ge.createNetworkLink("");
networkLink.setDescription("NetworkLink open to fetched content");
networkLink.setName("Open NetworkLink");
networkLink.setFlyToView(true);
// create a Link object
var link = ge.createLink("");
//link.setHref("http://nonprasa.t15.org/kml/PR0302013/doc.kml);
link.setHref (kmlfile);
// attach the Link to the NetworkLink
networkLink.setLink(link);
// add the NetworkLink feature to Earth
ge.getFeatures().appendChild(networkLink);
// look at the placemark we created
var la = ge.createLookAt('');
la.set(18, -67,
0, // altitude
ge.ALTITUDE_RELATIVE_TO_GROUND,
0, // heading
0, // straight-down tilt
1500 // range (inverse of zoom)
);
ge.getView().setAbstractView(la);
}
function buttonClick() {
// Get the current view.
var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND);
// Zoom out to x times the current range.
lookAt.setRange(lookAt.getRange() * 5.0);
// Update the view in Google Earth.
ge.getView().setAbstractView(lookAt);
}
</script>
</head>
<body onload="init()" style="font-family: arial, sans-serif; font-size: 8px; border: 0;">
<div id="sample-ui"></div>
<div id="map3d" style="width: 1200px; height: 800px;"></div>
<br>
<div>Installed Plugin Version: <span id="installed-plugin-version" style="font-weight: bold;">Loading...</span></div>
</body>
kmlfile
是init
函数的本地变量,因此当您尝试在createNetworkLink
函数中使用它时,它总是未定义的。
为了强调我的意思,请看以下内容,为了清晰起见,我删除了其他代码。。。
function init() {
var kmlfile = '<?php echo $kmlpath;?>'; // kmlfile defined here
}
function createNetworkLink() {
link.setHref(kmlfile); // kmlfile is undefined here
alert(kmlfile); // this would have told you as much...
}
要修复它,可以将kmlfile
设置为全局变量,以便它在createNetworkLink
函数的范围内可用。要做到这一点,只需在init
方法之外创建变量,就像使用ge
变量一样。
再次,为了清楚起见,请删除其他代码并查看以下内容。
<script type="text/javascript">
var ge;
var kmlfile; // kmlfile defined here
function init() {
kmlfile = '<?php echo $kmlpath;?>'; // set the variable
}
function createNetworkLink() {
link.setHref(kmlfile); // kmlfile is now available here
}
</script>
相关文章:
- javascript美元符号变量不起作用
- Url中的JavaScript变量不起作用
- 全局变量不起作用
- 彩色变量不起作用
- 变量不起作用(不知道为什么)javascript
- Angularjs 更新 setTimeout 中的范围变量不起作用
- 将 jQuery 函数分配给变量不起作用,但包装它不起作用
- 谷歌图表-重新格式化变量不起作用
- AngularJS-ng显示范围变量不起作用
- 在 JavaScript 中设置变量不起作用
- 如何将 html 输入值传递给 php?- 变量不起作用
- Javascript变量等于下面的另一个变量不起作用
- Javascript 初学者 - 全局变量不起作用
- JavaScript 中的全局变量不起作用
- d3 本地存储比较数组 .filter 咖啡脚本;数组变量不起作用
- JS中的PHP变量不起作用
- 作为“名称”的Javascript数组变量不起作用
- 鼠标上的补间变量不起作用
- Javascript onclick 变量不起作用
- 角度 JS 范围变量不起作用