Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 只有一个标记存在时,Google Maps信息窗口打开_Javascript_Wordpress_Google Maps_Google Maps Api 3_Advanced Custom Fields - Fatal编程技术网

Javascript 只有一个标记存在时,Google Maps信息窗口打开

Javascript 只有一个标记存在时,Google Maps信息窗口打开,javascript,wordpress,google-maps,google-maps-api-3,advanced-custom-fields,Javascript,Wordpress,Google Maps,Google Maps Api 3,Advanced Custom Fields,我正在使用一个高级自定义字段(ACF)谷歌地图地址,创建一个带有多个标记和单一信息窗口的谷歌地图。然后根据激活的标记交换该信息窗口的内容 PHP$location数组通过ACF设置,javascript循环并运行到Google Maps API 当只有一个标记时,我希望在默认情况下打开信息窗口。 我已经找到了如何确定new\u map函数末尾是否只有一个标记 if($markers.length == 1) { console.log('yes'); } 但我不确定一旦地图已经呈现,调

我正在使用一个高级自定义字段(ACF)谷歌地图地址,创建一个带有多个标记和单一信息窗口的谷歌地图。然后根据激活的标记交换该信息窗口的内容

PHP
$location
数组通过ACF设置,javascript循环并运行到Google Maps API

当只有一个标记时,我希望在默认情况下打开信息窗口。

我已经找到了如何确定
new\u map
函数末尾是否只有一个标记

if($markers.length == 1) {
    console.log('yes');
}
但我不确定一旦地图已经呈现,调用什么/如何访问谷歌地图API

我认为我的问题与范围有关,因为我尝试了
google.maps.event.trigger()
google.maps.Map.event.trigger(标记,'click')我能想到的样式函数

Array
(
    [address] => 123 Hamaker Rd, Manheim, PA, United States
    [lat] => 40.1789636
    [lng] => -76.3852963
)



