0

私の Android アプリは、有効になっている携帯電話ネットワークまたは GPS のいずれかを使用して、ユーザーに最も近い 5 つの番地を取得します。

問題は、私が 5 つの場所をリクエストしているのに、1 つの正確な場所を受信し、その後約 3 つのひどい場所が続き、1 つが場所を取得できなかったことです。

に住んでいるとし123 Wilbur Rd., Townsvilleます。結果は次のようになります。

1. 143 Wilbur Rd., Townsville
2. USA, null
3. null, Townsville
4. null, NearbyVillage
5. null, null

これらの結果の品質を向上させる方法はありますか、それとも最初の結果を自動的に選択する必要がありますか? 私のロケータークラスの関連するメソッドは以下のとおりです

GPSTracker.java

public class GPSTracker extends Service implements LocationListener {

private final Context mContext;

// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
}

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            Log.d("TAG", "No GPS or Network enabled. Unable to get location.");
            Toast.makeText(mContext, "Error: No GPS or Network available.", Toast.LENGTH_LONG).show();
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

/**
 * Attempts to get the closest five address using getLatitiudeAndLongitude()
 * Returns the closest address if an address is found, otherwise returns dummy values
 * @return an array of the nearest five addresses in a String array
 */
public String[] getNearestFiveAddresses(){
    String[] result = {"fail", "fail", "fail", "fail", "fail"};//TODO dummy values for now
    int RESULT_SIZE = 5;
    //try to get closest address
    try {

        Geocoder geo = new Geocoder(mContext, Locale.getDefault());
        List<Address> addresses = geo.getFromLocation(latitude, longitude, 5);
        if (addresses.isEmpty()) {
            Log.d("TAG", "No addresses found");
        }
        else {
            if (addresses.size() > 0) {
                for(int i=0; i<RESULT_SIZE; i++){
                    result[i] = addresses.get(i).getAddressLine(i) + ", " + addresses.get(i).getLocality();
                }
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace(); // getFromLocation() may sometimes fail
    }

    return result;
}

...
}
4

1 に答える 1