Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting / Getting Property Values from within JQuery Plugin
    primarykey
    data
    text
    <p>Greetings.</p> <p>I am trying to modify Shawn Mayzes JQuery Google Maps plugin to support retrieving a "current" latlng property.</p> <p>Shawn's plugin and examples can be found here: <a href="http://www.mayzes.org/googlemaps.jquery.examples.html" rel="nofollow noreferrer">http://www.mayzes.org/googlemaps.jquery.examples.html</a></p> <p>The example on which I am focusing is "Map Geocoding Simple" (<a href="http://www.mayzes.org/fragments/new/geocode.html" rel="nofollow noreferrer">http://www.mayzes.org/fragments/new/geocode.html</a>). The relevant function within the plugin is:</p> <pre><code> 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 &amp; 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); } </code></pre> <p>Now, what I want to do is retrieve the "center point", the lat / long value of the center point, after passing ad address to the plugin.</p> <p>So, in my code when I pass an address, thus:</p> <pre><code>$('#map').googleMaps({ geocode: add }); </code></pre> <p>I would like some means of of retrieving the lat / long like this:</p> <pre><code>$('#map').getLatLng(); </code></pre> <p>...or as a callback function within the plugin instantiation (like they do in the JQuery UI Tabs control).</p> <p>Those would be the "ideal" solutions, but in the meantime, I was willing to "content myself" with a hack. Why not use the Jquery "data" function like this?</p> <pre><code> 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 &amp; 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); } </code></pre> <p>Then I would be able to retrieve the latlng value by simply referencing:</p> <pre><code> $('#map').data('latlng'); </code></pre> <p>But, alas, this does not work at all.</p> <p>Any guidance / hints / solutions to similar problems would be of great assistance.</p> <p>Shameless</p> <p>//////////////////////////////////////////////////////// Response to Matchu ////////////////////////////////////////////////////////</p> <p>Thank you for the idea. Here is what I tried:</p> <pre><code>$.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);... </code></pre> <p>The "var ThisMap = this" is on Line 3 of the plugin, which seems pretty "up high" to me :)</p> <p>I try to retrieve the "latlng" using:</p> <pre><code> var map = $('#map').googleMaps({ geocode: add }); alert('latlng--'+map.data('latlng')); </code></pre> <p>I know that what needs to happen here is possible because I have used similar usage pattersn with other plugins. Unfortunately, those plugins seem to be pretty "advanced" and I have not seen an example of storing and retrieving current property values in any of the simple JQuery Plugin tutorials. Has anyone else?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload