Android 从获取的坐标中获取位置名称
我拥有的:目前我的应用程序只告诉我当前位置的坐标Android 从获取的坐标中获取位置名称,android,location,Android,Location,我拥有的:目前我的应用程序只告诉我当前位置的坐标 我想要什么:从gps获取的坐标中获取位置名称,这样我就可以知道我的确切位置。(位置名称)您可以使用android.location中提供的GeoCoder。GeoCoder软件包。这篇文章给了你充分的解释。可能的样本为美国 List<Address> list = geoCoder.getFromLocation(location .getLatitude(), location.getLongitu
我想要什么:从gps获取的坐标中获取位置名称,这样我就可以知道我的确切位置。(位置名称)您可以使用android.location中提供的
GeoCoder
。GeoCoder
软件包。这篇文章给了你充分的解释。可能的样本为美国
List<Address> list = geoCoder.getFromLocation(location
.getLatitude(), location.getLongitude(), 1);
if (list != null & list.size() > 0) {
Address address = list.get(0);
result = address.getLocality();
return result;
List List=geoCoder.getFromLocation(位置
.getLatitude(),location.getLatitude(),1);
if(list!=null&list.size()>0){
地址=list.get(0);
结果=address.getLocation();
返回结果;
结果
将返回位置的名称。在这里,我得到了一个单独的名称,只需在这个函数中传递纬度和经度,就可以得到与这个纬度和经度相关的所有信息
public void getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
GUIStatics.currentAddress = obj.getSubAdminArea() + ","
+ obj.getAdminArea();
GUIStatics.latitude = obj.getLatitude();
GUIStatics.longitude = obj.getLongitude();
GUIStatics.currentCity= obj.getSubAdminArea();
GUIStatics.currentState= obj.getAdminArea();
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
Log.v("IGA", "Address" + add);
// Toast.makeText(this, "Address=>" + add,
// Toast.LENGTH_SHORT).show();
// TennisAppActivity.showDialog(add);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
public-void-getAddress(双lat,双lng){
Geocoder Geocoder=新的Geocoder(HomeActivity.mContext,Locale.getDefault());
试一试{
列表地址=地理编码器。getFromLocation(lat,lng,1);
地址obj=地址。获取(0);
字符串add=obj.getAddressLine(0);
GUIStatics.currentAddress=obj.getSubAdminArea()+“,”
+obj.getAdminArea();
GUIStatics.latitude=obj.getLatitude();
GUIStatics.longitude=obj.getLongitude();
GUIStatics.currentCity=obj.getSubAdminArea();
GUIStatics.currentState=obj.getAdminArea();
add=add+“\n”+obj.getCountryName();
add=add+“\n”+obj.getCountryCode();
add=add+“\n”+obj.getAdminArea();
add=add+“\n”+obj.getPostalCode();
add=add+“\n”+obj.getSubAdminArea();
add=add+“\n”+obj.getLocation();
add=add+“\n”+obj.getSubthoroughe();
日志v(“IGA”、“地址”+添加);
//Toast.makeText(这个“Address=>”+add,
//吐司。长度(短)。show();
//TennisAppActivity.showDialog(添加);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
Toast.makeText(this,e.getMessage(),Toast.LENGTH_SHORT.show();
}
}
我希望您能找到答案的答案。以下是从获取long-lat到获取地址的完整代码:
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(new Criteria(), true);
Location locations = locationManager.getLastKnownLocation(provider);
List<String> providerList = locationManager.getAllProviders();
if(null!=locations && null!=providerList && providerList.size()>0){
double longitude = locations.getLongitude();
double latitude = locations.getLatitude();
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
if(null!=listAddresses&&listAddresses.size()>0){
String _Location = listAddresses.get(0).getAddressLine(0);
}
} catch (IOException e) {
e.printStackTrace();
}
}
LocationManager LocationManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
字符串提供程序=locationManager.getBestProvider(新条件(),true);
Location locations=locationManager.getLastKnownLocation(提供者);
List providerList=locationManager.getAllProviders();
如果(null!=位置&&null!=providerList&&providerList.size()>0){
double longitude=位置。getLongitude();
双纬度=位置。getLatitude();
Geocoder Geocoder=新的Geocoder(getApplicationContext(),Locale.getDefault());
试一试{
List listAddresses=geocoder.getFromLocation(纬度、经度,1);
if(null!=listAddresses&&listAddresses.size()>0){
字符串_Location=listAddresses.get(0).getAddressLine(0);
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
您在找这个“[given-a-lation-and-longitude-get-the-location-name][1]”[1]:看一看你的问题和我几个月前发布的问题是一样的。检查这个链接或检查下面我的答案…@Krishna是的,我看到了,但是给你的问题的答案不太容易,所以我决定把我的放进去。@Sam Quest。。在那里给出的答案我不太理解。有时这种反向地理编码技术ique不提供任何地址。因此,如何在每次需要时从lat long获取地址???是的,它有时不会返回地址。为此,您可以实施一种备份机制,例如当您没有获取地址时,您可以再次点击它以获取地址,或者您可以使用google api获取地址。使用此权限:我正在获取地址阳离子总是空的!我如何才能得到我的地方的确切位置,它给最大的城市值,我想在城市内的位置名称