I would like to display coordinates - retrieved from an Oracle database - as markers on a Google Map by calling pl/sql variables from within a javascript function. This is all done in APEX 5. Any help would be greatly appreciated!
What works: I select GEOMETRY (SDO_GEOMETRY) in the PHOTO table into l_lng (longitude) and l_lat (latitude) variables:
SELECT t.x Longitude,
t.y Latitude
FROM photo,
TABLE(sdo_util.getvertices(photo.geometry)) t;
Using DBMS_OUTPUT.put_line(l_lng); and DBMS_OUTPUT.put_line(l_lat);, I can see that l_lng and l_lat successfully stored the values because the output is:
-4.083714
50.315475
Specifically, the javascript variable myLatLng needs to pull the coordinates from l_lng and l_lat.
var myLatLng = {lat: l_lat, lng: l_lng};
My full code:
DECLARE
l_lng NUMBER;
l_lat NUMBER;
begin
SELECT t.x Longitude,
t.y Latitude
INTO l_lng, l_lat
FROM photo,
TABLE(sdo_util.getvertices(photo.geometry)) t
WHERE photo.id=39;
htp.print('
<html>
<head>
<style>
#map {
width: 500px;
height: 400px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
var myLatLng = {
lat: l_lat,
lng: l_lng
};
var map = new google.maps.Map(document.getElementById("map"), {
zoom: 17,
center: myLatLng
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});
}
</script>
</head>
<body>
<div id="map"></div>
</body>
</html>
');
end;
As suggested by @vmachan, I modified my ht.print statement to read:
htp.print('<html>');
htp.print('<head>');
htp.print('<style>
#map {
width: 500px;
height: 400px;
}
</style>
');
htp.print('<INPUT TYPE="hidden" NAME="p_lng" VALUE="'||l_lng||'">');
htp.print('<INPUT TYPE="hidden" NAME="p_lat" VALUE="'||l_lat||'">');
htp.print('
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
function initialize() {
var myLatLng = {
lat: p_lat,
lng: p_lng
};
var map = new google.maps.Map(document.getElementById("map"), {
zoom: 17,
center: myLatLng
});
}
</script>
');
htp.print('</head>');
htp.print('
<body>
<div id="map"></div>
</body>
');
htp.print('</html>');
text, it prints the correct coordinates on the page), but the map does not display though. It works if I hard code the coordinates tomyLatLnglike so:var myLatLng = { lng: -4.083714, lat: 50.315475 };suggesting that the javascript is not picking up the values! I'll add my htp.print statement to my question.