2017-09-13 37 views
0

我已經嘗試了這麼多的代碼,找出當前城市但是沒有一個工作設備以及模擬器....他們給我的位置空如何找到當前城市

locationManager.requestLocationUpdates(
          LocationManager.NETWORK_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
        Log.d("Network", "Network"); 
        if (locationManager != null) { 

         if (ActivityCompat.checkSelfPermission(this, 
    Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
          // TODO: Consider calling 
          // ActivityCompat#requestPermissions 
          // here to request the missing permissions, and then overriding 
          // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
          //           int[] grantResults) 
          // to handle the case where the user grants the permission. See the documentation 
          // for ActivityCompat#requestPermissions for more details. 

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

請給出解決方案爲什麼這個代碼給我null。

回答

1
private String getCompleteAddressString(double LATITUDE, double LONGITUDE) { 
    String strAdd = ""; 
    Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
    try { 
     List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1); 
     String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex() 
     String city = addresses.get(0).getLocality(); 
     String postalCode = addresses.get(0).getPostalCode(); 
     String one3 = addresses.get(0).getSubLocality(); 
     if (addresses != null) { 
      Address returnedAddress = addresses.get(0); 
      StringBuilder strReturnedAddress = new StringBuilder(""); 
      for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) { 
       strReturnedAddress 
         .append(returnedAddress.getAddressLine(i)).append(
         "\n"); 
      } 
      strAdd = strReturnedAddress.toString(); 
      et_longitude.setText("" + LONGITUDE); 
      et_latitude.setText("" + LATITUDE); 
      et_street_no.setText(address); 
      // et_street_name.setText(one3); 
      et_surburb.setText(postalCode); 
      et_city.setText(city); 
     } else { 
      Log.e("", "My Current location address No Address returned!"); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     Log.e("", "My Current location address No Address returned!"); 
    } 
    return strAdd; 

} 
+0

et_city.setText(市)是當前的城市 –

+0

但朋友在此方法中,我需要的緯度和經度...在質疑我正在使用,獲得這個值。所以請嘗試解決這些問題。意味着如何找到經緯度。 –

0
mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    Location location = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
    Log.e("TAG","Lat : " + location.getLatitude()); 
    Log.e("TAG","Lng : " + location.getLongitude()); 

通過這種方式,你可以得到當前的經緯度

+0

獲取位置null :( –

+0

是否添加權限? –

+0

<使用權限android:name =「android.permission.ACCESS_COARSE_LOCATION」/> <使用權限android:name =「android.permission.ACCESS_FINE_LOCATION」/> <使用權限android:name =「android.permission.ACCESS_NETWORK_STATE」/> –