저는 Android 개발을 처음 사용합니다. 다음은 Geocoder를 사용하여 현재 위치의 도시 이름을 가져 오는 방법에 대한 내 코드이며 null을 반환합니다.
private void updateCurrentLocation (위치 위치) {double lat = 0.0, lng = 0.0;
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
Log.i("tag", "Latitute is" + lat + ", Longtitute is" + lng);
} else {
City_Name = "Unavailable";
}
List<Address> list = null;
Geocoder geocoder = new Geocoder(this.getActivity());
try {
list = geocoder.getFromLocation(lat, lng, 1);
} catch (IOException e) {
e.printStackTrace();
}
//may provide multiple locations.
if (list != null && list.size() > 0) {
Address address = list.get(0);
City_Name = address.getLocality();
}
Log.i("Try", "CityName:" + City_Name);
//send empty message
handler.sendEmptyMessage(1);
}
GPS 서비스를 열고 이미 Manifest에 ACCESS_FINE_LOCATION 및 INTERNET 권한을 추가했습니다. 또한 Geocoder가 null을 반환한다는 Stackoverflow에서 비슷한 질문을 검색했지만 유용한 솔루션을 찾지 못했습니다. 그중 하나는 Geocoder 웹 사이트에서 JSON을 분석하는 것이지만 작동하지 않습니다.
누구든지 이것을 도울 수 있습니까? 감사합니다! BTW, 도시 이름을받는 더 좋은 해결책이 있습니까? 감사합니다!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다