GPS位置不正确检索 [英] GPS locations are not retrieved properly

查看:112
本文介绍了GPS位置不正确检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林新的Andr​​oid开发,我明白了Android活动的生命周期。请看下面的code。

Im new to android development and i understand the android activity life cycle. Please see the following code.

    public class MyTest extends Activity{

        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        cont = getApplicationContext();
        final MyLocation my_loc = new MyLocation ();
        my_loc.initialize(cont);

        myMethod();
        }

    Public void myMethod()
    {
       //here when I retrieve the values, it always shows 0.0.
    }
}

    public class MyLocation implements LocationListener {
        public double user_lat;
        public double user_lng;
        private LocationManager locationManager;
        private Context ctx;

        @Override
        public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
            user_lat = location.getLatitude();
            user_lng = location.getLongitude();     

            //here i save the values for constands to use in myMethod                           
            }
        }

        public void initialize(Context context) {
            this.ctx = context;
            locationManager = (LocationManager) context
                    .getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 0, 0, this);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                    0, this);
        } 
}

为什么我不能初始化GPS,并在同一活动取得长期和纬度值?

Why is it that i cannot initialize GPS and retrieve long and lat values in the same activity?

推荐答案

LocationClient是让全球定位系统的新途径。关注影片有关最新更新的完整详细信息。

LocationClient is the new way of getting GPS. Watch video for complete details on the recent update.

请注意,这样做事情的LocationManager方式是越野车,因为有需要补充大量的code GPS和网络供应商。新方法(内部的谷歌称融合位置提供商)与传感器的工作原理,以减少电池消耗大的时候。同时降低了对复杂的API和舞台明智选择最佳提供商。它只是2-3线code和你做。

Note that the LocationManager way of doing things is buggy, since there is need to add a lot of code for GPS and NETWORK provider. The new way (internal to Google referred to as the Fused Location Provider) works with sensors to reduce battery consumption big time. Also reduces the need for complex API's and stage wise selection of the best provider. Its just 2-3 lines of code and you are done.

由于许多三星手机(Y型含)虽然有大部分的时间他们不返回的位置在所有的特定问题。所以,你需要踢启动手机返回的GPS。要做到这一点,你可以用

With many Samsung phones (Y model including) though there is a specific issue that most of the times they don't return location at all. So you need to kick start that phone to return the GPS. To do that you can use

HomeScreen.getLocationManager().requestLocationUpdates(
    LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
        @Override
        public void onProviderEnabled(String provider) {
        }
        @Override
        public void onProviderDisabled(String provider) {
        }
        @Override
        public void onLocationChanged(final Location location) {
        }
    });

然后打电话给你的 locationClient.getLastLocation API。作为把code以上,之前您LocationClient.getLastLocation或LocationManager.getLastKnownLocation电话。

And then call your locationClient.getLastLocation api. As in put the code above, just before your LocationClient.getLastLocation or LocationManager.getLastKnownLocation call.

你要知道三星是高度定制的Andr​​oid的开源产品。谷歌无法响应三星相关的错误,而三星没有任何Android开发相关的支持。

Mind you Samsung is the highly customized android open source product. Google cannot respond to samsung related bugs, and Samsung does not have any android developer related support.

编辑:观看视频,相信我,不知道是什么LocationClient给你,你不会AP preciate的变化。您还将了解GeoFenceing。

Edit : Watch the video, trust me, without knowing what LocationClient gives you, you wont appreciate the change. You will also learn about GeoFenceing.

这篇关于GPS位置不正确检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