郵便番号の配列をループして、ターゲットに最も近い郵便番号を見つける方法を見つけようとして立ち往生しています。郵便番号には、ターゲットまでの距離を計算するために使用される緯度と経度も含まれています。最も近い距離を格納する配列をループしてから、最も近い距離の郵便番号を返す方法を理解する必要があります。考えられることはすべて試したので、どんな助けも素晴らしいでしょう。
* Finds and returns the zip code of a postal zone in the collection
* whose centroid is closest to a given location.
*
* @param target the target location.
*
* @return returns zipCode of the postal zone whose centroid is closest;
* returns COLLECTION_EMPTY if no zones are in the collection.
*/
public String findClosestZone(Location target)
{
int counter = 0;
String closeZip = COLLECTION_EMPTY;
double closestDistance = 100.0;
for (int i = 0; i < this.zoneCount; i++)
{
if (this.zones[i].getZoneLocation()
.calcDistance(target) < closestDistance)
{
closeZip = this.zones[i].getZoneZipCode();
closestDistance = this.zones[i]
.getZoneLocation().calcDistance(target);
counter++;
return closeZip;
}
}
return closeZip;
}