2016-09-28 75 views
1

我已經計算出當前地點的經緯度。我現在想要的是獲得那個地方的位置。這裏是我的java代碼來計算經度和緯度。我採取了清單代碼的權限,我不包括這個Java代碼。如何通過計算經緯度得到位置

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    locationListener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      textView.append("\n " + location.getAltitude() + " " + location.getLongitude()); 
     } 

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

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      startActivity(intent); 
     } 
    }; 

    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_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, 
       Manifest.permission.INTERNET 
     }, 10); 
     return; 
    } else { 
     configureButton(); 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    switch (requestCode) { 
     case 10: 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
       configureButton(); 
      return; 
    } 
} 

private void configureButton() { 
    button.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      locationManager.requestLocationUpdates("gps", 5000, 0, locationListener); 
     } 
    }); 
} 
+2

請註明你是什麼意思下的 「那個地方的位置」?經度和緯度已經是該地點的一個位置。你想用指定的座標構造Location類的實例嗎? –

回答

0

這是我用來從緯度和經度獲取位置的代碼片段。

Geocoder geocoder; 
List<Address> addresses; 
geocoder = new Geocoder(this, Locale.getDefault()); 

addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5 

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 state = addresses.get(0).getAdminArea(); 
String country = addresses.get(0).getCountryName(); 
String postalCode = addresses.get(0).getPostalCode(); 
String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL 

或者你可以使用官方的文檔通過此鏈接獲得更多的自定義信息: https://developer.android.com/training/location/display-address.html

下面是相同的示例代碼:

http://www.androidauthority.com/get-location-address-android-app-628764/

1
Geocoder geocoder = new Geocoder(context, Locale.getDefault()); 
String result = null; 
try { 
     List<Address> addressList = geocoder.getFromLocation(
     latitude, longitude, 1); //use your lat, long value here 

     if (addressList != null && addressList.size() > 0) { 
      Address address = addressList.get(0); 
      StringBuilder sb = new StringBuilder(); 
      for (int i = 0; i < address.getMaxAddressLineIndex(); i++){ 
       sb.append(address.getAddressLine(i)).append("\n"); 
      } 
      sb.append(address.getLocality()).append("\n"); 
      sb.append(address.getPostalCode()).append("\n"); 
      sb.append(address.getCountryName()); 
      result = sb.toString(); 
      } 
    } catch (IOException e) { 
     Log.e(TAG, "Unable connect to Geocoder", e); 
} 

更多參考:

相關問題