简体   繁体   English

PrimeFaces gmap组件中的fitBounds

[英]fitBounds in PrimeFaces gmap component

Is there a way to call fitBounds method in the google map object created by PrimeFaces gmap component ? 有没有一种方法可以在PrimeFaces gmap组件创建的google map对象中调用fitBounds方法? I tried the code below, but get javascript error: TypeError: primefacesMap.fitBounds is not a function . 我尝试了下面的代码,但遇到javascript错误: TypeError: primefacesMap.fitBounds is not a function It appears that fitBounds is overriden by a boolean property by PrimeFaces framework. 似乎fitBounds被PrimeFaces框架的布尔属性覆盖。

<h:head>
  <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"/>
  <script type="text/javascript">
//<![CDATA[
  function initPrimefacesComponent(){
    var primefacesMap = gmtls.getMap();
    var bounds = new google.maps.LatLngBounds();
    var points = [
      new google.maps.LatLng(41.3, 2),
      new google.maps.LatLng(41.309, 2)
      ];
    // Extend bounds with each point
    for (var i = 0; i < points.length; i++) {
      bounds.extend(points[i]);
      new google.maps.Marker({position: points[i], map: primefacesMap});
    }
    // Apply fitBounds
    primefacesMap.fitBounds(bounds); 
  }
//]]>
  </script>
</h:head>

<h:body onload="initPrimefacesComponent();" >
  <p:gmap center="41.3, 2" zoom="15" type="ROADMAP"   widgetVar="gmtls"
    style="width:600px;height:400px" />  
</h:body>

Below is the code that works for google map object that is created without primefaces: 以下是适用于创建的没有质数的google map对象的代码:

<h:head>
  <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"/>
  <script type="text/javascript">
//<![CDATA[
  function initOriginalMap() {
    var mapOptions = {
      center: new google.maps.LatLng(41.3, 2),
      zoom: 15
    };
    var originalMap = new google.maps.Map(document.getElementById("map-canvas"),
        mapOptions);
    var bounds = new google.maps.LatLngBounds();
    var points = [
      new google.maps.LatLng(41.3, 2),
      new google.maps.LatLng(41.309, 2)
      ];

    // Extend bounds with each point
    for (var i = 0; i < points.length; i++) {
      bounds.extend(points[i]);
      new google.maps.Marker({position: points[i], map: originalMap});
    }

    // Apply fitBounds
    originalMap.fitBounds(bounds); 
  }
  google.maps.event.addDomListener(window, 'load', initOriginalMap);
//]]>
  </script>
</h:head>

<h:body >
  <div id="map-canvas" style="width:600px;height:400px"/>
</h:body>

To make fitBounds work in PrimeFaces I had to override again the fitBounds property of the map by original source: 为了使fitBounds在PrimeFaces fitBounds工作,我不得不通过原始来源再次覆盖地图的fitBounds属性:

//remember the property set by PrimeFaces
var tmp = map.fitBounds;

//replace the code by the one provided by google maps api
map.fitBounds = google.maps.Map.prototype.fitBounds;

//execute fitBounds function
this.fitBounds(bounds);

//restore PrimeFaces property (probably PrimeFaces use it somehow)
this.fitBounds = tmp;

I submitted a PR to have this fixed natively in PF 7.1+. 我提交了一份PR,以在PF 7.1+中进行本地修复。 The fitBounds method will now be right on the widget itself. 现在,fitBounds方法将直接在小部件本身上。

https://github.com/primefaces/primefaces/pull/5132 https://github.com/primefaces/primefaces/pull/5132

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM