返回顶部

收藏

html5 geolocation api

更多
// CLASS
var Geolocation = Class.extend({
    init : function(options){
        var on = {
            success : options.success||function(){},
            error : options.error||function(){},
            set : options.set||{}
        };

        navigator.geolocation.watchPosition(on.success, on.error, on.set);
    }
});

// USE
var geolocation = new Geolocation({
    set : {
        enableHighAccuracy : true,
        maximumAge : 8000 // 8 sec.
    },

    success : function(geo){
        var coords = geo.coords;
        var nd = "brak informacji";

        get("geo_latitude", coords.latitude||nd);
        get("geo_longitude", coords.longitude||nd);
        get("geo_speed", coords.speed||nd);
        get("geo_altitude", coords.altitudeAccuracy||nd);
        get("geo_timestamp", geo.timestamp||nd);
    },

    error : function(error){
        switch(error.code){
            case error.PERMISSION_DENIED:
                alert("user did not share geolocation data");
                break;

            case error.POSITION_UNAVAILABLE:
                alert("could not detect current position");
                break;

            case error.TIMEOUT:
                alert("retrieving position timedout");
                break;

            default:
                alert("unknown error");
        }
    }
});

标签:html5

收藏

0人收藏

支持

0

反对

0