0

アプリケーションで現在の場所 (緯度と経度の値) を取得しようとしています。以下のコードを使用しました。

問題は ------------------>

 When the Best provider is GPS,Location returned  is null.

私はいくつかのSO投稿を学び、コードにLocationListenerを追加しましたが、うまくいきましたが、デバイスを再起動してアプリケーションを再度実行すると、プロバイダーがGPSの場合と同じNULL値が返されます。なぜ今それが機能しないのか理解できません。

また、Gps プロバイダーが値を取得するのが遅いことも知ったので、この問題を解決するには LocationListener を追加する必要があります。これをコードに追加しました。

  LocationListener locationListener = new MyLocationListener();
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, locationListener);

ここに完全なコード ----------->

      public void getCurrentlocation()
    {
        String providers = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
        Log.e("provider"," "+providers);
        if(!providers.contains("gps")){
          final Intent poke = new Intent();
          poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
          poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
          poke.setData(Uri.parse("3")); 
          sendBroadcast(poke);
          Log.e("your gps is","enabled");
        }
        LocationListener locationListener = new MyLocationListener();
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, locationListener);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 35000, 10, locationListener);
           // Define the criteria how to select the locatioin provider -> use
           // default
           Criteria criteria = new Criteria();
           String provider = locationManager.getBestProvider(criteria,true);
           Log.e("location provider",""+provider);
           Location location = locationManager.getLastKnownLocation(provider);

           // Initialize the location fields
           if (location != null) 
           {
            System.out.println("Provider " + provider + " has been selected.");
            strLatitude=String.valueOf(location.getLatitude());
                  strLongitude=String.valueOf(location.getLongitude());
                  Log.e("lattitude",strLatitude);
                  Log.e("logntitude",strLongitude);
                  latituteField.setText( strLatitude);
                  longitudeField.setText(strLongitude);
           }
           else 
           {


                  strLatitude=String.valueOf(0);
                  strLongitude=String.valueOf(0);
                  Log.e("lattitude",strLatitude);
                  Log.e("logntitude",strLongitude);
                  latituteField.setText( strLatitude);
                  longitudeField.setText(strLongitude);
           }




     } 



    private class MyLocationListener implements LocationListener
    {

        @Override
        public void onLocationChanged(Location location)
        {

//            mLattitude = location.getLatitude();
//            mLongitude = location.getLongitude();
//
//            Log.w("value : ",
//                    String.valueOf(location.getLatitude()) + String.valueOf(location.getLongitude())
//                            + 
//
//
//            context.sendBroadcast(new Intent(Constants.BRDCAST_LOCATION_UPDATED));

        }

        @Override
        public void onProviderDisabled(String provider)
        {
            // Log.w("provider", provider);

        }

        @Override
        public void onProviderEnabled(String provider)
        {
            // Log.w("provider", provider);

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras)
        {
            // Log.w("status", String.valueOf(status));

        }


}

助けてください

前もって感謝します

4

1 に答える 1

0

さて、次のコードを試してみたところ、緯度と経度が取得されました。

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

/* Use the LocationManager class to obtain GPS locations */

  LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
  List<String> providers = lm.getProviders(true);

/* 配列を逆方向にループし、正確な位置を取得したら、ループを抜け出します */

      Location location = null;

      for (int i=providers.size()-1; i>=0; i--) {
              location = lm.getLastKnownLocation(providers.get(i));
              if (location != null) break;
      }

      String Text = "My current location is: " +
              "Latitud = " + location.getLatitude() +
              "Longitud = " + location.getLongitude();

              Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
   }

AndroidManifest.xml:

次のアクセス許可が設定されていることを確認します。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
于 2013-01-25T06:46:30.023 に答える