通过Android应用程序获取GPS定位瞬间 [英] Get GPS Location instantly via Android app

查看:111
本文介绍了通过Android应用程序获取GPS定位瞬间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有。我正在写一个Android应用程序有关GPS位置。

我试图在仿真器和手动输入经纬度,以及它工作得很好。 不过,我的问题是:真正的设备上,在调试模式下,去当位置改变,才能达到下一个类使用意图。如果我直接启动应用程序,我可以看到闪烁的GPS图标,但应用程序将只留在这儿,并不会开始下一个活动。它似乎在onLocationChanged变量()将永远不会被改变。

我听说拿到的位置瞬间是使用getLastKnownLocation()方法。但我没有得到我应该使用它。

下面是类的我如何使用LocationManager来获取位置。

有什么办法呢?我很困惑。非常感谢你!

 公共类MAINMENU延伸活动{

私人LocationManager locationManager = NULL;

公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);




    按钮按钮1 =(按钮)findViewById(R.id.button1);
    button1.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){

            意图I3 =新意图();
            i3.setClass(mainMenu.this,police.class);
            i3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mainMenu.this.startActivityForResult(i3,0);


        }
    });

    按钮按钮2 =(按钮)findViewById(R.id.button2);
    button2.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){

            意图I3 =新意图();
            i3.setClass(mainMenu.this,ambulance.class);
            i3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mainMenu.this.startActivityForResult(i3,0);


        }
    });

    按钮按钮3 =(按钮)findViewById(R.id.button3);
    button3.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){

            意图I3 =新意图();
            i3.setClass(mainMenu.this,fire_station.class);
            i3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mainMenu.this.startActivityForResult(i3,0);


        }
    });



        locationManager =(LocationManager)mainMenu.this.getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,120000,0,新MyLocationUpdater());

        字符串提供商= LocationManager.GPS_PROVIDER;
        位置位置= locationManager.getLastKnownLocation(供应商);



}



公共类MyLocationUpdater实现LocationListener的{//改变位置接口
    @覆盖
    公共无效onLocationChanged(位置定位){
        // TODO自动生成方法存根
    //存储位置的数据
    //获取最佳战绩



        双纬度= location.getLatitude();
        双LON = location.getLongitude();

        的System.out.println(纬度为+纬度+和
                +经度是+ LON);


        双lat11 =纬度 -  1 / 69.0;
        双lat12 =纬度+ 1 / 69.0;
        双lon11 = LON  -  1 / 42.0;
        双lon12 = LON + 1 / 42.0;

        StaticVariables.latS1 = lat11.toString();
        StaticVariables.latN1 = lat12.toString();
        StaticVariables.lonW1 = lon11.toString();
        StaticVariables.lonE1 = lon12.toString();

        双lat111 =纬度 -  2 / 69.0;
        双lat121 =纬度+ 2 / 69.0;
        双lon111 = LON  -  2 / 42.0;
        双lon121 = LON + 2 / 42.0;

        StaticVariables.latS11 = lat111.toString();
        StaticVariables.latN11 = lat121.toString();
        StaticVariables.lonW11 = lon111.toString();
        StaticVariables.lonE11 = lon121.toString();

    // ================================================ ==
    //救护车

        双LAT21 =纬度 -  3 / 69.0;
        双lat22 =纬度+ 3 / 69.0;
        双lon21 = LON  -  3 / 42.0;
        双lon22 = LON + 3 / 42.0;

        StaticVariables.latS2 = lat21.toString();
        StaticVariables.latN2 = lat22.toString();
        StaticVariables.lonW2 = lon21.toString();
        StaticVariables.lonE2 = lon22.toString();

        双lat211 =纬度 -  5.5 / 69.0;
        双lat221 =纬度+ 5.5 / 69.0;
        双lon211 = LON  -  5.5 / 42.0;
        双lon221 = LON + 5.5 / 42.0;

        StaticVariables.latS21 = lat211.toString();
        StaticVariables.latN21 = lat221.toString();
        StaticVariables.lonW21 = lon211.toString();
        StaticVariables.lonE21 = lon221.toString();

    // ================================================ ===
    //消防站

        双lat31 =纬度 -  2 / 69.0;
        双lat32 =纬度+ 2 / 69.0;
        双lon31 = LON  -  2 / 42.0;
        双lon32 = LON + 2 / 42.0;

        StaticVariables.latS3 = lat31.toString();
        StaticVariables.latN3 = lat32.toString();
        StaticVariables.lonW3 = lon31.toString();
        StaticVariables.lonE3 = lon32.toString();

        双lat311 =纬度 -  2 / 69.0;
        双lat321 =纬度+ 2 / 69.0;
        双lon311 = LON  -  2 / 42.0;
        双lon321 = LON + 2 / 42.0;

        StaticVariables.latS31 = lat311.toString();
        StaticVariables.latN31 = lat321.toString();
        StaticVariables.lonW31 = lon311.toString();
        StaticVariables.lonE31 = lon321.toString();

        意向意图=新的意图();
        intent.setClass(mainMenu.this,getPhoneNumber.class);
        mainMenu.this.startActivity(意向);
    }

    @覆盖
    公共无效onProviderDisabled(字符串为arg0){
        // TODO自动生成方法存根

    }

    @覆盖
    公共无效onProviderEnabled(字符串为arg0){
        // TODO自动生成方法存根

    }

    @覆盖
    公共无效onStatusChanged(字符串为arg0,INT ARG1,捆绑ARG2){
        // TODO自动生成方法存根

    }
}

    }
 

