2010-01-13 55 views
0

問候。設置/從JQuery插件獲取屬性值

我在嘗試修改Shawn Mayzes JQuery Google Maps插件以支持檢索「當前」latlng屬性。

肖恩的插件和例子可以在這裏找到:http://www.mayzes.org/googlemaps.jquery.examples.html

上我聚焦的例子是「地圖地理編碼簡單」(http://www.mayzes.org/fragments/new/geocode.html)。插件中的相關功能是:

if (opts.geocode) { 
     geocoder = new GClientGeocoder(); 
     geocoder.getLatLng(opts.geocode, function(center) { 
      if (!center) { 
       alert(address + " not found"); 
      } 
      else { 
      $.googleMaps.gMap.setCenter(center, opts.depth); 
       $.googleMaps.latitude = center.x; 
       $.googleMaps.longitude = center.y; 
      } 
    }); 
    } 
    else { 
     // Latitude & Longitude Center Point 
     var center = $.googleMaps.mapLatLong(opts.latitude, opts.longitude); 
     // Set the center of the Map with the new Center Point and Depth 
     $.googleMaps.gMap.setCenter(center, opts.depth); 
    } 

現在,我要做的是找回了「中心點」,中心點的緯度/長值,通過廣告地址插件後。

所以,在我的代碼時,我傳遞一個地址,這樣的:

$('#map').googleMaps({ 
    geocode: add 
}); 

我想了一些手段獲取的緯度/長這樣的:

$('#map').getLatLng(); 

...或作爲插件實例化中的回調函數(就像他們在JQuery UI Tabs控件中那樣)。

這些將是「理想」的解決方案,但在此期間,我願意「的內容自己」有黑客。爲什麼不使用這樣的Jquery「數據」函數?

if (opts.geocode) { 
     geocoder = new GClientGeocoder(); 
     geocoder.getLatLng(opts.geocode, function(center) { 
      if (!center) { 
       alert(address + " not found"); 
      } 
      else { 

      alert('center--'+center); 
      **$(this).data('latlng', center);** 

      $.googleMaps.gMap.setCenter(center, opts.depth); 
       $.googleMaps.latitude = center.x; 
       $.googleMaps.longitude = center.y; 
      } 
    }); 
    } 
    else { 
     // Latitude & Longitude Center Point 
     var center = $.googleMaps.mapLatLong(opts.latitude, opts.longitude); 
     // Set the center of the Map with the new Center Point and Depth 
     $.googleMaps.gMap.setCenter(center, opts.depth); 
    } 

然後,我將能夠獲取經緯度值,通過簡單地參照:

$('#map').data('latlng'); 

但是,唉,這並不在所有的工作。

對類似問題的任何指導/提示/解決方案都會有很大的幫助。

無恥

///////////////////////////////////////// ////////////// 對Matchu //////////////////////////////的回覆//////////////////////////

謝謝你的想法。這裏是我的嘗試:

$.googleMaps = { 
    mapsConfiguration: function(opts) { 
    var ThisMap = this; 
     // GEOCODE 
     if (opts.geocode) { 
      geocoder = new GClientGeocoder(); 
      geocoder.getLatLng(opts.geocode, function(center) { 
       if (!center) { 
        alert(address + " not found"); 
       } 
       else { 

       alert('center--'+center); 
       ThisMap.data('latlng', center); 

        $.googleMaps.gMap.setCenter(center, opts.depth);... 

的「VAR ThisMap =這」是對插件的3號線,這似乎很「高處」給我:)

我嘗試找回「經緯度「使用:

var map = $('#map').googleMaps({ 
     geocode: add 
    }); 

alert('latlng--'+map.data('latlng')); 

我知道什麼需要在這裏發生可能是因爲我曾經用過類似的使用pattersn與其他插件。不幸的是,這些插件看起來非常「先進」,我還沒有看到在任何簡單的JQuery插件教程中存儲和檢索當前屬性值的示例。有其他人嗎?

+0

試圖通過鍵訪問數據如下: \t變種地圖= $( '#地圖')GOOGLEMAPS({ \t \t地理編碼:添加 \t}); alert('latlng - '+ map.data('latlng')); ...並且這也不起作用。 – shamelesshacker 2010-01-14 00:57:42

回答

0

我的第一個猜測是,this並不是指元素了。沒有看到整個代碼,我的猜測是this重置在該片段的第3行開始的匿名函數中。試試看存儲上面一個變量:

if (opts.geocode) { 
    geocoder = new GClientGeocoder(); 
    var element = this; // really you should move the var declaration up higher 
    geocoder.getLatLng(opts.geocode, function(center) { 
     // ... 
    }); 
} 

當然,即使在那裏,$(this)可能仍引用到別的東西,因爲我還沒有在整個插件不見了。嘗試記錄(alert,console.log,無論什麼)如果您不確定,存儲在this中。 。