0

我的 Android 应用程序使用手机网络或 GPS 获取离用户最近的五个街道地址,具体取决于启用了哪一个。

问题是我请求五个位置,但收到一个精确位置,然后是大约 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

0

通过这种方式,您可以获得很多信息。

String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);

你可以追加result[i]=addresses.get(0).getAddressLine(0)+","+addresses.get(0).getAddressLine(1)..

我希望这对您和新的 SO 用户有用。

于 2013-09-23T09:26:15.753 回答