我一直在试图弄清楚如何遍历一系列邮政编码并找到最接近目标的邮政编码。邮政编码还包含它们的纬度和经度,用于计算到目标的距离。我需要弄清楚如何遍历存储最近距离的数组,然后返回最近距离的邮政编码。任何帮助都会很棒,因为我已经尝试了我能想到的一切。
* 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;
}