Getting the initial value for a marker

10 views
Skip to first unread message

Alan94539

unread,
Nov 15, 2009, 11:57:54 PM11/15/09
to gmaps4jsf-dev
I am using the value attribute of <m:marker> to call a backing bean
with the lat/long value. This part works great, but only if the user
drags the marker. Is there a way that I can get the lat/long value
if the user doesn't drag the marker?

The application lets the user adjust the map in other ways, and the
default new marker is in the center of the map. So I want to record
the coordinates when the form is submitted without requiring the user
to move the marker.

dapooley

unread,
Nov 16, 2009, 6:24:13 AM11/16/09
to gmaps4jsf-dev
function marker1DragHandler(point)
{
document.getElementById("newLoc:mapMouseLat").value = point.y;
document.getElementById("newLoc:mapMouseLon").value = point.x;
}

OK for dragging, but the function can be populated from a bean EL too
I would guess (to initialise).

document.getElementById("newLoc:mapMouseLat").value = #
{myMarkerPojo.lat}

Alan94539

unread,
Nov 16, 2009, 11:44:33 AM11/16/09
to gmaps4jsf-dev
On Nov 16, 3:24 am, dapooley <dapoo...@gmail.com> wrote:
>
> OK for dragging, but the function can be populated from a bean EL too
> I would guess (to initialise).
>

OK, this is the right approach, but I can't seem to find the right
event to tie the event listener to.

Can someone point me to a list of what are the available values for
the eventName attribute for the eventListner tag? What I need is a
"load" event but that doesn't work.

<m:map ... >
<m:eventListener eventName="moveEnd" .. /> -- this works
<m:eventlistener eventName="load" .. /> -- this doesn't work.
</m:map>

dapooley

unread,
Nov 16, 2009, 6:56:47 PM11/16/09
to gmaps4jsf-dev
For the "load", what/where is the actual data that you want to use?
Its a marker value right?
So you will have:

<m:map>
<m:marker lat="#{pojo.lat}" long="#{pojo.long}" />
</m:map>

<script>
document.getElementById("newLoc:mapMouseLat").value = #{pojo.lat}
</script>

The script will be run on each page load (whether you want that or
not, i don't know) using
the pojo to set the form element. The pojo has page scope - or its
bean.getPojo does - so
it can be used outside of the m:map as well.

That way you could put a marker on the map and have a sidebar of all
the markers on the map
(whether currently visible in the viewport or not), and use the
sidebar items as links to jump to
that marker, show its info window etc.

Alan94539

unread,
Nov 18, 2009, 10:24:19 AM11/18/09
to gmaps4jsf-dev
On Nov 16, 3:56 pm, dapooley <dapoo...@gmail.com> wrote:

> For the "load", what/where is the actual data that you want to use?
> Its a marker value right?

I am not looking for when the page is loaded but instead where the map
is loaded. More specifically, where the map bounds are set.

My code so far looks like this:

<script type="text/javascript">
function setLatLong(x, y) {
document.getElementById
("LocationSelector:locx").value = x;
document.getElementById
("LocationSelector:locy").value = y;
document.getElementById("mlocation").innerHTML
=
"Latitude: " + y + " Longitude: " + x;
}
function readMarker(point) {
setLatLong(point.x, point.y);
}
function readMapCenter() {
var point = theMap.getCenter();
alert("readMapCenter Called = " + point);
setLatLong(point.x, point.y);
marker.setLatLng(point)
}
</script>

And the Map tag looks like this:

The marker is at <div id="mlocation">(not set
yet)</div></p>
<m:map width="400px" height="400px" address="#
{LocationBrowser.address}"
jsVariable="theMap" >
<m:mapControl name="GLargeMapControl"
position="G_ANCHOR_BOTTOM_RIGHT"/>
<m:eventListener eventName="moveend"
jsFunction="readMapCenter" />
<m:eventListener eventName="load"
jsFunction="readMapCenter" />
<m:marker id="marker" draggable="true"
jsVariable="marker" value="#{LocationBrowser.markerLocation}">
<m:eventListener eventName="dragend"
jsFunction="readMarker"/>
</m:marker>
</m:map>

The tags with ID locx and locy (not shown) are <inputHidden> tags.
They get updated along with the <div> tag whenever a dragend event
happens on the marker, just as advertised. Also when I drag the map
(the moveend event) the marker moved to the center and the tags get
updated.

However when the page is loaded, there is a marker placed in the
center of the map, but none of the events I have registered have yet
fired, so my inputHidden fields are left blank. There is a "load"
event for the GMap object listed in the API, but this isn't reflected
in gmaps4jsf.



dapooley

unread,
Nov 21, 2009, 2:38:52 AM11/21/09
to gmaps4jsf-dev
Having a <script> after the map will execute after the map is loaded
so its jsvariable will exist.

You should simply be able to do:

<script>
function readMapCenter() {...}
<script>

<m:map>...
</m:map>

<script>
readMapCenter();
</script>
Reply all
Reply to author
Forward
0 new messages