每次调用函数时,如何获取当前的GPS位置? [英] How can I get the current gps location every time a function is called?

查看:75
本文介绍了每次调用函数时,如何获取当前的GPS位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是每次调用函数时获取位置的纬度和经度.据我了解,执行此操作的最佳方法是将位置更新保留几秒钟以获取正确的修复程序,然后将其禁用,但是我无法使其在我的应用程序中正常工作.

What I want to do is to get the location latitude and longitude each time a function is called. As I have understood the best way to do that is to leave the location update for a few seconds to get the correct fix and then disable it, but I can't make it work in my app.

到目前为止,我管理的是每次调用displayData函数时都获得手机的最后一个已知位置,但是我无法克服试图更改为时出现的所有错误. requestLocationUpdates.我要做的就是在从蓝牙设备接收到数据时调用displayData函数,以获取位置并将数据+位置写入文件中.

What I have managed until now is to get the last known location of the phone, every time the displayData function is called, but I have not been able to get over all the errors that appear when I'm trying to change to requestLocationUpdates. What exactly I am doing here is to call the displayData function, when there is incoming data from a bluetooth device, in order to get the location and write the data + location in a file.

有人可以帮我吗,因为所有指南都显示了位置更新时如何触发某些操作,但我不想这样做. 我只想定期找一个正确的位置...

Can someone help me because all the guides show how to trigger something when location updates but I don't want to do that. I just want a correct location periodically...

private void displayData(final byte[] byteArray) {
try {

    mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    // Got last known location. In some rare situations this can be null.

                    if (byteArray != null) {
                        String data = new String(byteArray);
                        tv.setText(n/2 + " measurements since startup...");
                        n += 1;

                        if (location != null) {
                            double lat = location.getLatitude();
                            double lng = location.getLongitude();
                            latitude = String.valueOf(lat);
                            longitude = String.valueOf(lng);
                        }

                        try
                        {
                            FileWriter fw = new FileWriter(textfile,true); //the true will append the new data
                            if (writeDate()) {
                                fw.write("\n");
                                fw.write(stringDate);
                                fw.write(data); //appends the string to the file
                            }
                            else {
                                fw.write(data); //appends the string to the file
                                fw.write(" - ");
                                fw.write(latitude);
                                fw.write(",");
                                fw.write(longitude);
                            }
                            fw.close();
                        }
                        catch(IOException ioe)
                        {
                            System.err.println("IOException: " + ioe.getMessage());
                        }


                        // find the amount we need to scroll. This works by
                        // asking the TextView's internal layout for the position
                        // of the final line and then subtracting the TextView's height
                        final int scrollAmount = tv.getLayout().getLineTop(
                                tv.getLineCount())
                                - tv.getHeight();
                        // if there is no need to scroll, scrollAmount will be <=0
                        if (scrollAmount > 0)
                            tv.scrollTo(0, scrollAmount);
                        else
                            tv.scrollTo(0, 0);
                    }
                }
            });

} catch (SecurityException e) {
    // lets the user know there is a problem with the gps
}
}

推荐答案

我遵循Markus Kauppinen的方法,要求在适合我的应用程序的时间间隔内更新位置,然后在有来自蓝牙的传入数据时才使用获取最后的已知位置.因此,我在活动中添加了以下内容:

I followed Markus Kauppinen's approach requesting for location updates at a time interval that suits my app and then just using the get last known location when there is incoming data from bluetooth. So I just added the following in my activity:

    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(30000);
    mLocationRequest.setFastestInterval(10000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    LocationCallback mLocationCallback = new LocationCallback();



// Register the listener with the Location Manager to receive location updates
    try {
        mFusedLocationClient.requestLocationUpdates(mLocationRequest,
                mLocationCallback,
                null /* Looper */);

    }
    catch (SecurityException e) {
        // lets the user know there is a problem with the gps
    }

这篇关于每次调用函数时,如何获取当前的GPS位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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