나는 안드로이드에서 도시 이름과 국가 이름을 주어진 위치로 가져 오려고하는데, 그 일을하는 데 어려움이 있습니다. 모든 경우에 gps, 인터넷 연결없이 또는 모든 기능이 켜져 있어야합니다. 내가 GeoCoder
시도했다하지만 난이 오류 :도시명 및 국가 이름을 위치로 가져 오기
Log.v("--", "got location ="+location.getLatitude()+" | "+location.getLongitude());
try {
Geocoder gcd = new Geocoder(Main.this, Locale.getDefault());
List<Address> addresses;
//im getting an error in this line and checked location values are OK
addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
Log.v("--", addresses.size()+" got location address size");
if (addresses.size() > 0) {
city = addresses.get(0).getLocality();
country = addresses.get(0).getCountryName();
prefs.edit().putString(Constants.COUNTRY, country).commit();
prefs.edit().putString(Constants.CITY, city).commit();
} else {
city = a.getString(R.string.app_name);
country = "France";
}
Log.v("--", "got location"+city+" | "+country);
}catch(Exception e){
Log.v("--", "got location failed");
e.printStackTrace();
}
그리고 MyLocation :
지오 코더가 내 MainActivity 여기에 위치하고 gotLocation에 내부에 위치한
11-29 11:54:01.718: W/System.err(32200): at com.myapp.Main$4.gotLocation(Main.java:282)
11-29 11:54:01.718: W/System.err(32200): at com.myapp.services.MyLocation$GetLastLocation.run(MyLocation.java:124)
내 코드입니다 수업 코드 :
public class MyLocation {
Timer timer1;
LocationManager lm;
LocationResult locationResult;
boolean gps_enabled = false;
boolean network_enabled = false;
private Context context;
public boolean getLocation(Context context, LocationResult result) {
// I use LocationResult callback class to pass location value from
// MyLocation to user code.
locationResult = result;
this.context = context;
if (lm == null)
lm = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
// exceptions will be thrown if provider is not permitted.
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = lm
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
// don't start listeners if no provider is enabled
if (!gps_enabled && !network_enabled)
return false;
if (gps_enabled)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListenerGps);
if (network_enabled)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
locationListenerNetwork);
timer1 = new Timer();
timer1.schedule(new GetLastLocation(), 20000);
return true;
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
class GetLastLocation extends TimerTask {
private Handler mHandler = new Handler(Looper.getMainLooper());
Location net_loc,gps_loc;
@Override
public void run() {
mHandler.post(new Runnable() {
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
net_loc = null; gps_loc = null;
if (gps_enabled)
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (network_enabled)
net_loc = lm
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// if there are both values use the latest one
if (gps_loc != null && net_loc != null) {
if (gps_loc.getTime() > net_loc.getTime())
locationResult.gotLocation(gps_loc);
else
locationResult.gotLocation(net_loc);
return;
}
if (gps_loc != null) {
locationResult.gotLocation(gps_loc);
timer1.notifyAll();
return;
}
if (net_loc != null) {
locationResult.gotLocation(net_loc);
return;
}
locationResult.gotLocation(null);
}
});
}
}
public void cancelTimer() {
timer1.cancel();
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
}
public static abstract class LocationResult {
public abstract void gotLocation(Location location);
}
}
누구나 할 수 있습니다. 이 문제를 해결하도록 도와 주시겠습니까?
여기에 위치 코드를 넣을 수 있습니까? –
@MoradiyaAkash가 MyLocation 클래스 코드 –
을 추가했는지 확인하십시오. https://developers.google.com/places/training/basic-place-search –