Get JavaScript function information in HTML. How?

advertisements

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.


You could use jQuery's .html() and write what goes inside the alert box in the div using .html() like this:

$('#id').html(lat + ', ' + long)

(where id is the element's id)

You could also use traditional JavaScript, if you prefer:

document.getElementById('id').innerHTML = lat + ', ' + long;

(where id is the element's id)

I hope this helps.