解决方案

getLastKnownLocation 方法不会触发 onLocationChanged 事件。重构你的code的一种方法是将前进一位置来一个单独的方法,然后调用该方法调用两个后<$ C $的行为逻辑C> getLastKnownLocation ,并从 onLocationChanged 方法。

请记住,有没有保证 getLastKnownLocation 将提供一个有意义的位置,因为你的设备可能自上次位置更新已经移动

例如code:

 公共无效的onCreate(包savedInstanceState){
    ....
    字符串提供商= LocationManager.GPS_PROVIDER;
    位置位置= locationManager.getLastKnownLocation(供应商);
    updateLocation(位置);
}

公共类MyLocationUpdater实现LocationListener的{//改变位置接口
    @覆盖
    公共无效onLocationChanged(位置定位){
        updateLocation(位置);
    }
    ...
}

无效updateLocation(位置定位){
    双纬度= location.getLatitude();
    双LON = location.getLongitude();

    //从code,其余onLocationChanged
}
 

all. I am writing an android app about GPS locations.

I tried it on emulator and entered the latitude and longitude manually, and it worked fine. However, my problem is: on the real device, in debugging mode, to go the next class by using intent can only be achieved when location is changed. If I start the app directly, I can see the blinking GPS icon, but the app will only stay here, and won't start the next activity. It seems that the variables in the onLocationChanged() will never be changed.

I have heard that to get the location instantly is to use the getLastKnownLocation() method. But I failed to get where I should use it.

Here is the class of how I use the LocationManager to get the locations.

Is there any solutions? I am quite confused. Thank you very much!!

    public class mainMenu extends Activity{

private LocationManager locationManager = null;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);




    Button button1 = (Button)findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

            Intent i3 = new Intent();
            i3.setClass(mainMenu.this, police.class);
            i3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mainMenu.this.startActivityForResult(i3,0);


        }
    });

    Button button2 = (Button)findViewById(R.id.button2);
    button2.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

            Intent i3 = new Intent();
            i3.setClass(mainMenu.this, ambulance.class);
            i3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mainMenu.this.startActivityForResult(i3,0);


        }
    });

    Button button3 = (Button)findViewById(R.id.button3);
    button3.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

            Intent i3 = new Intent();
            i3.setClass(mainMenu.this, fire_station.class);
            i3.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mainMenu.this.startActivityForResult(i3,0);


        }
    });



        locationManager = (LocationManager)mainMenu.this.getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000 , 0, new MyLocationUpdater());

        String provider = LocationManager.GPS_PROVIDER;
        Location location = locationManager.getLastKnownLocation(provider);



}



