我試圖在半徑內生成一個點,並且出現錯誤的值。有人介意看看並告訴我我在做什麼錯誤的經度?這是張貼在一個不同的問題一個公式化的方法...在半徑內隨機生成一個latlng會產生一個超出範圍的點
public static Location generateLocationWithinRadius(Location myCurrentLocation) {
return getLocationInLatLngRad(1000, myCurrentLocation);
}
protected static Location getLocationInLatLngRad(double radiusInMeters, Location currentLocation) {
double x0 = currentLocation.getLatitude();
double y0 = currentLocation.getLongitude();
Random random = new Random();
// Convert radius from meters to degrees
double radiusInDegrees = radiusInMeters/111000f;
double u = random.nextDouble();
double v = random.nextDouble();
double w = radiusInDegrees * Math.sqrt(u);
double t = 2 * Math.PI * v;
double x = w * Math.cos(t);
double y = w * Math.sin(t);
double new_x = x/Math.cos(y0);
double new_y = y/Math.cos(x0);
double foundLatitude;
double foundLongitude;
boolean shouldAddOrSubtractLat = random.nextBoolean();
boolean shouldAddOrSubtractLon = random.nextBoolean();
if (shouldAddOrSubtractLat) {
foundLatitude = new_x + x0;
} else {
foundLatitude = x0 - new_x;
}
if (shouldAddOrSubtractLon) {
foundLongitude = new_y + y0;
} else {
foundLongitude = y0 - new_y;
}
Location copy = new Location(currentLocation);
copy.setLatitude(foundLatitude);
copy.setLongitude(foundLongitude);
return copy;
}
我還應該說,由於某種原因,有效點產生座標的統一的行看着他們的時候。
我認爲緯度正確處理,而經度不正確。
顯示您的輸入,輸出和預期輸出。 – shmosel
我也應該說我現在正在使用靜態值,但我會保持它們可調整並減少指針的數量 – KoalaKoalified
lat lng在小數位數方面變化很大。因此,如果在半徑範圍內,您生成的隨機值在0.000x範圍內會有所不同。 –