0
我的Android應用程序使用手機網絡或GPS獲取最近的五個街道地址,具體取決於哪一個已啓用。獲取多個街道地址正在返回低質量結果
問題是我要求5個位置,但接收一個精確的位置,然後是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;
}
...
}
不,它不是:(我的第一個結果是接近,但我想從幾個選擇誰也接近,而其他地點是非常不準確或完全關閉。 –