设备准备好后的地理位置

时间:2011-09-19 10:06:11

标签: iphone jquery-ui geolocation cordova

我有一个用jquery mobile构建的iphone应用程序,它包含在phonegap中。设备准备好摆脱讨厌的警报/var/mobile/Applications/157EB70D-4AA7-826E-690F0CBE0F/appname.app/www/index.html后,我正试图获得地理定位。

我已经设置了一些警报,看看设备是否准备就绪,并且一直说isDeviceReady是:false,这意味着设备没有准备就绪

他是代码

$(function() {


var isWatching = false;
var isAndroid = ( navigator.userAgent.indexOf('Android') != -1 ) ? true : false;
    var isDeviceReady = false;

   document.addEventListener("deviceready", onDeviceReady, false);

   function onDeviceReady() {
  isDeviceReady = true;
   }
   function getClientPosition(id, successCallback, errorCallback) {
    if ( isDeviceReady ) {
      var hasFoundMarker = false;
    if ( hasClientPosition() ) {
$(id).gmap('findMarker', 'tag', 'client', function(found, marker) { 
                if (found) {
                    hasFoundMarker = true;
                    marker.setPosition(getClientLatLng()); 
                         $(id).gmap('getMap').setCenter(marker.getPosition());
        if ( $.isFunction(successCallback) ) {
        successCallback.call(this, getClientLatLng());
                    }
                } 
            });
            if ( !hasFoundMarker ) {
                addClientMarker(id, getClientLatLng());
                if ( $.isFunction(successCallback) ) {
                    successCallback.call(this,    getClientLatLng());
                }
            }
        }

                            if ( !isWatching[id] ) {
            if ( navigator.geolocation ) {
                isWatching[id] = true;
                if ( isAndroid ) {
                    watch[id] = setInterval(function() {
                         navigator.geolocation.getCurrentPosition ( 
                            function( position ) {
                                var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                                $(id).gmap('findMarker', 'tag', 'client', function(found, marker) { 
                                    if (found) { 
                                        hasFoundMarker = true;
                                        marker.setPosition(latlng);
                                        $(id).gmap('getMap').setCenter(latlng);
                                    }
                                });
                                if ( !hasFoundMarker ) {
                                    addClientMarker(id, latlng);
                                }
                                setData(CLIENT_HAS_POSITION, true);
                                setData(CLIENT_LATITUDE, latlng.lat());
                                setData(CLIENT_LONGITUDE, latlng.lng());
                                if ( $.isFunction(successCallback) ) {
                                    successCallback.call(this, latlng);
                                }
                            },
                            function( error ) {
                                if ( $.isFunction(errorCallback) ) {
                                    errorCallback.call(this, error);
                                }
                            },
                            opts.geolocationOptions
                        );
                    }, 5000);
                } else {
                    watch[id] = navigator.geolocation.watchPosition ( 
                        function( position ) {
                            var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                            $(id).gmap('findMarker', 'tag', 'client', function(found, marker) { 
                                if (found) { 
                                    hasFoundMarker = true;
                                    marker.setPosition(latlng);
                                    $(id).gmap('getMap').setCenter(latlng);
                                }
                            });
                            if ( !hasFoundMarker ) {
                                addClientMarker(id, latlng);
                            }
                            setData(CLIENT_HAS_POSITION, true);
                            setData(CLIENT_LATITUDE, latlng.lat());
                            setData(CLIENT_LONGITUDE, latlng.lng());
                            if ( $.isFunction(successCallback) ) {
                                successCallback.call(this, latlng);
                            }
                        },
                        function( error ) {
                            if ( $.isFunction(errorCallback) ) {
                                errorCallback.call(this, error);
                            }
                        },
                        opts.geolocationOptions
                    );
                }
            }
        } 

    } else {
        var timer = setTimeout(function() {
            getClientPosition(id, successCallback, errorCallback);
            alert('Trying to get client position. This message will pop up again in 15 sec, unless device is ready. IsDeviceReady is: '+isDeviceReady);
        }, 15000);
    }
}

任何人都有任何想法我错了吗?

0 个答案:

没有答案