0

私は次のようにコーディングします:

   Criteria cri = new Criteria();
   LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
   String tower = locationManager.getBestProvider(cri, false);
   Location location = locationManager.getLastKnownLocation(tower);

私のデバイスのアンドロイドはwifi、gpsをオンにしていました(インターネットは有効です)が、場所= nullです。なんで?

4

2 に答える 2

2

これを試してください。現在の場所を取得するには、以下のコードを使用します。

    String location;
        LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
            location = LocationManager.PASSIVE_PROVIDER;
        else
            location = LocationManager.GPS_PROVIDER;
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates(location, 0, 0, mlocListener);
        mlocManager.requestLocationUpdates(location, 0, 1, mlocListener);
        Location locate = mlocManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (locate != null) {
            lati = locate.getLatitude();
            longi = locate.getLongitude();

        } else {
            locate = mlocManager
                    .getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
            if (locate != null) {
                lati = locate.getLatitude();
                longi = locate.getLongitude();

            }
        }

MyLocationListener クラス

   public class MyLocationListener implements LocationListener {

    public void onLocationChanged(Location loc) {
        lati = loc.getLatitude();
        longi = loc.getLongitude();

    }

    public void onProviderDisabled(String provider) {

    }

    public void onProviderEnabled(String provider) {

    }

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

    public MainActivity getLocationCordinates() {
        MainActivity location_Gps = new MainActivity();
        return location_Gps;
    }
}

次の権限をmanifest.xml

  <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
于 2013-03-19T07:14:42.387 に答える
0

使ってみてください:

Criteria cri = new Criteria();
cri.setAccuracy(Criteria.ACCURACY_FINE);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
String tower = locationManager.getBestProvider(cri, false);
Location location = locationManager.getLastKnownLocation(tower);
于 2013-03-19T07:14:28.187 に答える