Javascript 对象/地理位置 API

Posted

技术标签:

【中文标题】Javascript 对象/地理位置 API【英文标题】:Javascript Objects / Geolocation API 【发布时间】:2012-04-12 05:55:25 【问题描述】:

我正在使用 Phonegap 开发一个使用 Geolocation 的移动应用程序。我之前使用过 Geolocation,但这次我认为为它创建一个新的对象包装器会更好,因为大部分功能都基于此,并且不想以混乱的代码结束。

解决方案可能是直截了当的,但它打败了我,因为我从来没有在 JS 中使用对象做过任何非常高级的事情。这是目前的代码(删除了不需要的部分):

function Geolocation(maximumAge, accurate) 
    this.settings = 
            'maximumAge': maximumAge,
            'accurate': accurate
    


Geolocation.prototype = 
    position: 
        latitude: null,
        longitude: null,
        altitude: null,
        accuracy: null,
        altitudeAccuracy: null,
        heading: null,
        speed: null
    ,
    lastCheck: null,
    watchId: null,
    onSuccess: function(position) 
        this.position.latitude = position.coords.latitude;
        this.position.longitude = position.coords.longitude;
        this.position.altitude = position.coords.altitude;
        this.position.accuracy = position.coords.accuracy;
        this.position.altitudeAccuracy = position.coords.altitudeAccuracy;
        this.position.heading = position.coords.heading;
        this.position.speed = position.coords.speed;
        this.lastCheck = new Date(position.timestamp);
    , 
    onError: function(error) 
        console.log(error.code+' '+error.message);
    ,
    getCoordinates: function() 
        if (this.watchId == null)  
            this.watchId = navigator.geolocation.watchPosition(this.onSuccess, this.onError,  maximumAge: this.settings.maximumAge, enableHighAccuracy: this.settings.accurate);
        
        return 
            latitude: this.position.latitude,
            longitude: this.position.longitude
        ;
    
;

您可能已经注意到,当我调用 getCoordinates() 时,回调成功函数(我猜)超出了对象的范围,因此不知道“this”是什么......知道如何解决这个问题或正确的实现是什么?

感谢您的宝贵时间!

稍后编辑:我知道我需要修改函数以仅在找到位置后返回坐标。目前不确定如何执行此操作,但如果您有任何提示,那就太好了!

【问题讨论】:

【参考方案1】:

好的,我想通了,部分是在其他线程关于绑定回调函数范围的帮助下:javascript Callback Scope

为了返回坐标,我使用了回调函数。包括下面的代码,我将其标记为社区 wiki - 但如果有人有任何建议,请继续提出。一切似乎都运行良好,但也许我在这里没有做某事。

function Geolocation(maximumAge, accurate) 
    this.settings = 
            'maximumAge': maximumAge,
            'accurate': accurate
    


Geolocation.prototype = 
    position: 
        latitude: null,
        longitude: null,
        altitude: null,
        accuracy: null,
        altitudeAccuracy: null,
        heading: null,
        speed: null
    ,
    lastCheck: null,
    callback: null,
    watchId: null,
    onSuccess: function(position) 
        this.position.latitude = position.coords.latitude;
        this.position.longitude = position.coords.longitude;
        this.position.altitude = position.coords.altitude;
        this.position.accuracy = position.coords.accuracy;
        this.position.altitudeAccuracy = position.coords.altitudeAccuracy;
        this.position.heading = position.coords.heading;
        this.position.speed = position.coords.speed;
        this.lastCheck = new Date(position.timestamp);

        var pos = 
            latitude: this.position.latitude,
            longitude: this.position.longitude
        ;
                // call callback with position and accuracy parameters
        this.callback(pos, this.position.accuracy);
    , 
    onError: function(error) 
        console.log(error.code+' '+error.message);
    ,
    getCoordinates: function(callback) 
                // Helper function to bind scope to callback function as seen at https://***.com/questions/183214/javascript-callback-scope
        function bind(scope, fn) 
            return function () 
                fn.apply(scope, arguments);
            ;
        
                // Assign the callback function to the local member
        this.callback = callback;
                // watchPosition is a method that gets called each time the position changes. Making sure it's only getting called once (watchId can be used to stop the function when necessary
        if (this.watchId == null) 
                        // notice usage of the bind function here 
            this.watchId = navigator.geolocation.watchPosition(bind(this, this.onSuccess), bind(this, this.onError),  maximumAge: this.settings.maximumAge, enableHighAccuracy: this.settings.accurate);
        
    
;

使用示例:

var geo = new Geolocation(3000, true);
geo.getCoordinates(createMap);

function createMap(position, accuracy) 
    // Your code here

【讨论】:

以上是关于Javascript 对象/地理位置 API的主要内容,如果未能解决你的问题,请参考以下文章

温故而知新-Javascript使用地理定位

Google Javascript API 地理编码限制

地理定位 api javascript

Google Maps JavaScript API 地理位置标记

地理位置始终显示莫斯科 Javascript/JQuery/JSON(开放天气 API)

JavaScript/jQuery:在 CodePen 中设置 IP 地理定位 API