Reliable methods for centering a p:gmap?

UI Components for JSF
Post Reply
healeyb
Posts: 365
Joined: 07 Apr 2010, 16:05

28 Jul 2011, 20:40

Getting a google map to center correctly seems to be a moving target,
especially as a cross browser solution. Ironically I seem to have more
problems with Chrome than IE. This is not a grumble about the
primefaces component, they must be changing things at the google
end because problems seems to crop up having made no primefaces
upgrades.

The sqmapinit() function below actuallly seems to work on IE & Chrome
today, but it seems a bit over complicated - has anyone got anything
that works for more than a week that I could use? simply calling
checkResize() just doesn't do it for me.

The problems I encounter are that the map is centered about 6 miles
away from the lat/lng I pass in, or only a partial map is rendered in the
top right of the window.

(apologies for the javascript - it was the best I could manage... )

Code: Select all

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.prime.com.tr/ui"
    xmlns:m="http://code.google.com/p/gmaps4jsf/"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions">
    <p:dialog id="mapdialog" widgetVar="dlgmap"
              onShow="sqmapinit();">
        <h:panelGroup id="mapAjaxTarget">
            <script type="text/javascript">
                //<![CDATA[
                jQuery(sqmapinit = function() {
                    //var saveCentre = mymap.getMap().getCenter();
                    mymap.checkResize();
                    //mymap.getMap().setCenter(saveCentre);
                    // google seemed to change something within week or two 28/04/11 - fixed
                    var mygmap = mymap.getMap();
                    if("#{manageEventsView.dialogAddress.lat}" == "") {
                        var mylat = 0;
                    }
                    else {
                        var mylat = "#{manageEventsView.dialogAddress.lat}";
                    }
                    if("#{manageEventsView.dialogAddress.lng}" == "") {
                        var mylng = 0;
                    }
                    else {
                        var mylng = "#{manageEventsView.dialogAddress.lng}";
                    }
                    var mycenter = new google.maps.LatLng(mylat,mylng);
                    mygmap.setCenter(mycenter);
                    //google.maps.event.trigger(mygmap, 'resize');
                })
                //]]>
            </script>
            <h:outputText value="#{manageEventsView.dialogAddress.mapAddress}"/>
            <br/>
            <h:outputText value="#{manageEventsView.dialogAddress.latlng}"/>
            <br/>
            <p:gmap center="#{manageEventsView.dialogAddress.latlng}"
                    model="#{manageEventsView.dialogAddress.markerModel}"
                    zoom="12"
                    type="roadmap"
                    widgetVar="mymap"
                    style="width:550px;height:550px"/>
        </h:panelGroup>
    </p:dialog>
</ui:composition>
Regards,
Brendan.

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 26 guests