当前位置始终为空

时间:2011-08-09 10:41:46

标签: android geolocation geocoding locationmanager

我正在尝试使用gps或network来检索我在编辑框中单击按钮的当前位置。我尝试了所有可能的方法,我在教程和旧帖子中找到....但我的位置始终为null .. 我没有得到我错的地方。 我在具有网络但没有互联网/ gprs的真实设备上进行测试...

这是我正在使用的代码。我尝试使用GPS_PROVIDER也一样..请帮助我..

     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();
    }

}   

我只是在编辑问题而不是问新问题。因为我的问题仍然与此相关...我没有改变代码中的任何内容,这现在工作得很好.....但问题是当我使用GPS_PROVIDER时,它会返回准确的地址以及经度和纬度的值。但是当我使用NETWORK_PROVIDER时它只返回经度和纬度的值没有地址...我想要检索完整的地址使用NETWORK_PROVIDER,因为GPS无法在室内工作......我怎么能这样做?

...谢谢!

6 个答案:

答案 0 :(得分:3)

请检查您的清单文件。您是否已添加这些权限。

 <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) {}
        };

答案 1 :(得分:1)

locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);获取空值的另一个原因是,如果您的网络提供商已关闭。在我的手机上,这是通过设置切换 - &gt;位置 - &gt;使用无线网络。

编辑:

您也可以尝试:

LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)

查看网络提供商是否已启用或

LocationManager.getProviders(true)

返回任何已启用的提供商。

有关详细信息,请参阅http://developer.android.com/reference/android/location/LocationManager.htm

答案 2 :(得分:1)

Android 2.3.x SDK(API 9和10)模拟器存在GPS错误。您应该尝试使用2.2(API 8)上的代码

答案 3 :(得分:1)

当我们指定NETWORK_PROVIDER来监听位置更新时,

Android系统利用Wi-Fi ID或互联网接入点来确定位置

如果没有互联网访问权限,则会返回空值

也发生在我身上,它没有像预期的那样从手机信号塔获得位置

请阅读:no gprs/no wi-fi location updates

答案 4 :(得分:0)

试用此代码:

public CurrentLocation(Context ctx) {
    this.context = ctx;

    locationManager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    @SuppressWarnings("unused")
    List<String> providers = locationManager.getAllProviders();

    Criteria locationCritera = new Criteria();
    String providerName = locationManager.getBestProvider(locationCritera,
            true);

    location = locationManager.getLastKnownLocation(providerName);
    locationListener = new MyLocationListener();

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
            0, locationListener);



}

答案 5 :(得分:0)

使用此代码

if (android.os.Build.VERSION.SDK_INT > 8) {

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

    }

并检查您的设置 - &gt;位置和安全 - &gt;是否启用无线网络。