我想在lat和long值更改时仅更新地图上的纬度和经度。但是现在我已经尝试使用按钮实现这一点,但这也不起作用。我不想更新整个地图,我只想更新地图中的lat long值,无论何时更改或新值来自代码。但是现在即使按钮代码也无效。
JSF页面和ManagedBean代码如下:
<?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">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</h:head>
<h:body>
Form1 hello
<h:form id="form1">
<p:inputText id="lat" value="#{mapBean.lat}"> </p:inputText>
<p:inputText id="lng" value="#{mapBean.lng}"> </p:inputText>
<p:commandButton update="gmp" value="button" actionListener="#{mapBean.funx}"></p:commandButton>
<p:gmap id="gmp" center="#{mapBean.geo}" zoom="15" type="TERRAIN" style="width:600px;height:400px" model="#{mapBean.advancedModel}" >
</p:gmap>
</h:form>
</h:body>
</html>
public class MapBean {
/**
* Creates a new instance of MapBean
*/
private final MapModel advancedModel;
private String geo;
private Double lat;
private Double lng;
private StringBuilder builder;
public MapBean() {
geo = "33.572571, 73.055555";
advancedModel = new DefaultMapModel();
//Shared coordinates 33.572571, 73.041444
//33.576021,73.044491
lat = 33.576021;
lng = 73.044491;
LatLng coord1 = new LatLng(lat, lng);
//Icons and Data
advancedModel.addOverlay(new Marker(coord1, "Rawalpindi", null, "https://storage.googleapis.com/support-kms-prod/SNP_2752125_en_v0"));
// for (int i = 0; i < advancedModel.getMarkers().size(); i++) {
// RequestContext.getCurrentInstance().addCallbackParam("marker" + i, advancedModel.getMarkers().get(i));
// }
}
public MapModel getAdvancedModel() {
return advancedModel;
}
public String getGeo() {
return geo;
}
public void setGeo(String geo) {
this.geo = geo;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLng() {
return lng;
}
public void setLng(Double lng) {
this.lng = lng;
}
public void funx(ActionEvent e) {
System.out.println("ajax funnx");
builder.append("\"").append(lat).append(",").append(lng).append("\"");
geo = builder.toString();
System.out.println("Latitude" + lat + "Longitutde" + lng);
LatLng coord1 = new LatLng(lat, lng);
//Icons and Data
advancedModel.addOverlay(new Marker(coord1, "Rawalpindi", null, "https://storage.googleapis.com/support-kms-prod/SNP_2752125_en_v0"));
}
}
错误:
WARNING: /index.xhtml @16,91 actionListener="#{mapBean.funx}": java.lang.NullPointerException
javax.el.ELException: /index.xhtml @16,91 actionListener="#{mapBean.funx}": java.lang.NullPointerException
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:111)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:147)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:818)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
at MapBean.MapBean.funx(MapBean.java:78)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.el.parser.AstValue.invoke(AstValue.java:275)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
... 37 more
答案 0 :(得分:0)
对于您当前的问题,您需要在构造函数中实例化构建器
public MapBean() {
geo = "33.572571, 73.055555";
builder = new StringBuilder();
...
}