<script type="text/javascript">
(function($) {

  /*
  *  new_map
  *
  *  This function will render a Google Map onto the selected jQuery element
  *
  *  @type function
  *  @date 8/11/2013
  *  @since   4.3.0
  *
  *  @param   $el (jQuery element)
  *  @return  n/a
  */

  /*
  * Modified from ACF Documentation, here are some links
  * http://support.advancedcustomfields.com/forums/topic/multiple-post-points-on-google-maps/
  * http://fancysquares.com/google-maps-infowindow-advanced-custom-fields-acf/
  */

  function new_map( $el ) {

     // var
     var $markers = $el.find('.marker');

     // vars
     var args = {
        zoom     : 16,
        center      : new google.maps.LatLng(0, 0),
        mapTypeId   : google.maps.MapTypeId.ROADMAP,
        scrollwheel : false,
        mapTypeControl: false,
        streetViewControl: false,
     };

     // create map           
     var map = new google.maps.Map( $el[0], args);

     // add a markers reference
     map.markers = [];

     // add markers
     $markers.each(function(){
        add_marker( $(this), map );
     });

     // center map
     center_map( map );

     /* Conditional works to find if it only has one marker, but I can't figure out what to trigger */
     //console.log($markers);

     if($markers.length == 1) {
        console.log('yes');
     }

  }

  var infowindow = new google.maps.InfoWindow({
     content     : '' 
  });

  /*
  *  add_marker
  *
  *  This function will add a marker to the selected Google Map
  *
  *  @type function
  *  @date 8/11/2013
  *  @since   4.3.0
  *
  *  @param   $marker (jQuery element)
  *  @param   map (Google Map object)
  *  @return  n/a
  */

  function add_marker( $marker, map ) {

     // var
     var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );

     // create marker
     var marker = new google.maps.Marker({
        position : latlng,
        map         : map,
        open     : true
     });

     // add to array
     map.markers.push( marker );

     // if marker contains HTML, add it to an infoWindow
     if( $marker.html() ) {

        // show info window when marker is clicked & close other markers
        google.maps.event.addListener(marker, 'click', function() {
           //swap content of that singular infowindow
           infowindow.setContent($marker.html());
           infowindow.open(map, marker);
        });

        // close info window when map is clicked
        google.maps.event.addListener(map, 'click', function(event) {
           if (infowindow) {
              infowindow.close();
           }
        }); 

     }
  }

  /*
  *  center_map
  *
  *  This function will center the map, showing all markers attached to this map
  *
  *  @type function
  *  @date 8/11/2013
  *  @since   4.3.0
  *
  *  @param   map (Google Map object)
  *  @return  n/a
  */

  function center_map( map ) {

     // vars
     var bounds = new google.maps.LatLngBounds();

     // loop through all markers and create bounds
     $.each( map.markers, function( i, marker ){

        var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );

        bounds.extend( latlng );

     });

     // only 1 marker?
     if( map.markers.length == 1 ) {
        // set center of map
         map.setCenter( bounds.getCenter() );
         map.setZoom( 10 );
     }
     else {
        // fit to bounds
        map.fitBounds( bounds );
     }
  }


  /*  This function will render each map when the document is ready (page has loaded)
  *
  *  @type function
  *  @date 8/11/2013
  *  @since   5.0.0
  *
  *  @param   n/a
  *  @return  n/a
  */
  // global var
  var map = null;

  $(document).ready(function(){
     $('.acf-map').each(function(){
        // create map
        map = new_map( $(this) );
     });

  });

})(jQuery);
</script>
数组
(
[地址]=>美国宾夕法尼亚州曼海姆市哈马克路123号
[lat]=>40.1789636
[lng]=>-76.3852963
)
(函数($){
/*
*新地图
*
*此函数将在所选jQuery元素上呈现Google地图
*
*@type函数
*@date 8/11/2013
*@自4.3.0以来
*
*@param$el(jQuery元素)
*@return不适用
*/
/*
*修改自ACF文档,以下是一些链接
* http://support.advancedcustomfields.com/forums/topic/multiple-post-points-on-google-maps/
* http://fancysquares.com/google-maps-infowindow-advanced-custom-fields-acf/
*/
功能新地图($el){
//变量
var$markers=$el.find('.marker');
//瓦尔斯
变量args={
缩放:16,
中心:新google.maps.LatLng(0,0),
mapTypeId:google.maps.mapTypeId.ROADMAP,
滚轮:错误,
mapTypeControl:false,
街景控制:错误,
};
//创建地图
var map=new google.maps.map($el[0],args);
//添加标记引用
map.markers=[];
//添加标记
$markers.each(函数(){
添加_标记($(此),映射);
});
//中心地图
中心地图(地图);
/*Conditional可以找到它是否只有一个标记,但我不知道该触发什么*/
//console.log($markers);
如果($markers.length==1){
console.log('yes');
}
}
var infowindow=new google.maps.infowindow({
内容:“”
});
/*
*添加标记
*
*此功能将向选定的Google地图添加标记
*
*@type函数
*@date 8/11/2013
*@自4.3.0以来
*
*@param$marker(jQuery元素)
*@param-map(谷歌地图对象)
*@return不适用
*/
函数add_marker($marker,map){
//变量
var latlng=new google.maps.latlng($marker.attr('data-lat'),$marker.attr('data-lng');
//创建标记
var marker=new google.maps.marker({
位置:latlng,
地图:地图,
开放:是的
});
//添加到数组
map.markers.push(marker);
//如果标记包含HTML,则将其添加到信息窗口
如果($marker.html()){
//单击标记时显示信息窗口并关闭其他标记
google.maps.event.addListener(标记'click',函数(){
//交换单一信息窗口的内容
setContent($marker.html());
信息窗口。打开(地图、标记);
});
//单击地图时关闭信息窗口
google.maps.event.addListener(映射,'click',函数(事件){
如果(信息窗口){
infowindow.close();
}
}); 
}
}
/*
*中心地图
*
*此功能将使地图居中,显示附加到此地图的所有标记
*
*@type函数
*@date 8/11/2013
*@自4.3.0以来
*
*@param-map(谷歌地图对象)
*@return不适用
*/
功能中心地图(地图){
//瓦尔斯
var bounds=new google.maps.LatLngBounds();
//循环遍历所有标记并创建边界
$.each(map.markers,function(i,marker){
var latlng=new google.maps.latlng(marker.position.lat(),marker.position.lng());
边界扩展(latlng);
});
//只有一个标记?
如果(map.markers.length==1){
//设置地图中心
map.setCenter(bounds.getCenter());
map.setZoom(10);
}
否则{
//合拍
映射边界(bounds);
}
}
/*此函数将在文档准备就绪(页面已加载)时呈现每个地图
*
*@type函数
*@date 8/11/2013
*@自5.0.0以来
*
*@param不适用
*@return不适用
*/
//全局变量
var-map=null;
$(文档).ready(函数(){
$('.acf map')。每个(函数(){
//创建地图
map=new_map($(this));
});
});
})(jQuery);

在新建映射功能中,您可以在添加标记之前检查数组的长度。如果长度为1,则传递一个参数,指示showInfoWindow为true,否则为false

var showInfoWindow = false;
if($markers.length == 1) {
        showInfoWindow = true
     }

 // add markers
     $markers.each(function(){
        add_marker( $(this), map , showInfoWindow);
     });
然后修改add_标记代码,如下所示

function add_marker( $marker, map, showInfoWindow ) {

 // var
 var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );

 // create marker
 var marker = new google.maps.Marker({
    position : latlng,
    map         : map,
    open     : true
 });

 // add to array
 map.markers.push( marker );

 // if marker contains HTML, add it to an infoWindow
 if( $marker.html() ) {

    // show info window when marker is clicked & close other markers
    google.maps.event.addListener(marker, 'click', function() {
       //swap content of that singular infowindow
       infowindow.setContent($marker.html());
       infowindow.open(map, marker);
    });

    // close info window when map is clicked
    google.maps.event.addListener(map, 'click', function(event) {
       if (infowindow) {
          infowindow.close();
       }
    }); 

  if(showInfoWindow)
  {
       infowindow.setContent($marker.html());
       infowindow.open(map, marker);
  }

 }
}

