Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 正在检索上次已知的地理位置-Phonegap_Jquery_Android_Iphone_Html_Geolocation - Fatal编程技术网

Jquery 正在检索上次已知的地理位置-Phonegap

Jquery 正在检索上次已知的地理位置-Phonegap,jquery,android,iphone,html,geolocation,Jquery,Android,Iphone,Html,Geolocation,下面的代码 navigator.geolocation.getCurrentPosition(getGeo_Success, getGeo_Fail, { enableHighAccuracy : true, maximumAge : Infinity, timeout : 15000 }); 检索当前GPS位置-但是-必须有有效的GPS信号(当然,设备的GPS功能应该打开) 如果我们查看其他应用程序(比如Android设备上的地图)——它知道如何检索上次已知的位置——

下面的代码

navigator.geolocation.getCurrentPosition(getGeo_Success, getGeo_Fail, {
    enableHighAccuracy : true,
    maximumAge : Infinity,
    timeout : 15000
});
检索当前GPS位置-但是-必须有有效的GPS信号(当然,设备的GPS功能应该打开)

如果我们查看其他应用程序(比如Android设备上的地图)——它知道如何检索上次已知的位置——即使我在打开地图之前没有使用更新地理位置的应用程序——它会在地图上显示我的位置,即使我在没有GPS信号的建筑物内

我只是想澄清一下:我对我的应用程序检索到的最后一个地理位置不感兴趣,因为在下一次启动它时,这个地理位置可能无关紧要


问题是:我们如何通过HTML5/Phonegap实现这一点?似乎是
navigator。地理位置
只知道检索当前位置,即使
maximumAge
设置为
Infinity
(这意味着最后一个缓存位置的年龄无关,所以任何命中都可以(或者应该是!)

我在
getGeo\u Success
函数调用
setLocation中建议([u位置]
要将其存储在本地存储上
您可以假设每个使用phoneGap的设备都已经实现了本地存储。
这样,你总是在那里有有效的位置。然后只要在你需要的时候弹出它

function getLocation() {
  return JSON.parse(localStorage.getItem('location'));
}
function setLocation(location) {
  localStorage.setItem('vibesList', JSON.stringify(vibes));
}
编辑:如果你想检索设备知道的最新位置(不是应用程序),你必须通过后台过程从设备中提取当前数据。这是一种有问题的方法,因为:

  • 并非所有的设备都允许这样做
  • 您必须编写自己的本机自定义来连接 电话差距
  • ANDROID解决方案(iPhone解决方案如下):

    public class App extends DroidGap {
    
        // Hold a private member of the class that calls LocationManager
        private GetNativeLocation gLocation;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // This line is important, as System Services are not available
            // prior to initialization
            super.init();
    
            gLocation = new GetNativeLocation(this, appView);
    
            // Add the interface so we can invoke the java functions from our .js 
            appView.addJavascriptInterface(gLocation, "NativeLocation");
    
            try {
                super.loadUrl("file:///android_asset/www/index.html");
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    
        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
        }
    
        @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
        }
    }
    
    这很好:

    我使用了Android自带的
    LocationManager
    ,它提供了一个
    getLastKnownLocation
    函数——名字说明了一切

    这是相关代码

    1) 将以下java类添加到应用程序中

    package your.package.app.app;
    
    import org.apache.cordova.DroidGap;
    
    import android.content.Context;
    import android.location.*;
    import android.os.Bundle;
    import android.webkit.WebView;
    
    public class GetNativeLocation implements LocationListener {
        private WebView mAppView;
        private DroidGap mGap;
        private Location mostRecentLocation;
    
        public GetNativeLocation(DroidGap gap, WebView view) {
            mAppView = view;
            mGap = gap;
        }
    
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            getLocation();
        }
    
        public void getLocation() {
            LocationManager lm = 
                            (LocationManager)mGap.
                                            getSystemService(Context.LOCATION_SERVICE);
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            String provider = lm.getBestProvider(criteria, true);
    
            lm.requestLocationUpdates(provider, 1000, 500, this);
            mostRecentLocation = lm
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }
    
        public void doInit(){
            getLocation();
        }
    
        public double getLat(){ return mostRecentLocation.getLatitude();}
        public double getLong() { return mostRecentLocation.getLongitude(); }
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub  
        }
    
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub  
        }
    
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
        }
    }
    
    2) 确保您的主类如下所示:

    public class App extends DroidGap {
    
        // Hold a private member of the class that calls LocationManager
        private GetNativeLocation gLocation;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // This line is important, as System Services are not available
            // prior to initialization
            super.init();
    
            gLocation = new GetNativeLocation(this, appView);
    
            // Add the interface so we can invoke the java functions from our .js 
            appView.addJavascriptInterface(gLocation, "NativeLocation");
    
            try {
                super.loadUrl("file:///android_asset/www/index.html");
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    
        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
        }
    
        @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
        }
    }
    
    3) 在JS代码中,只需使用以下命令调用本机java:

    window.NativeLocation.doInit();
    alert(window.NativeLocation.getLat());
    alert(window.NativeLocation.getLong());
    
    这就是所有的人!:-)

    编辑: iPhone解决方案:

    public class App extends DroidGap {
    
        // Hold a private member of the class that calls LocationManager
        private GetNativeLocation gLocation;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // This line is important, as System Services are not available
            // prior to initialization
            super.init();
    
            gLocation = new GetNativeLocation(this, appView);
    
            // Add the interface so we can invoke the java functions from our .js 
            appView.addJavascriptInterface(gLocation, "NativeLocation");
    
            try {
                super.loadUrl("file:///android_asset/www/index.html");
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    
        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
        }
    
        @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            super.onStop();
        }
    }
    
    我编写了一个很小的Phonegap插件,它创建了一个自定义类的接口,该自定义类使用iOS的原生
    CLLocationManager

    1)Phonegap插件(JS)

    2)使我们能够调用“CCLocationManager”函数的objective-c类 *国产化*

    最后,调用main.js中的所有内容

    function getLongitudeSuccess(result){
        gLongitude = result;
    }
    
    function getLatitudeSuccess(result){
        gLatitude = result;
    }
    
    function runGPSTimer(){
        var sTmp = "gps";
    
        theTime = setTimeout('runGPSTimer()', 1000);
    
        NativeLocation.getLongitude(
                                    ["getLongitude"],
                                    getLongitudeSuccess,
                                    function(error){ alert("error: " + error); }
                                    );
    
        NativeLocation.getLatitude(
                                   ["getLatitude"],
                                   getLatitudeSuccess,
                                   function(error){ alert("error: " + error); }
                                   );
    

    在我的PhoneGap/Sencha Touch 2应用程序中,我创建了该函数

    function getLastKnownLocation(){
        if(typeof localStorage.lastKnownPosition == "undefined"){
            localStorage.lastKnownPosition = JSON.stringify(null);
        }
    
        navigator.geolocation.getCurrentPosition(
            function(position){
                localStorage.lastKnownPosition = JSON.stringify(position);
            }
        );
    
        return JSON.parse(localStorage.lastKnownPosition);
    }
    

    因此,每次调用getLastKnownLocation()时,我都会立即得到一个结果。对于我来说,这是一个很好的解决方案。

    请记住,此解决方案仅适用于android,正如我前面所说,您必须为每种设备类型找到本机解决方案。祝您好运
    function getLastKnownLocation(){
        if(typeof localStorage.lastKnownPosition == "undefined"){
            localStorage.lastKnownPosition = JSON.stringify(null);
        }
    
        navigator.geolocation.getCurrentPosition(
            function(position){
                localStorage.lastKnownPosition = JSON.stringify(position);
            }
        );
    
        return JSON.parse(localStorage.lastKnownPosition);
    }