내 앱에서 특정 위치가 지정된 지역에 있는지 여부를 알아야합니다. 나는 Connaught Place, New Delhi를 중심점으로 삼고 있습니다. 중심점에서 200 마일 떨어진 지역에 주소가 있습니다. 그러나 "abcdfdfkc"와 같은 잘못된 위치를 입력하면이 위치의 좌표를 찾으려고하기 때문에 앱이 다운되며이를 방지하려고합니다.Android : 잘못된 위치를 입력하는 중에 앱이 작동을 멈 춥니 다
내가 코드를 게시하고 아래 :
public static boolean isServicedLocation(Context _ctx, String strAddress){
boolean isServicedLocation = false;
Address sourceAddress = getAddress(_ctx, "Connaught Place, New Delhi, India");
Location sourceLocation = new Location("");
sourceLocation.setLatitude(sourceAddress.getLatitude());
sourceLocation.setLongitude(sourceAddress.getLongitude());
Address targetAddress = getAddress(_ctx, strAddress);
Location targetLocation = new Location("");
if (targetLocation != null) {
targetLocation.setLatitude(targetAddress.getLatitude());
targetLocation.setLongitude(targetAddress.getLongitude());
float distance = Math.abs(sourceLocation.distanceTo(targetLocation));
double distanceMiles = distance/1609.34;
isServicedLocation = distanceMiles <= 200;
//Toast.makeText(_ctx, "Distance "+distanceMiles, Toast.LENGTH_LONG).show();
}
return isServicedLocation;
}
getAddress에 방법 :
public static Address getAddress(Context _ctx, String addressStr) {
Geocoder geoCoder = new Geocoder(_ctx, Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName(addressStr,
1);
if (addresses.size() != 0) {
return addresses.get(0);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
로그에 무엇이 있습니까? –