在new_map函数中,可以在添加标记之前检查数组的长度。如果长度为1,则传递一个参数,指示showInfoWindow为true,否则为false

var showInfoWindow = false;
if($markers.length == 1) {
        showInfoWindow = true
     }

 // add markers
     $markers.each(function(){
        add_marker( $(this), map , showInfoWindow);
     });
然后修改add_标记代码,如下所示

function add_marker( $marker, map, showInfoWindow ) {

 // var
 var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );

 // create marker
 var marker = new google.maps.Marker({
    position : latlng,
    map         : map,
    open     : true
 });

 // add to array
 map.markers.push( marker );

 // if marker contains HTML, add it to an infoWindow
 if( $marker.html() ) {

    // show info window when marker is clicked & close other markers
    google.maps.event.addListener(marker, 'click', function() {
       //swap content of that singular infowindow
       infowindow.setContent($marker.html());
       infowindow.open(map, marker);
    });

    // close info window when map is clicked
    google.maps.event.addListener(map, 'click', function(event) {
       if (infowindow) {
          infowindow.close();
       }
    }); 

  if(showInfoWindow)
  {
       infowindow.setContent($marker.html());
       infowindow.open(map, marker);
  }

 }
}

精彩的解决方案,谢谢!你代码中的最后一个“}”出现在代码框中,我试图编辑,但S.O.给了我一段时间,因为它少于6个字符。没有看到那个家伙逃跑。。他现在又回到密码箱里了-P谢谢你指出这一点。非常好的解决方案,谢谢!你代码中的最后一个“}”出现在代码框中,我试图编辑,但S.O.给了我一段时间,因为它少于6个字符。没有看到那个家伙逃跑。。他现在又回到密码箱里了-P谢谢你指出这一点。