简体   繁体   English

如何将数据从 javascript 导入 html 表

[英]How to import data from javascript to html table

I have created a project that when the page opens, it asks the user to accept his location.我创建了一个项目,当页面打开时,它会要求用户接受他的位置。 When the user accepts, a google maps window opens, displaying the user's current location as well as the coordinates:当用户接受时,谷歌地图 window 打开,显示用户的当前位置以及坐标:

在此处输入图像描述

I want the coordinates to be displayed in an html table next to the google maps window, something similar or the same as this below:我希望坐标显示在谷歌地图 window 旁边的 html 表中,类似于以下内容:

在此处输入图像描述

How can I achieve this?我怎样才能做到这一点? I am a begginer in javascript and I need help.我是 javascript 的初学者,我需要帮助。 Thank you!!!谢谢!!!

I will post my javascript code in case it's needed:如果需要,我将发布我的 javascript 代码:

 // SHOW COORDINATES if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function (p) { var LatLng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude); var mapOptions = { center: LatLng, zoom: 13, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions); var marker = new google.maps.Marker({ position: LatLng, map: map, title: "<div style = 'height:60px;width:200px'><b>Your location:</b><br />Latitude: " + p.coords.latitude + "<br />Longitude: " + p.coords.longitude }); google.maps.event.addListener(marker, "click", function (e) { var infoWindow = new google.maps.InfoWindow(); infoWindow.setContent(marker.title); infoWindow.open(map, marker); }); }); } else { alert('Geo Location feature is not supported in this browser.'); }

You could create a table element and append it to document.body :您可以创建一个table元素并将 append 它添加到document.body

 // SHOW COORDINATES if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function (p) { let table = document.createElement("table"); table.innerHTML = "<tr><th>Latitude</th><th>Longitude</th></tr><tr><td>" + p.coords.latitude + "</td><td>" + p.coords.longitude + "</td></tr>"; document.body.appendChild(table); }); } else { console.log('Geo Location feature is not supported in this browser.'); }

If you are looking for a solution to get the lat and lng then here is how you can get inside the function you are using如果您正在寻找获取 lat 和 lng 的解决方案,那么这里是您如何进入您正在使用的 function 的方法

   lat = p.coords.latitude;
   lng = p.coords.longitude;

Then you can learn how to insert data in DOM something like this然后你可以学习如何在 DOM 中插入数据,就像这样

document.getElementById('latvalue').innerHTML += lat;
document.getElementById('lngvalue').innerHTML += lng;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM