5

I have a problem with Primefaces whose version is 3.0.M3 . I used gmap and prime p:ajax.I wanted to do something is to get clicked Marker when a marker is clicked by a user on Gmap.

Here is the Code

<p:tabView effect="slide" effectDuration="normal" style="width:1050px;height:450px" >
            <p:tab title="blabla">
                <h:panelGrid colums="1">    
                </h:panelGrid>
            </p:tab>
            <p:tab title="blabla" >
                <h:panelGrid colums="1">
                    <h:form id="mapId">

                        <p:gmap  id="asd" center="39.000409,35.201554" 
                            zoom="#{mapBean.modelMap.zoomLevel}" 
                            type="ROADMAP" 
                            style="width:1000px;height:400px"
                            model="#{mapBean.emptyModel}"
                            widgetVar="map" >

                            <p:ajax event="overlaySelect" listener="#{mapBean.onMarkerSelect}"/>

                        </p:gmap>

                    </h:form>   
                </h:panelGrid>
            </p:tab></p:tabView>

and My ManagedBean

public void onMarkerSelect(OverlaySelectEvent event) {  

    Marker marker = (Marker) event.getOverlay();
    if (marker!=null) {
        System.out.println(marker.getId());
    }
    System.out.println("Clicked");
    modelMap.setZoomLevel(modelMap.getZoomLevel()+1);

}

Managed Bean Declarations

@ManagedBean(name="mapBean")
@RequestScoped
public class MapBean implements Serializable 

I am taking NullPointerException in onMarkerSelect method.(event.getOverlay();)

Simeon
  • 7,582
  • 15
  • 64
  • 101
Cetin Imre
  • 331
  • 1
  • 8
  • 15

5 Answers5

6

Fixed my problem.

The problem was that when the MapModel was created it was a local var:

public MapModel getModel() {

    final MapModel mapModel = new DefaultMapModel(); // this should be a field

    final Set<MapEventDto> events = service.loadEvents();
    for (MapEventDto event : events) {

        final double latitude = event.getLatitude().doubleValue();
        final double longitude = event.getLongitude().doubleValue();
        final String magnitude = event.getMagnitude().toString();

        final String title = "Id: " + event.getId() + ", Lat: " + latitude + ", Lng: " + longitude + ", Mag: " + magnitude;

        mapModel.addOverlay(new Marker(new LatLng(latitude, longitude), title));

    }
    return mapModel;
}

The whole mapModel could be garbage collected after the map is rendered (as it is no longer needed). So when the overlay event is called there would be no mapModel any more.

As soon as I made mapModel a field of the Bean the problem disappeared.

Simeon
  • 7,582
  • 15
  • 64
  • 101
  • If someone else could also test this I'd be extremely grateful. – Simeon Jun 06 '12 at 09:02
  • 2
    The general rule in JSF is that getters/setters should **not** do anything else than doing *just* the job they're named for: getting and setting a property. Preparing the data should be done in the (post)constructor or a (action)listener method or at highest lazy loading in the getter. See also http://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times – BalusC Jun 06 '12 at 12:42
3

Use the static field as follows. It worked for me.

final static MapModel mapModel = new DefaultMapModel();
888
  • 3,246
  • 8
  • 41
  • 60
gayan
  • 39
  • 1
  • As a rule of thumb, static fields should not represent state, as you pave the way for a lot of head aches. – Simeon Mar 11 '13 at 13:40
0
final DefaultMapModel mapModel = new DefaultMapModel();

worked For as Global Variable,in viewScoped best vj

Steve P.
  • 14,489
  • 8
  • 42
  • 72
0

Got through this and solved by changing scope for the Bean to ViewScoped instead of Request Scope, so it lasts during user activity.

No final/static declarators needed

  @ManagedBean(name="mapBean")
  @ViewScoped
  public class MapBean implements Serializable {  
    private MapModel draggableModel;   
    ...
  }
perezale
  • 21
  • 4
0

Just change your bean scope to @SessionScoped or @ViewScoped so your bean won't be initialized for each request.

gvieira
  • 21
  • 5