2013-03-19 3 views
0

I 코드 :기기 안드로이드에서 현재 위치를 가져올 수없는 이유는 무엇입니까? 로

Criteria cri = new Criteria(); 
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    String tower = locationManager.getBestProvider(cri, false); 
    Location location = locationManager.getLastKnownLocation(tower); 

내 장치 안드로이드 와이파이 켜했다, GPS (인터넷이 가능하다)하지만, 위치 = null를 돌려줍니다. 왜? 사용

+0

에 다음과 같은 권한을 추가합니다. 아무것도 반환되지 않았습니다. –

+0

MuraliGanesan 대답을 참조하십시오. –

+0

탑에 대해 무엇을 얻습니까? GPS 또는 네트워크? –

답변

0

시도 :

Criteria cri = new Criteria(); 
cri.setAccuracy(Criteria.ACCURACY_FINE); 
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
String tower = locationManager.getBestProvider(cri, false); 
Location location = locationManager.getLastKnownLocation(tower); 
+0

코드를 추가했습니다. cri.setAccuracy (Criteria.ACCURACY_FINE); 그러나 위치는 여전히 null입니다. –

2

코드 아래에 현재 위치의 사용을 얻으려면, 이것을 시도.

public class MyLocationListener implements LocationListener { 

    public void onLocationChanged(Location loc) { 
     lati = loc.getLatitude(); 
     longi = loc.getLongitude(); 

    } 

    public void onProviderDisabled(String provider) { 

    } 

    public void onProviderEnabled(String provider) { 

    } 

    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 

    public MainActivity getLocationCordinates() { 
     MainActivity location_Gps = new MainActivity(); 
     return location_Gps; 
    } 
} 

String location; 
     LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
      location = LocationManager.PASSIVE_PROVIDER; 
     else 
      location = LocationManager.GPS_PROVIDER; 
     LocationListener mlocListener = new MyLocationListener(); 
     mlocManager.requestLocationUpdates(location, 0, 0, mlocListener); 
     mlocManager.requestLocationUpdates(location, 0, 1, mlocListener); 
     Location locate = mlocManager 
       .getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if (locate != null) { 
      lati = locate.getLatitude(); 
      longi = locate.getLongitude(); 

     } else { 
      locate = mlocManager 
        .getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); 
      if (locate != null) { 
       lati = locate.getLatitude(); 
       longi = locate.getLongitude(); 

      } 
     } 

MyLocationListener 클래스는 리스너를 구현하지 않은 당신의 manifest.xml

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
관련 문제