0

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:

enter image description here

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:

enter image description here

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.');
}

1

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you sir. I created a table with HTML and then passed the latitude and longitude to the id of the <td> with the method you suggested!
1

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.');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.