误解有关onLocationChange误解、onLocationChange

2023-09-06 10:42:34 作者:初南溪

让我给你一个简单的背景。

Let me give you a brief background.

我需要的无论是过了一段时间后,还是有一段距离的检查用户的位置。所以我使用LocationListner类来跟踪的位置。为了测试它的模拟器,我在。GPX文件创建设定方向。而我得到的仿真器的位置目前按一下按钮。但我如何能够获得基于距离或时间更新的位置。正如develper.adnroid声明

I need to check the users location either after some time or after some distance. So I am using LocationListner class to track the location. To test it on Emulator, I have created on .gpx file to set the direction. and I am getting the location on emulator currently on button click. But how will I be able to get the updated location based on distance or time. As stated by develper.adnroid

You can control the frequency at which your listener receives updates with the second 
and third parameter—the second is the minimum time interval between notifications and
the third is the minimum change in distance between notifications—setting both to
zero requests location notifications as frequently as possible.

但我没有得到预期的结果。做任何身体知道什么是错的,我做的事情。??

But I am not getting the expected result. Do any body know What's wrong I am doing.??

code片段

在点击链接这块code的执行

On button click this piece of code execute

    LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            MyLocationListener locListener = new MyLocationListener(this, locManager);
            boolean isGPSEnabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
            if (isGPSEnabled) 
            {
                Toast.makeText(getApplicationContext(), "Called isGPSEnabled", Toast.LENGTH_SHORT).show();
                locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 0, locListener);
                } 

FYI:烤面包里面,如果叫

这是我onLoactionChange。添加此方法只能作为其他方法都是空的。

And this is my onLoactionChange. Adding this method only as rest methods are empty.

@Override
    public void onLocationChanged(Location location) 
    {
           if(location != null)
        {
            locManager.removeUpdates(this);

            Geocoder gcd = new Geocoder(context.getApplicationContext(), Locale.getDefault());       
            List<Address>  addresses; 
            try
            {  
                addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);  
                if (addresses.size() > 0)
                {
                    String cityName = addresses.get(0).getAddressLine(0) + "\n";
                    cityName = cityName + addresses.get(0).getAddressLine(1);
                    TextView tvInfo = (TextView) ((Activity) context).findViewById(R.id.tv_info);
                    tvInfo.setVisibility(View.VISIBLE);
                    tvInfo.setText("Curent address is" + cityName + "Latitude :" + addresses.get(0).getLatitude() + "Longitude :" +addresses.get(0).getLongitude());
                }
            } catch (IOException e) 
            {           
                //Toast
                Toast.makeText(context.getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                e.printStackTrace();  
            } 
        }
    }

清单有权限

   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.INTERNET" />

结果:一旦按钮被点击它给出的当前位置。但仍只有所有的时间。

Result: Once button is clicked it gives the current location. but remains that only all the time.

预期结果:要么一段时间后,还是有些距离的移动当前位置应该更新。

Expected result: Either after some time or some distance moved current location should update.

任何波特能帮我实现这个。或指出哪里我做错了。??

Can any bode help me in achieving this. or point out where am I doing wrong.??

推荐答案

看来我需要删除 locManager.removeUpdates(本);

删除这条线使我的应用程序来接收模拟器更新。

Removing this line enabled my application to receive update on emulator.