mmarcon / jhere

jHERE - Maps made easy
MIT License
255 stars 51 forks source link

Map Status Getters #10

Closed thingsinjars closed 11 years ago

thingsinjars commented 11 years ago
$('#mapInstance').jHERE('center')

$('#mapInstance').jHERE('boundingBox')

... and so on.

mmarcon commented 11 years ago

Can't be done with the current structure of the plugin. A call to $('#mapInstance').jHERE('anything', anyargument) always returns the jQuery object for chainability.

See, there was a good reason for that functionality not being present. I need to think more about that.

thingsinjars commented 11 years ago

How about directly extending the jQuery object returned?

var c = $('.#map').jHERE().center;

In the same style as .length returns the size of the jquery set.

mmarcon commented 11 years ago

The thing is also that everything that happens with JSLA is asynchronous, as the code is executed within the done callback of a promise that is resolved only when JSLA is loaded. So by the time the center method determines the center of the map, the plugin has already returned. It's not as easy as it looked...

It's always like that, I do very clever and elegant things and then I find myself cursing when I try to solve the issues you post.

mmarcon commented 11 years ago

I might have an idea that comes from your latest suggestion. I can just add the center property to the instance of the jQuery object, but I need to find a way to do that and not touch the jQuery prototype at the same time.

mmarcon commented 11 years ago

Ok, got this type of API committed but not pushed, I will push tomorrow if I don't come up with a better idea:

$(window).on('load', function(){
    var map = $('#map');
    //Set default credentials
    $.jHERE.defaultCredentials('_peU-uCkp-j8ovkzFGNU', 'gBoUkAMoxoqIWfxWA5DuMQ');
    map.jHERE({center: {latitude: 52.49, longitude: 13.39}});
    //At this point the map is not yet initialized, so this should console.log
    //an object with the center property === undefined
    console.log(map.jHERE());

    $('.center').on('click', function(){
        var properties = map.jHERE();
        alert('LAT: ' + properties.center.latitude + ' LON: ' + properties.center.longitude);
    });

    $('.zoom').on('click', function(){
        var properties = map.jHERE();
        alert('ZOOM LEVEL: ' + properties.zoom);
    });

});
mmarcon commented 11 years ago

Almost there. I will merge into master tomorrow.

mmarcon commented 11 years ago

Pushed, check the docs here: http://jhere.net/docs.html (Inspect map properties)