允许具有空格的用户输入(“”)

时间:2015-06-05 00:19:21

标签: google-maps jsf oracle-adf

我正在创建一个程序,该程序获取用户输入的地址,并使用GeoCoder将该地址放在地图上。这是通过oracle adf专题图和java作为支持bean来完成的。我得到的问题是输入工作,但只有在用户的输入中没有空格。当输入的输入中有空格时,我收到此错误:

<oracle.adf.view> <PartialResponseUtils> <handleError> <ADF_FACES-60096:Server Exception during PPR, #1> 
javax.el.ELException: .../map.jsf @76,86 pointX="#{row.lattitude}": java.lang.NullPointerException

由于GeoCoder不关心空格,我猜测错误是在地图代码中?这是地图jsf页面的代码:

<af:form id="f1">
    <af:panelStretchLayout topHeight="50px" id="psl1">
        <f:facet name="top">
            <af:panelHeader text="Regional Map" id="ph1">
                <f:facet name="context"/>
                <f:facet name="menuBar"/>
                <f:facet name="toolbar"/>
                <f:facet name="legend"/>
                <f:facet name="info"/>
            </af:panelHeader>
        </f:facet>
        <f:facet name="center">
            <af:panelSplitter id="ps1" splitterPosition="289">
                <f:facet name="first">
                    <af:decorativeBox id="db1">
                        <f:facet name="center">
                            <af:panelGroupLayout layout="scroll" id="pgl1">
                                <af:panelFormLayout id="pfl1">
                                    <f:facet name="footer">
                                        <af:commandButton text="Add Location" id="cb1" partialSubmit="true"
                                                          actionListener="#{locationsCollector.addCurrentLocation}"/>
                                    </f:facet>
                                    <af:inputText label="Label" id="it1" autoSubmit="true"
                                                  value="#{currentLocation.label}"/>
                                    <af:inputText label="Description" id="it1a" autoSubmit="true"
                                                  value="#{currentLocation.description}"/>
                                    <af:inputText label="Location (NO SPACES)" id="it2" autoSubmit="true"
                                                  value="#{currentLocation.location}"/>
                                </af:panelFormLayout>
                                <af:spacer id="spac1" height="40"/>
                                <af:table value="#{locationsCollector.locations}" var="row"
                                          rowBandingInterval="0" id="t1" partialTriggers="::cb1">
                                    <af:column sortable="false" headerText="Label" align="start" id="c1">
                                        <af:outputText value="#{row.label}" id="ot1"
                                                       shortDesc="#{row.description}"/>
                                    </af:column>
                                    <af:column sortable="false" headerText="Description" align="start" id="c2">
                                        <af:outputText value="#{row.description}" id="ot2"/>
                                    </af:column>
                                    <!--<af:column sortable="false" headerText="Country" align="start" id="c3">
                                        <af:outputText value="#{row.country}" id="ot3"/>
                                    </af:column>-->
                                </af:table>
                            </af:panelGroupLayout>
                        </f:facet>
                        <!--<f:facet name="top">
                            <af:panelHeader text="Enter location details" id="ph2">
                                <f:facet name="context"/>
                                <f:facet name="menuBar"/>
                                <f:facet name="toolbar"/>
                                <f:facet name="legend"/>
                                <f:facet name="info"/>
                            </af:panelHeader>
                        </f:facet>-->
                    </af:decorativeBox>
                </f:facet>
                <f:facet name="second">
                    <dvt:thematicMap basemap="usa" id="tm1" partialTriggers="::cb1" summary="map">
                        <dvt:areaLayer layer="states" id="al1" rendered="true">
                            <dvt:pointDataLayer id="pdl1c" value="#{locationsCollector.locations}" var="row">
                                <dvt:pointLocation id="pl1c" type="pointXY" pointX="#{row.lattitude}"
                                                   pointY="#{row.longitude}">
                                    <dvt:marker id="m1c" labelDisplay="on" value="#{row.label}"
                                                labelPosition="top"
                                                shortDesc="#{row.description} #{row.location} "/>
                                </dvt:pointLocation>
                            </dvt:pointDataLayer>
                        </dvt:areaLayer>
                    </dvt:thematicMap>
                </f:facet>
            </af:panelSplitter>
            <!-- id="af_one_column_header_stretched"  -->
        </f:facet>
    </af:panelStretchLayout>
</af:form>

以下是从Geocoder获取纬度和经度以添加到地图的点细节的bean:

public class Location {

    private String location;
    private String country;
    private String label;
    private String description;
    private float[] coordinates;

    private static float[] getCoordinatesForLocation(String location) {
        URL geoCodeUrl;

        String url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + location
                + "&oe=utf8&sensor=false";
        try {
            geoCodeUrl
                    = new URL(url);
        } catch (MalformedURLException e) {
            System.out.println(e.getMessage() + " url=" + url);
            return null;
        }
        BufferedReader in;
        String coord = null;
        try {

            in = new BufferedReader(new InputStreamReader(geoCodeUrl.openStream()));
            char[] buf = new char[8000];

            in.read(buf);
            coord = new StringBuilder().append(buf).toString();

            in.close();
        } catch (IOException e) {
            System.out.println(e.getMessage() + " IO Exception ");
            return null;
        }

        if (coord != null) {
            float[] coordinates;
            try {
                // find first occurrence of lat
                int posLAT = coord.indexOf("\"lat\"");
                String latString = coord.substring(posLAT, posLAT + 21);

                String lat = latString.split(":")[1].replaceAll(" ", "").replaceAll(",", "");
                // find first occurrence of lng
                int posLNG = coord.indexOf("\"lng\"");
                String lngString = coord.substring(posLNG, posLNG + 21);
                String lng = lngString.split(":")[1].replaceAll(" ", "").replaceAll(",", "");

                coordinates
                        = new float[]{Float.parseFloat(lat), Float.parseFloat(lng)};
                return coordinates;
            } catch (Exception e) {
                System.out.println("Coordinates stank " + coord);
            }
        }
        System.out.println("Failed to create proper coordinates; sorry!");
        return null;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getLocation() {
        return location;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCountry() {
        return country;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getLabel() {
        return label;
    }

    public void setCoordinates(float[] coordinates) {
        this.coordinates = coordinates;
    }

    public float[] getCoordinates() {
        if (coordinates == null) {
            coordinates = getCoordinatesForLocation(location);
        }
        return coordinates;
    }

    public float getLongitude() {
        return getCoordinates()[0];
    }

    public float getLattitude() {
        return getCoordinates()[1];
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }
}

我正在寻找的是程序接受带空格的地址的能力,因此程序更加用户友好。谢谢你看这个!

0 个答案:

没有答案