简体   繁体   中英

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:

在此处输入图像描述

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:

在此处输入图像描述

How can I achieve this? I am a begginer in javascript and I need help. Thank you!!!

I will post my javascript code in case it's needed:

 // 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 :

 // 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 = p.coords.latitude;
   lng = p.coords.longitude;

Then you can learn how to insert data in DOM something like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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