public class MyLocationUpdater implements LocationListener{ //change location interface
    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
    //          store the location data
    //          get the best record



        Double lat = location.getLatitude();
        Double lon = location.getLongitude();

        System.out.println("The latitude is " + lat + "and " 
                + "the longitude is "+ lon);


        Double lat11 = lat - 1/69.0;
        Double lat12 = lat + 1/69.0;
        Double lon11 = lon - 1/42.0;
        Double lon12 = lon + 1/42.0;

        StaticVariables.latS1 = lat11.toString();
        StaticVariables.latN1 = lat12.toString();
        StaticVariables.lonW1 = lon11.toString();
        StaticVariables.lonE1 = lon12.toString();

        Double lat111 = lat - 2/69.0;
        Double lat121 = lat + 2/69.0;
        Double lon111 = lon - 2/42.0;
        Double lon121 = lon + 2/42.0;

        StaticVariables.latS11 = lat111.toString();
        StaticVariables.latN11 = lat121.toString();
        StaticVariables.lonW11 = lon111.toString();
        StaticVariables.lonE11 = lon121.toString();

    //          ==================================================
    //          ambulances

        Double lat21 = lat - 3/69.0;
        Double lat22 = lat + 3/69.0;
        Double lon21 = lon - 3/42.0;
        Double lon22 = lon + 3/42.0;

        StaticVariables.latS2 = lat21.toString();
        StaticVariables.latN2 = lat22.toString();
        StaticVariables.lonW2 = lon21.toString();
        StaticVariables.lonE2 = lon22.toString();

        Double lat211 = lat - 5.5/69.0;
        Double lat221 = lat + 5.5/69.0;
        Double lon211 = lon - 5.5/42.0;
        Double lon221 = lon + 5.5/42.0;

        StaticVariables.latS21 = lat211.toString();
        StaticVariables.latN21 = lat221.toString();
        StaticVariables.lonW21 = lon211.toString();
        StaticVariables.lonE21 = lon221.toString();

    //          ===================================================
    //          fire stations

        Double lat31 = lat - 2/69.0;
        Double lat32 = lat + 2/69.0;
        Double lon31 = lon - 2/42.0;
        Double lon32 = lon + 2/42.0;

        StaticVariables.latS3 = lat31.toString();
        StaticVariables.latN3 = lat32.toString();
        StaticVariables.lonW3 = lon31.toString();
        StaticVariables.lonE3 = lon32.toString();

        Double lat311 = lat - 2/69.0;
        Double lat321 = lat + 2/69.0;
        Double lon311 = lon - 2/42.0;
        Double lon321 = lon + 2/42.0;

        StaticVariables.latS31 = lat311.toString();
        StaticVariables.latN31 = lat321.toString();
        StaticVariables.lonW31 = lon311.toString();
        StaticVariables.lonE31 = lon321.toString();

        Intent intent = new Intent();
        intent.setClass(mainMenu.this, getPhoneNumber.class);
        mainMenu.this.startActivity(intent);
    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }
}

    }

解决方案

The getLastKnownLocation method does not trigger an onLocationChanged event. One way to refactor your code would be to move the logic that acts on a Location to a separate method and then call that method both after you call getLastKnownLocation, and from your onLocationChanged method.

Bear in mind that there is no guarantee that getLastKnownLocation will provide a meaningful Location, since you the device might have moved since the last location update.

Example code:

public void onCreate(Bundle savedInstanceState){
    ....
    String provider = LocationManager.GPS_PROVIDER;
    Location location = locationManager.getLastKnownLocation(provider);
    updateLocation(location);
}

public class MyLocationUpdater implements LocationListener{ //change location interface
    @Override
    public void onLocationChanged(Location location) {
        updateLocation(location);
    }
    ...
}

void updateLocation(Location location) {
    Double lat = location.getLatitude();
    Double lon = location.getLongitude();

    // the rest of the code from onLocationChanged
}

这篇关于通过Android应用程序获取GPS定位瞬间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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