2014-01-09 73 views
0

我的插件使用Google Maps API在用户在输入字段中键入几个字母时自动建议位置。冲突的Google地图API

要做到这一点,我在排队谷歌地图API如下:

wp_register_script('auto_location_map_library', 'http://maps.googleapis.com/maps/api/js?key=' . get_option('auto_location_maps_key') . '&sensor=false&libraries=places', array('jquery'), '1.0', false); 
wp_enqueue_script('auto_location_map_library'); 

一切工作正常,我得到自动的建议。但是,只要在已经包含Google地图库的主题上进行测试,就会完全崩溃。

我试过在我的插件中插入主题的库调用,但是这并没有工作,并不是一个很好的解决方案。

有没有一种类似于jQuery中使用的我不知道的noConflict Google Maps调用?

由于利益的问题,对于引用自我暗示的代码如下:

jQuery(document).ready(function() { 
    function initialize() { 
     var autocompleteOptions = { 
      <?php if (!(get_option('auto_location_countrycode') == '')) { ?> 
       componentRestrictions: {country: '<?php echo get_option('auto_location_countrycode'); ?>'}, 
      <?php } ?> 
     }; 

     if (jQuery('#search_location').length) { 
      var input = document.getElementById('search_location'); 
      var autocomplete = new google.maps.places.Autocomplete(input, autocompleteOptions); 
     }   
    } 

    google.maps.event.addDomListener(window, 'load', initialize); 
}); 
+0

看看你在运行谷歌地图时发现的萤火虫? –

+0

@Mahmood - 在Chrome控制台中,我收到“警告:您已在此页面上多次包含Google Maps API,这可能会导致意外错误。” –

+0

删除后面的'google API'并尝试。 –

回答

0

我遇到了你与my plugin同样的问题。

尝试以下操作:

add_action('wp_enqueue_scripts', 'randomfunction234_deregister_scripts',999); 
add_action('wp_head', 'randomfunction234_deregister_scripts',999); 
add_action('init', 'randomfunction234_deregister_scripts',999); 
add_action('wp_footer', 'randomfunction234_deregister_scripts',999); 
add_action('wp_print_scripts', 'randomfunction234_deregister_scripts',999); 

function randomfunction234_deregister_scripts() { 


     $map_handle = ''; 
     global $wp_scripts; 
     if (isset($wp_scripts->registered) && is_array($wp_scripts->registered)) { 
      foreach ($wp_scripts->registered as $script) {    
       if ($script->handle !== 'YOUR_MAPS_API_ENQUEUE_HANDLE'){ 
        if (strpos($script->src, 'maps.google.com/maps/api/js') !== false || strpos($script->src, 'maps.googleapis.com/maps/api') !== false || strpos($script->src, 'maps.googleapis') !== false || strpos($script->src, 'maps.google') !== false) { 
         if (!isset($script->handle) || $script->handle == '') { 
          $script->handle = 'remove-this-map-call'; 
         } 
         unset($script->src); 
         $map_handle = $script->handle; 
         if ($map_handle != '') { 
          $wp_scripts->remove($map_handle); 
          $map_handle = ''; 
          break; 
         } 
        } 
       } 
      } 
     } 

} 

确保您更改YOUR_MAPS_API_ENQUEUE_HANDLE到您的排队手柄的名字,当你排队的谷歌地图API,而你的情况是auto_location_map_library

这是什么脚本它尝试在wp_enqueue_scriptswp_headinitwp_footerwp_print_scripts挂钩注销任何排队的脚本。

警告:这不会注销任何已被其他插件或主题硬编码的脚本 - 这些将永远是我们双方的痛苦。