2016-09-14 1 views
0

위치 관리자 API를 사용하여 네트워크별로 현재 위치를 가져 오려고합니다.getLastKnownLocation (LocationManager.NETWORK_PROVIDER)는 위치 (1.0, 1.0)을 반환합니다.

내 코드 :

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

if (isNetworkEnabled) { 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return; 
    } 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, this); 
    Log.d("Network", "Network Enabled"); 

    if (locationManager != null) { 
     location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
     if (location != null) { 
      double latitude = location.getLatitude(); 
      double longitude = location.getLongitude(); 
     } 
    } 
} 

문제는 내가 가진 모두 위도와 경도는 1.0이다. lastknownLocation가 다른 공급자를 통해 위치를 가져 오는 데 시간이 걸립니다 알 수 GPS 또는 Network을 말한다면

답변

0

구글지도는 사용자의 lastKnownLocation 위치에 대한 사용한다. lastKnownLocation을 기본 위치로 사용하고 LocationListener로 업데이트하는 것이 좋습니다.

 if (ActivityCompat.checkSelfPermission 
       (this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED 
       && 
       ActivityCompat.checkSelfPermission 
         (this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) 
     { 
      requestPermissions(new String[]{ 
        Manifest.permission.ACCESS_COARSE_LOCATION, 
        Manifest.permission.ACCESS_FINE_LOCATION 
      }, 1); 

     } 
     else { 

      // enable location buttons 
      googleMap.setMyLocationEnabled(true); 
      googleMap.getUiSettings().setMyLocationButtonEnabled(true); 

      // fetch last location if any from provider - GPS. 
      final LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
      final Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      //if last known location is not available 
      if (loc == null) { 

       final LocationListener locationListener = new LocationListener() { 
        @Override 
        public void onLocationChanged(final Location location) { 

         // getting location of user 
         final double latitude = location.getLatitude(); 
         final double longitude = location.getLongitude(); 
         //do something with Lat and Lng 
        } 

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

        @Override 
        public void onProviderEnabled(String provider) { 
         //when user enables the GPS setting, this method is triggered. 
        } 

        @Override 
        public void onProviderDisabled(String provider) { 
         //when no provider is available in this case GPS provider, trigger your gpsDialog here. 
        } 
       }; 

       //update location every 10sec in 500m radius with both provider GPS and Network. 

      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10*1000, 500, locationListener); 
       locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 500, locationListener); 
      } 
      else { 
       //do something with last known location. 
       // getting location of user 
       final double latitude = loc.getLatitude(); 
       final double longitude = loc.getLongitude(); 
      } 
     } 

허가 처리 :

 @Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    switch (requestCode) { 

     case 1: 
      if (grantResults[0] != PackageManager.PERMISSION_GRANTED){ 
       //do something 
      } 
     default: 
      super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    } 
} 
관련 문제