0

我試圖實現這個例子http://jsfiddle.net/gzF6w/1/(從另一個問題在這裏樁)在wordpress插件(metabox上的帖子)。谷歌地圖V3,WordPress的和jQuery

由於某些原因,該框不顯示它應該顯示的地圖。

這裏是我到目前爲止的代碼

// Put the script on the head after jQuery is loaded   
function map_script_one($hook) { 
     wp_deregister_script('googlemapsapi3'); 
     wp_enqueue_script('googlemapsapi3', 'http://maps.google.com/maps/api/js?sensor=false', false, '3', false); 
     wp_enqueue_script('gmap3', get_site_url().'/wp-content/plugins/maps/gmap3-3.4-min.js', array('jquery')); 
     wp_enqueue_script('autocomplete', get_site_url().'/wp-content/plugins/maps/jquery-autocomplete.min.js', array('jquery')); 
    } 

add_action('admin_enqueue_scripts', 'map_script_one'); 


// Include some css and jQuery script for the maps 
function map_script_two() { ?> 
    <link rel="stylesheet" type="text/css" href="'.get_site_url().'/wp-content/plugins/maps/jquery-autocomplete.css"/> 
     <style> 
      .gmap3{ 
      margin: 20px auto; 
      border: 1px dashed #C0C0C0; 
      width: 1000px; 
      height: 500px; 
      } 
      .ui-menu .ui-menu-item{ 
      text-align: left; 
      font-weight: normal; 
      } 
      .ui-menu .ui-menu-item a.ui-state-hover{ 
      border: 1px solid red; 
      background: #FFBFBF; 
      color: black; 
      font-weight:bold; 
      } 
     </style> 

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

       jQuery('#test').gmap3(); 

       jQuery('#address').autocomplete({ 
       source: function() { 
        jQuery("#test").gmap3({ 
        action:'getAddress', 
        address: jQuery(this).val(), 
        callback:function(results){ 
         if (!results) return; 
         jQuery('#address').autocomplete(
         'display', 
         results, 
         false 
        ); 
        } 
        }); 
       }, 
       cb:{ 
        cast: function(item){ 
        return item.formatted_address; 
        }, 
        select: function(item) { 
        jQuery("#test").gmap3(
         {action:'clear', name:'marker'}, 
         {action:'addMarker', 
         latLng:item.geometry.location, 
         map:{center:true} 
         } 
        ); 
        } 
       } 
       }) 
       .focus(); 

      }); 
     </script><?php 
    } 
add_action('admin_head', 'map_script_two'); 

// Create the box  
function set_map_box() { 
    add_meta_box('addressmap', 'Address Map', 'address_map', 'post', 'normal', 'default'); 
    } 
add_action('admin_init','set_map_box'); 


// Content of the box 
function address_map() { 
     global $post; ?> 
     <input type="text" id="address" size="60"> 
    <div id="test" class="gmap3"></div> 
<?php } // more code after. 

到目前爲止,我曾試圖改變「$」將jQuery的腳本,從WordPress的註銷原來的jQuery和使用另一個版本,禁用所有插件和刪除不必要的代碼這個自定義插件,但至今似乎沒有任何工作:(

什麼可以是問題的任何想法?

基本上什麼例子確實是顯示一個框,用戶可以輸入一個地址,地圖會更新到該地址ESS。

回答

1

您可能會在創建#test div之前嘗試繪製地圖。嘗試使用

jQuery(document).ready(function($){ 

map_script_two()功能,延緩地圖的繪製,直到整個文件已加載。

您也可以在#test div之後在您的address_map()函數中包含該jQuery。