gpt4 book ai didi

javascript - 样式化 javascript document.write

转载 作者:行者123 更新时间:2023-12-02 18:47:02 27 4
gpt4 key购买 nike

我在 HTML5 页面中使用谷歌地图。我想在一个段落中显示 map 上方的坐标,但是当执行以下代码时,它会在空白页面上显示变量。

如何让坐标显示在同一页面的 map 上方?

    <!DOCTYPE HTML>
<html>
<head>
<title>Geolocation With a Map</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript">
function TestGeo()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition( TestMap, error, {maximumAge: 3000000, timeout: 1000000, enableHighAccuracy: true} );
}
else
{
alert("Sorry, but it looks like your browser does not support geolocation.");
}
}
//Create a new map variable
var map;
function TestMap(position)
{
// Define the coordinates as a Google Maps LatLng Object
var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

// Prepare the map options
var mapOptions =
{
zoom: 10,
center: coords,
mapTypeControl: false,
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
mapTypeId: google.maps.MapTypeId.ROADMAP
};

// Create the map, and place it in the map_canvas div
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

// Place the initial marker
var marker = new google.maps.Marker({
position: coords,
map: map,
title: "Your current location!"
});

}

function error() {
alert("Cannot locate user");
}

</script>

</head>
<body onload="TestGeo();">

<header>
<nav>
<ul>
<li><button onclick="location.href='takePicture.html'"><img src="/img/pic_icon.png" alt="Take Picture" /><br />Take<br />Picture</button>

<button onclick="location.href='gallery.html'"><img src="/img/gallery_icon.png" alt="Gallery" /><br />Show<br />Gallery</button>
<button onclick="location.href='recordsound.html'"><img src="/img/rec_icon.png" alt="Audio Recorder" /><br />Record<br />Sound</button>
<button onclick="location.href='more.html'"><img src="/img/more_icon.png" alt="More Options" /><br />More<br />Options</button>
</li>
</ul>

</nav>
</header>

<section>
<article>
<script>
var lat = position.coords.latitude;
var lon = position.coords.longitude;
document.write('<p>Latitude: ' + lat + '<br>Longitude: ' + lon + '</p>');
</script>
<br />
<div id="map_canvas" style="width: 300px; height: 300px; border-right: 1px solid #666666; border-bottom: 1px solid #666666; border-top: 1px solid #AAAAAA; border-left: 1px solid #AAAAAA;"></div>
</article>
</section>

</body>
</html>

编辑

更新的代码:

    <section>
<article>
<p>Latitude: <span id="lat"></span><br />Longitude: <span id="lon"></span></p>
<script>
var lat = position.coords.latitude;
var lon = position.coords.longitude;
document.getElementById('lat').innerHTML = lat;
document.getElementById('lon').innerHTML = lon;
</script>
<br />
<div id="map_canvas" style="width: 300px; height: 300px; border-right: 1px solid #666666; border-bottom: 1px solid #666666; border-top: 1px solid #AAAAAA; border-left: 1px solid #AAAAAA;"></div>
</article>
</section>

这似乎没有打印出坐标。

最佳答案

改变

<script>
var lat = position.coords.latitude;
var lon = position.coords.longitude;
document.write('<p>Latitude: ' + lat + '<br>Longitude: ' + lon + '</p>');
</script>

致:

<p>Latitude: <span id="lat"></span><br />Longitude: <span id="lon"></span></p>
<script>
var lat = position.coords.latitude;
var lon = position.coords.longitude;
document.getElementById('lat').innerHTML = lat;
document.getElementById('lon').innerHTML = lon;
</script>

关于javascript - 样式化 javascript document.write,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16262822/

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