휴대 기기에서 GPS 위도와 경도를 가져 오려고합니다. 아래의 코드는 작동합니다. 즉, 장치의 "무선 네트워크 사용"설정이 선택되어 있지만 "GPS 위성 사용"이 선택되어 있지 않은 경우 원하는 방식으로 모든 정보를 제공합니다. 정보.GPS로 위도와 경도 받기
이void getLatitudeAndLongitude() {
boolean gpsEnabled = false;
LocationManager mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Log.e("mLocation#####", "" + mLocMan);
try {
gpsEnabled = mLocMan
.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
boolean networkEnabled = false;
try {
networkEnabled = mLocMan
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
Location mCurrentLocation = null;
// network*****************
if (networkEnabled)
mCurrentLocation = mLocMan
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
// gps********************
if (gpsEnabled)
mCurrentLocation = mLocMan
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.d("current location", "" + mCurrentLocation);
LocationProvider mGpsProv = null;
if (mGpsProv == null && mLocMan != null) {
mGpsProv = mLocMan.getProvider(LocationManager.GPS_PROVIDER);
}
if (mLocMan != null) {
mylocationlistener mGpsLocListener = new mylocationlistener();
mLocMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
0 /* minTime ms */, 0 /* minDistance in meters */,
mGpsLocListener);
Log.d("Provoider1", "NETWORK_PROVIDER");
}
if (mLocMan != null && mGpsProv != null) {
mylocationlistener mGpsLocListener = new mylocationlistener();
mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,
0 /* minTime ms /, 0/minDistance in meters */, 0,
mGpsLocListener);
Log.d("Provoider2", "GPS_PROVIDER");
}
}
class mylocationlistener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
latitude = String.valueOf(location.getLatitude());
longitude = String.valueOf(location.getLongitude());
Log.e("location==", "" + location);
Log.e("getLatitude", location.getLatitude() + "");
Log.e("getLongitude", location.getLongitude() + "");
if (GlobalConfig.DEBUG)
Log.d("setUpIndividualWork", "location==" + location);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
}
}, 1000);
onGPSUpdate(location);
Geocoder gcd = new Geocoder(setUpIndividualWorkOut.this,
Locale.getDefault());
List<Address> addresses = null;
try {
addresses = gcd.getFromLocation(location.getLatitude(),
location.getLongitude(), 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses != null && addresses.size() > 0) {
userCurrentLocation = addresses.get(0).getLocality();
Log.e("current location by GPS ", ""
+ addresses.get(0).getLocality());
}
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
나는 당신의 응답을 매우 감사 할 것입니다 :
여기에 코드입니다.
당신이 이것을 시도 할 때 하늘에 대한 명확한 전망이 있습니까? GPS는 대개 내부에서 작동하지 않습니다. – Craigy
일부 'else'를 던집니다.'networkEnabled '와'gpsEnabled '가 모두'true '이면 해당 LastKnownLocation을 두 번 가져옵니다. 또한 mLocMan 및 mGpsProv가 둘 다 null 인 경우 해당 업데이트를 두 번 요청하는 것입니다. –