1

当我搜索一个地方时,地图会更新。但中心值不会更新。任何帮助都会很棒。

/* 下面给出了 xhtml */

<h:outputLabel value="Search Location :" for="acSimple" />
<p:autoComplete id="acSimple" minQueryLength="0" value="#{sampleMapProducer.searchString}" forceSelection="true" completeMethod="#{sampleMapProducer.complete}">
    <p:ajax event="itemSelect" update="gmap_loc" process="#{sampleMapProducer.searchCenter}" />
    <p:ajax process="@this" update="gmap_loc" />
</p:autoComplete>
<p:outputPanel id="gmapPanel" autoUpdate="true">
    <p:gmap center="#{sampleMapProducer.searchCenter}" zoom="6" type="ROADMAP" style="width:100%;height:600px" model="#{sampleMapProducer.mapBySearch}" id="gmap_loc"></p:gmap>
</p:outputPanel>

地图的 Bean 代码:

private void clearMap (MapModel map)
{
    for (Marker marker : map.getMarkers()) {
        marker.setVisible(false);
    }
}

/* 获取地图进行搜索 */

 public MapModel getMapBySearch () 
 {
    clearMap(mapBySearch);
    for (Marker markerIterator : markersList)
    {
        if (searchString == null || searchString.trim().equals(""))
        {
            mapBySearch.addOverlay(markerForLocation(markerIterator,BLUE_DOT));
        }
        else
        {
          if (markerIterator.getTitle().toLowerCase().contains(searchString.toLowerCase()))
            {
                mapBySearch.addOverlay(markerForLocation(markerIterator,BLUE_DOT));
            }
        }
    }
    return mapBySearch;
}

/* 自动完成字符串 */

public List<String> complete ( String searchString) 
{
    List<String> suggestedStrings = new ArrayList<String>();
    for (String locationName : LOCATION_NAMES)
    {
        if (locationName.toLowerCase().contains(searchString.toLowerCase())) {
            suggestedStrings.add(locationName);
        }
    }
    this.searchString = searchString;
    return suggestedStrings;
}

/* 获取位置中心 */

public String getLocationCenter () 
{ 
    String center = "39,-98"; 
    for (LocationGeocodeBean geoCode : GEO_CODES) 
    { 
        if (geoCode.getId().equals(selectedLocationId())) 
        { 
            if ((geoCode.getLatitude() != 0) && (geoCode.getLongitude() != 0)) 
            { 
                center = String.format("%s,%s", geoCode.getLatitude(),geoCode.getLongitude()); } 
                return center; 
            } 
        } 

    }
    return center; 
}       
4

1 回答 1

0

好吧,我不完全理解您的代码,所以我创建了一个简单的示例。它会在地图上放置一些标记,当您单击地图时,地图会以标记为中心。您可以使用回调参数。因此,当您单击或选择表单中的某些内容时,您会发回一个参数并在 JavaScript 中捕获它,然后更新您的地图。试试下面的例子:

豆:

public class MapBean
{
    private MapModel advancedModel;

    public MapBean()
    {   
    advancedModel = new DefaultMapModel();

    LatLng coord1 = new LatLng(36.883707, 30.689216);
    LatLng coord2 = new LatLng(36.879703, 30.706707);
    LatLng coord3 = new LatLng(36.885233, 30.702323);

    advancedModel.addOverlay(new Marker(coord1));
    advancedModel.addOverlay(new Marker(coord2));
    advancedModel.addOverlay(new Marker(coord3));

    }

    public MapModel getAdvancedModel()
    {

    return advancedModel;
    }

    public void onMarkerSelect(OverlaySelectEvent event)
    {   
    Marker marker = (Marker) event.getOverlay();
    RequestContext context = RequestContext.getCurrentInstance();        
        context.addCallbackParam("lat", marker.getLatlng().getLat()); 
        context.addCallbackParam("lng", marker.getLatlng().getLng());        
    }

}

和 .xhtml

<h:form id="form">
    <p:gmap center="36.890257,30.707417" zoom="13" type="HYBRID" model="#{mapBean.advancedModel}" style="width:600px;height:400px" id="maps" widgetVar="map" onPointClick="pointClick(event)">
        <p:ajax event="overlaySelect" listener="#{mapBean.onMarkerSelect}" oncomplete="handleComplete(xhr, status, args)"/>
    </p:gmap>
    <script type="text/javascript">  
        function handleComplete(xhr, status, args) 
        {  
            //Create center point from callback params
            var latlng = new google.maps.LatLng(args.lat , args.lng);
            //Get de google map from your widget now you can use the googlemap api
            var gmap = map.getMap();
            gmap.setCenter(latlng);                   
        }   
    </script>  
</h:form>
于 2013-09-11T06:46:03.083 回答