JSF Primefaces Gmap将标记添加到现有的谷歌地图

时间:2016-10-26 16:55:16

标签: google-maps jsf primefaces google-maps-markers

我正在使用primefaces gmap来显示地图。有一个inputText框,它接受一个字符串并在地图中心纬度的某个半径内重新输入一个“地点”列表,长(使用Facebook的图形搜索)并填充一个selectonelistbox。这可以按预期工作。

我的问题是,我无法将标记丢弃并叠加在地图上。我尝试在这个例子的最后几行设置标记。

通常我想在现有的谷歌地图上添加标记。 lat,lngs是在selectonelistbox

中显示的对象的一部分

CreatePoint.xhtml

<ui:define name="googleMapsSrc">
    <script
        src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCMb2mTOPGgQRS8TlxMKY_yDnhX9Mfh0Vw"
        type="text/javascript"></script>
</ui:define>


<ui:define name="content">              
    <!--the google map-->
    <h:form styleClass="simpleformstyle" id="gmapForm">
        <p:growl id="messages" showDetail="true" life="1000" />

        <p:gmap id="gmapp" widgetVar="w_gmap" type="hybrid" style="width:50%; height:400px; float:left" model="#{mapBean.model}" >
            <p:ajax event="stateChange" listener="#{mapBean.onStateChange}" update="messages,:latlngGrid" />
            <f:param name="idUser" value="#{mapBean.tipTourUser.idUser}" />
        </p:gmap>
    </h:form>


    <!--the list box results from the search-->
    <h:form id="newPointsResultList" styleClass="simpleformstyle">
        <p:selectOneListbox id="selectedPoints_listbox" 
                            value="#{mapBean.selectedPoint}" 
                            converter="omnifaces.SelectItemsConverter" 
                            scrollHeight="395" 
                            styleClass="simpleformstyle10" 
                            rendered="#{not empty mapBean.newPointsHashMap}">

            <f:selectItems value="#{mapBean.newPointsHashMap}" />

            <p:ajax listener="#{mapBean.valueChanged}" update=":newPointsGrid:selectedPoint_grid" process="@this" />
            <f:param name="idUser" value="#{mapBean.tipTourUser.idUser}" />
        </p:selectOneListbox>
    </h:form>
    <br />


    <!--the input text search box-->
    <h:form id="placesSearchForm" styleClass="simpleformstyle" style="clear:both">
        <p:inputText id="placesSearch" 
                     value="#{mapBean.searchString}"
                     a:placeholder="Search places"
                     onkeypress="if (event.keyCode == 13) { onchange(); return false; }">

            <p:ajax event="change" update="placesSearchForm,:newPointsResultList" listener="#{mapBean.findPlaces}" />
            <f:param name="idUser" value="#{mapBean.tipTourUser.idUser}" />
        </p:inputText>
    </h:form>
</ui:define>

MapBean.java

@ViewScoped
@Named
public class MapBean implements Serializable
{
    private MapModel model = new DefaultMapModel();     

    private NewPoint selectedPoint;
    private Point point = new Point();
    private Map<String, NewPoint> newPointsHashMap = new HashMap<String, NewPoint>();

    private User tipTourUser;

    public void findPlaces()  {

        newPointsHashMap.clear();

        String facebookSearhString = "https://graph.facebook.com/search?type=place&q=" + 
                                         URLEncoder.encode(searchString, "UTF-8") + 
                                         "&center=" + lat + "," + lng + 
                                         "&distance=50000&access_token=...";

        createJsonString(facebookSearhString);

        createNewPointsFromJson();

    } 


    private void createJsonString(String s) {

        ...
    }


    private void createNewPointsFromJson() {

        newPointsHashMap.clear();
        //points.clear();

        newPointsHashMap = new HashMap<String, NewPoint>();

        try {
            JSONObject jsonObj = new JSONObject(url);
            JSONArray getArray = jsonObj.getJSONArray("data");

            for (int i = 0; i < getArray.length(); i++) {

                JSONObject c = getArray.getJSONObject(i);               

                String name = c.getString("name");
                String id   = c.getString("id");                            

                JSONObject location = c.getJSONObject("location");

                String latitude = location.getString("latitude");
                String longitude = location.getString("longitude");          

                String street = "", zipCode = "";

                if(location.optString("street") != null)
                    street = location.optString("street");

                if(location.optString("zip") != null)
                    zipCode = location.optString("zip");

                if(name.length() > 49){
                    name = name.substring(0, 45);
                }

                if(street.length() > 40){
                    street = street.substring(0, 40);
                }

                street = street + " " + zipCode;

                NewPoint newPoint = new NewPoint(name, id, latitude, longitude, street);

                if(!newPointsHashMap.containsKey(name)) {
                    newPointsHashMap.put(newPoint.title, newPoint);
                }

                markers += latitude + ",,," + longitude + ",,," + name + ":";               

                //DROPPING MARKERS NOT WORKING!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
                LatLng cordinates = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
                model.addOverlay(new Marker(cordinates, name));     

                //System.out.println(newPoint);
            }       
        }

        catch (JSONException e) {
            e.printStackTrace();
        }
    }

0 个答案:

没有答案