2

gpsまたはネットワークを使用して、編集ボックスのボタンをクリックして現在の場所を取得しようとしています..チュートリアルや古い投稿で見つけたすべての可能な方法を試しました....しかし、私の場所は常に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が機能しないため...どうすればいいですか?

ありがとう...!!

4

6 に答える 6

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) {}
        };
于 2011-08-09T10:49:06.553 に答える
1

から null が返されるもう 1 つの理由はlocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);、ネットワーク プロバイダーがオフになっている場合です。私の電話では、これは [設定] -> [場所] -> [ワイヤレス ネットワークを使用] で切り替えられます。

編集:

あなたも試すことができます:

LocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)

ネットワークプロバイダーが有効になっているかどうかを確認するか、

LocationManager.getProviders(true)

有効なプロバイダーを返します。

詳細については、 http://developer.android.com/reference/android/location/LocationManager.htmを参照してください。

于 2011-08-09T11:19:19.443 に答える
1

NETWORK_PROVIDER を指定して位置情報の更新をリッスンすると、

Android システムは、Wi-Fi ID またはインターネット アクセス ポイントを使用して場所を特定します

インターネットにアクセスできない場合、null 値を返します

私にも起こりました。期待どおりに携帯電話基地局から位置情報を取得できません

これを読んでください:GPRSなし/ Wi-Fiロケーションの更新なし

于 2011-11-11T09:07:56.410 に答える
1

Android 2.3.x SDK (API 9 および 10) エミュレーターには GPS に関するバグがあります。2.2 (API 8) でコードを試す必要があります。

于 2011-09-28T12:17:43.037 に答える
0

このコードを使用

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

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

    }

また、[設定] --> [場所とセキュリティ] --> [ワイヤレス ネットワークを使用する] が有効になっているかどうかも確認してください。

于 2012-12-13T13:02:00.083 に答える
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);



}
于 2011-08-09T11:18:37.650 に答える