本文整理汇总了Java中android.location.Address.getLocality方法的典型用法代码示例。如果您正苦于以下问题:Java Address.getLocality方法的具体用法?Java Address.getLocality怎么用?Java Address.getLocality使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.location.Address
的用法示例。
在下文中一共展示了Address.getLocality方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: formatAddress
import android.location.Address; //导入方法依赖的package包/类
@NonNull
private static String formatAddress(@NonNull Address address) {
String addressText = "";
String streetAndNumber = address.getAddressLine(0);
if (!(streetAndNumber == null || streetAndNumber.isEmpty())) {
addressText += streetAndNumber;
}
String locality = address.getLocality();
if (locality != null) {
if (!(addressText.isEmpty() || addressText.trim().endsWith(","))) {
addressText += ", ";
}
addressText += locality;
}
return addressText;
}
示例2: getLocalityAdminForAddress
import android.location.Address; //导入方法依赖的package包/类
private String getLocalityAdminForAddress(final Address addr, final boolean approxLocation) {
if (addr == null)
return "";
String localityAdminStr = addr.getLocality();
if (localityAdminStr != null && !("null".equals(localityAdminStr))) {
if (approxLocation) {
// TODO: Uncomment these lines as soon as we may translations
// for Res.string.around.
// localityAdminStr =
// mContext.getResources().getString(Res.string.around) + " " +
// localityAdminStr;
}
String adminArea = addr.getAdminArea();
if (adminArea != null && adminArea.length() > 0) {
localityAdminStr += ", " + adminArea;
}
return localityAdminStr;
}
return null;
}
示例3: getLocality
import android.location.Address; //导入方法依赖的package包/类
/**
* Try to get Locality
* @return null or locality
*/
public String getLocality(Context context)
{
List<Address> addresses = getGeocoderAddress(context);
if (addresses != null && addresses.size() > 0)
{
Address address = addresses.get(0);
String locality = address.getLocality();
return locality;
}
else
{
return null;
}
}
示例4: getLocalityNameEng
import android.location.Address; //导入方法依赖的package包/类
public static String getLocalityNameEng(Context context, LatLng latLng) {
Geocoder gcd = new Geocoder(context, Locale.US);
try {
List<Address> addresses = gcd.getFromLocation(latLng.latitude, latLng.longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
String name = address.getCountryName();
if (address.getLocality() != null)
name = address.getLocality();
return name;
}
} catch (IOException ex) {
Log.d(TAG, Log.getStackTraceString(ex));
}
return null;
}
示例5: getLocalityName
import android.location.Address; //导入方法依赖的package包/类
@Nullable
public static String getLocalityName(Context context, LatLng latLng) {
Geocoder gcd = new Geocoder(context, Locale.getDefault());
try {
List<Address> addresses = gcd.getFromLocation(latLng.latitude, latLng.longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
String name = address.getCountryName();
if (address.getLocality() != null)
name = address.getLocality() + ", " + name;
return name;
}
} catch (IOException ex) {
Log.d(TAG, Log.getStackTraceString(ex));
}
return null;
}
示例6: onPostExecute
import android.location.Address; //导入方法依赖的package包/类
@Override
protected void onPostExecute(Address address) {
if (address == null)
return;
StringBuilder builder = new StringBuilder();
if (address.getMaxAddressLineIndex() >= 0)
builder.append(address.getAddressLine(0));
if (address.getLocality() != null) {
builder.append(", ");
builder.append(address.getLocality());
}
locationString = builder.toString();
Intent intent = new Intent(LOCATION_CHANGED_ACTION);
intent.putExtra(EXTRA_LOCATION, location);
intent.putExtra(EXTRA_GEOCODE, locationString);
localBroadcastManager.sendBroadcast(intent);
}
示例7: getSimplifiedAddress
import android.location.Address; //导入方法依赖的package包/类
/**
* @param latitude latitude of address
* @param longitude longitude of address
* @return simplified address of location
*/
public String getSimplifiedAddress(double latitude, double longitude) {
String location = "";
try {
Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
String admin = address.getAdminArea();
String subLocality = address.getSubLocality();
String locality = address.getLocality();
if (admin.length() > 10) {
admin = admin.substring(0, 10) + "..";
}
if (locality != null && subLocality != null) {
location = subLocality + "," + locality;
} else if (subLocality != null) {
location = subLocality + "," + admin;
} else {
location = locality + "," + admin;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return location;
}
示例8: getCompleteAddress
import android.location.Address; //导入方法依赖的package包/类
/**
* @param latitude latitude of address
* @param longitude longitude of address
* @return complete address of location
*/
public String getCompleteAddress(double latitude, double longitude) {
String location = "";
try {
Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
String state, city, zip, street;
if (address.getAdminArea() != null) {
state = address.getAdminArea();
} else {
state = "";
}
if (address.getLocality() != null) {
city = address.getLocality();
} else {
city = "";
}
if (address.getPostalCode() != null) {
zip = address.getPostalCode();
} else {
zip = "";
}
if (address.getThoroughfare() != null) {
street = address.getSubLocality() + "," + address.getThoroughfare();
} else {
street = address.getSubLocality() + "," + address.getFeatureName();
}
location = street + "," + city + "," + zip + "," + state;
}
} catch (IOException e) {
e.printStackTrace();
}
return location;
}
示例9: locationToAddress
import android.location.Address; //导入方法依赖的package包/类
public boolean locationToAddress(double latitude, double longitude) {
Geocoder geocoder = new Geocoder(this.context, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
locationInfo.clear();
String city = returnedAddress.getAdminArea();
if(city==null) city = returnedAddress.getLocality();
locationInfo.put("country", returnedAddress.getCountryName());
locationInfo.put("countrycode", returnedAddress.getCountryCode().toLowerCase());
locationInfo.put("latitude", ""+returnedAddress.getLatitude());
locationInfo.put("longitude", ""+returnedAddress.getLongitude());
if(city!=null)locationInfo.put("city", ""+city);
String address = "";
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
address = address + returnedAddress.getAddressLine(i) + "\n";
}
locationInfo.put("address", address);
return true;
} else {
}
} catch (Exception e) {
}
return false;
}
示例10: getAddressHTMLText
import android.location.Address; //导入方法依赖的package包/类
@android.support.annotation.NonNull
public String getAddressHTMLText(Address addressObj) {
String address = addressObj.getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addressObj.getLocality();
String state = addressObj.getAdminArea();
String country = addressObj.getCountryName();
String postalCode = addressObj.getPostalCode();
String part1 = addressObj.getFeatureName();
String part2 = address + ", " + city + ", " + state + ", " + country + ", " + postalCode;
return "<b>" + part1 + "</b><label style='color:#ccc'> <br>" + part2 + "</label>";
}
示例11: getAddressString
import android.location.Address; //导入方法依赖的package包/类
@android.support.annotation.NonNull
public String getAddressString(Address addressObj) {
String address = addressObj.getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addressObj.getLocality();
String state = addressObj.getAdminArea();
String country = addressObj.getCountryName();
String postalCode = addressObj.getPostalCode();
return address + ", " + city + ", " + state + ", " + country + ", " + postalCode;
}
示例12: getLocality
import android.location.Address; //导入方法依赖的package包/类
/**
* 根据经纬度获取所在地
*
* @param latitude 纬度
* @param longitude 经度
* @return 所在地
*/
public static String getLocality(double latitude, double longitude) {
Address address = getAddress(latitude, longitude);
return address == null ? "unknown" : address.getLocality();
}