当前的位置总是空 [英] current location is always null

查看:138
本文介绍了当前的位置总是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找回我使用GPS或network..i曾尝试,我发现的教程及以上职位的所有可能的方法,按一下按钮在编辑框中当前的位置....但我的位置总是空.. 我没有得到我在哪里错了? 我测试了真有网络设备上,但没有互联网/ GPRS ...

I am trying to retrieve my current location on the button click in an editbox using gps or network..i have tried all possible methods which i found in tutorials and older posts....but my location is always null.. i am not getting where i am wrong.? i am testing it on real device having network but no internet/gprs...

这是在code,我很using..i试图与GPS_PROVIDER相同以及..please帮助我..

this is the code which i am using..i tried the same with GPS_PROVIDER as well ..please help me..

     protected void showCurrentLocation() 
   {
     geocoder = new Geocoder(this); 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
     locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 
                MINIMUM_TIME_BETWEEN_UPDATES, 
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                new MyLocationListener()
        );

   Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
   System.out.println("loc.."+location);
   if (location != null) 
   {
   String message = String.format("Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude());
       Toast.makeText(ShowActivity.this, message,
               Toast.LENGTH_LONG).show();
  //acTextView.setText(message);
   try {
          List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10>
          for (Address address : addresses) {
              System.out.println("my location .."+address.getAddressLine(0));
            acTextView.setText(address.getAddressLine(0));
          }



        } catch (IOException e) {
          Log.e("LocateMe", "Could not get Geocoder data", e);
        }
    }
    else
    {
    AlertDialog.Builder alertbox1 = new AlertDialog.Builder(this);
    alertbox1.setMessage("No GPS or network ..Signal please fill the location manually!");
    alertbox1.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface arg0, int arg1) 
    {}});
    alertbox1.show();
    }

}   

我只是编辑的问题,而不是请求新one..because我的问题仍然是关系到一样的...我没有改变,在code任何东西,这是工作完全正常,现在.​​. ......但问题是,当我使用GPS_PROVIDER,它返回准确的地址以及经度的价值和latitude..but当我使用NETWORK_PROVIDER返回经度只值和纬度,地址......我想检索使用NETWORK_PROVIDER因为GPS不工作室内完整的地址...我怎样才能做到这一点?

I am just editing the question instead of asking the new one..because my problem is still related to the same...i didn't change anything in the code and this is working perfectly fine now.....But the issue is when i use the GPS_PROVIDER ,it returns the accurate address along with the value of longitude and latitude..but when i use NETWORK_PROVIDER it returns only the value of longitude and latitude no address...i want to retrieve the full address using NETWORK_PROVIDER because GPS not works indoor ...How can i do that?

谢谢... !!

推荐答案

认罪检查清单file.whether有你添加的权限或没有。

pleas check your manifest file.whether have you added those permissions or not.

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

请参阅更新的答案:

protected void showCurrentLocation() 
       {
         geocoder = new Geocoder(this); 
         locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
         locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER, 
                    MINIMUM_TIME_BETWEEN_UPDATES, 
                    MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                    myLocationListener
            );
         timer = new Timer();
         timer.schedule(new GetLastLocation(),20000);

       }

     class GetLastLocation extends TimerTask {

            @Override
            public void run() {
                timer.cancel();
                locationManager.removeUpdates(locationListener);
                 Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                   System.out.println("loc.."+location);
                   if (location != null) 
                   {
                   String message = String.format("Location \n Longitude: %1$s \n Latitude: %2$s",
                                location.getLongitude(), location.getLatitude());
                       Toast.makeText(ShowActivity.this, message,
                               Toast.LENGTH_LONG).show();
                  //acTextView.setText(message);
                   try {
                          List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10>
                          for (Address address : addresses) {
                              System.out.println("my location .."+address.getAddressLine(0));
                            acTextView.setText(address.getAddressLine(0));
                          }



                        } catch (IOException e) {
                          Log.e("LocateMe", "Could not get Geocoder data", e);
                        }
                    }
                    else
                    {
                    AlertDialog.Builder alertbox1 = new AlertDialog.Builder(this);
                    alertbox1.setMessage("No GPS or network ..Signal please fill the location manually!");
                    alertbox1.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface arg0, int arg1) 
                    {}});
                    alertbox1.show();
                    }
                }   
                return;
            }
        }

     /** The location listener. */
        LocationListener myLocationListener = new LocationListener() {

            public void onLocationChanged(Location location) {

            }
            public void onProviderDisabled(String provider) {}
            public void onProviderEnabled(String provider) {}
            public void onStatusChanged(String provider, int status, Bundle extras) {}
        };

这篇关于当前的位置总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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