获取多个街道地址会返回低质量的结果

时间:2013-09-23 09:07:36

标签: java android gps locationmanager

我的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;
}

...
}

1 个答案:

答案 0 :(得分: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用户有用。