从 HTML 中的 JavaScript 函数获取信息。如何?
我有以下函数来获取用户的当前位置(如果允许)。
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
(function () {
google.load("maps", "2");
google.setOnLoadCallback(function () {
// Create map
var map = new google.maps.Map2(document.getElementById("map_canvas")),
markOutLocation = function (lat, long) {
var latLong = new google.maps.LatLng(lat, long),
marker = new google.maps.Marker(latLong);
map.setCenter(latLong, 13);
map.addOverlay(marker);
marker.openInfoWindow(markerText);
google.maps.Event.addListener(marker, "click", function () {
marker.openInfoWindow(markerText);
});
};
map.setUIToDefault();
// Check for geolocation support
if (navigator.geolocation) {
// Get current position
navigator.geolocation.getCurrentPosition(function (position) {
// Success!
markOutLocation(position.coords.latitude, position.coords.longitude);
},
function () {
// Gelocation fallback: Defaults to Stockholm, Sweden
markOutLocation(59.3325215, 18.0643818);
}
);
}
else {
// No geolocation fallback: Defaults to Eeaster Island, Chile
markOutLocation(-27.121192, -109.366424);
}
});
})();
</script>
如果我将 alert(lat + ', ' + long);
放在 map.setCenter(latLong, 13);
上,我会在警报框中得到 59.378217、13.504219。如何将此信息获取到 BODY 标签内的 DIV 标签?
提前致谢。
I have the following function to fetch the current position of a user (if allowed).
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
<script type="text/javascript">
(function () {
google.load("maps", "2");
google.setOnLoadCallback(function () {
// Create map
var map = new google.maps.Map2(document.getElementById("map_canvas")),
markOutLocation = function (lat, long) {
var latLong = new google.maps.LatLng(lat, long),
marker = new google.maps.Marker(latLong);
map.setCenter(latLong, 13);
map.addOverlay(marker);
marker.openInfoWindow(markerText);
google.maps.Event.addListener(marker, "click", function () {
marker.openInfoWindow(markerText);
});
};
map.setUIToDefault();
// Check for geolocation support
if (navigator.geolocation) {
// Get current position
navigator.geolocation.getCurrentPosition(function (position) {
// Success!
markOutLocation(position.coords.latitude, position.coords.longitude);
},
function () {
// Gelocation fallback: Defaults to Stockholm, Sweden
markOutLocation(59.3325215, 18.0643818);
}
);
}
else {
// No geolocation fallback: Defaults to Eeaster Island, Chile
markOutLocation(-27.121192, -109.366424);
}
});
})();
</script>
If I put alert(lat + ', ' + long);
over map.setCenter(latLong, 13);
I get 59.378217, 13.504219 in a alert-box. How do I get this information to a DIV-tag within the BODY-tag?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 jQuery 的
.html()
并使用.html()
在 div 中编写警报框中的内容,如下所示:(其中
id
是元素的 id)如果您愿意,您也可以使用传统的 JavaScript:(
其中
id
是元素的 id)我希望这会有所帮助。
You could use jQuery's
.html()
and write what goes inside the alert box in the div using.html()
like this:(where
id
is the element's id)You could also use traditional JavaScript, if you prefer:
(where
id
is the element's id)I hope this helps.
您可以使用
document.getElementById(id).innerHTML
,其中 id 是元素的 id。前任。
document.getElementById("坐标").innerHTML = lat + ', ' + long;
还有 document.getElementById(id).textContent
You can use
document.getElementById(id).innerHTML
where id is the id of the element.Ex.
document.getElementById("coordinates").innerHTML = lat + ', ' + long;
There is also document.getElementById(id).textContent