如何通过纬度和纬度获取城市名称;android中的经度?
我想按当前位置输入城市名称,但我有纬度和经度。如何获取?我用按钮点击,然后我得到两倍的纬度和经度值。我的代码在下面。请帮助我 谢谢如何通过纬度和纬度获取城市名称;android中的经度?,android,map,gps,location,Android,Map,Gps,Location,我想按当前位置输入城市名称,但我有纬度和经度。如何获取?我用按钮点击,然后我得到两倍的纬度和经度值。我的代码在下面。请帮助我 谢谢 Button btnShowLocation; GPSTracker gps; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.l
Button btnShowLocation;
GPSTracker gps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
// show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// create class object
gps = new GPSTracker(AndroidGPSTrackingActivity.this);
// check if GPS enabled
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
// \n is for new line
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
gps.showSettingsAlert();
}
}
});
我编辑了下面的代码,但没有得到城市名称,请帮助我
Button btnShowLocation;
GPSTracker gps;
String CityName;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
// show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// create class object
gps = new GPSTracker(AndroidGPSTrackingActivity.this);
// check if GPS enabled
if (gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Geocoder geocoder = new Geocoder(
AndroidGPSTrackingActivity.this, Locale
.getDefault());
List<Address> addresses;
try {
Log.v("log_tag", "latitude" + latitude);
Log.v("log_tag", "longitude" + longitude);
addresses = geocoder.getFromLocation(latitude,
longitude, 1);
Log.v("log_tag", "addresses+)_+++" + addresses);
CityName = addresses.get(0).getAddressLine(0);
Log.v("log_tag", "CityName" + CityName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// \n is for new line
Toast.makeText(
getApplicationContext(),
"Your Location is - \nLat: " + latitude
+ "\nLong: " + longitude, Toast.LENGTH_LONG)
.show();
} else {
gps.showSettingsAlert();
}
}
});
在onClick上添加以下内容:
Geocoder gcd = new Geocoder(AndroidGPSTrackingActivity.this, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0)
System.out.println(addresses.get(0).getLocality());
Geocoder gcd=new Geocoder(androidgpstrackactivity.this,Locale.getDefault());
列表地址=gcd.getFromLocation(纬度,经度,1);
如果(地址.size()>0)
System.out.println(addresses.get(0.getLocality());
使用地理编码器。通过纬度和经度,获得城市名称和地址
try {
Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault());
List < Address > addresses = geo.getFromLocation(latitude, longitude, 1);
if (addresses.isEmpty()) {
addres.setText("Waiting for Location");
} else {
if (addresses.size() > 0) {
addres.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() + ", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());
//Toast.makeText(getApplicationContext(), "Address:- " + addresses.get(0).getFeatureName() + addresses.get(0).getAdminArea() + addresses.get(0).getLocality(), Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
e.printStackTrace(); // getFromLocation() may sometimes fail
}
试试看{
Geocoder geo=新的Geocoder(this.getApplicationContext(),Locale.getDefault());
Listaddresses=geo.getFromLocation(纬度、经度,1);
if(addresses.isEmpty()){
地址:setText(“等待位置”);
}否则{
如果(地址.size()>0){
addres.setText(addresses.get(0).getFeatureName()+”,“+addresses.get(0).getLocality()+”,“+addresses.get(0).getAdminArea()+”,“+addresses.get(0).getCountryName());
//Toast.makeText(getApplicationContext(),“Address:-”+addresses.get(0).getFeatureName()+addresses.get(0).getAdminArea()+addresses.get(0).getLocality(),Toast.LENGTH_LONG).show();
}
}
}捕获(例外e){
e、 printStackTrace();//getFromLocation()有时可能会失败
}
使用以下方法:
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(MyLat, MyLong, 1);
String cityName = addresses.get(0).getAddressLine(0);
String stateName = addresses.get(0).getAddressLine(1);
String countryName = addresses.get(0).getAddressLine(2);
Geocoder-Geocoder=new-Geocoder(这个,Locale.getDefault());
名单
对于后台位置更新:
gps=新位置地址(getActivity());
if(gps.canGetLocation()){
双纬度=gps.getLatitude();
double longitude=gps.getLongitude();
Geocoder Geocoder=新的地理编码器(getActivity(),Locale.getDefault());
//列表地址=geocoder.getFromLocation(纬度,经度,1);
试一试{
Listaddresses=geocoder.getFromLocation(纬度、经度,1);
字符串地址=地址。get(0)。getSubLocality();
字符串cityName=addresses.get(0.getLocality();
字符串stateName=addresses.get(0.getAdminArea();
txt_paddress.setText(地址);
txt_city.setText(cityName);
txt_state.setText(stateName);
}捕获(IOE异常){
e、 printStackTrace();
}
}否则{
gps.showSettingsAlert();
}
对于我的国家(越南)getLocation
将返回null和地址。get(0)。getAddressLine(0)
将返回地区而不是城市
所以我会这样做
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
int maxAddressLine = addresses.get(0).getMaxAddressLineIndex();
String countryName = addresses.get(0).getAddressLine(maxAddressLine);
String stateName = addresses.get(0).getAddressLine(maxAddressLine-1);
String cityName = addresses.get(0).getAddressLine(maxAddressLine-2);
Geocoder-Geocoder=new-Geocoder(这个,Locale.getDefault());
列表地址=geocoder.getFromLocation(纬度,经度,1);
int maxAddressLine=addresses.get(0.getMaxAddressLineIndex();
字符串countryName=addresses.get(0).getAddressLine(maxAddressLine);
字符串stateName=addresses.get(0).getAddressLine(maxAddressLine-1);
字符串cityName=addresses.get(0).getAddressLine(maxAddressLine-2);
我只需要显示国家和城市名称,所以我使用以下代码
在这里,我用“en”来表示国家和城市的名称,而不是用户的区域语言。否则,您可以Locale.getDefault()
方法
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Locale locale = new Locale("en"); //OR Locale.getDefault()
Geocoder geocoder = new Geocoder(MainActivity.this, locale);
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
updatedCity = addresses.get(0).getLocality();
if (updatedCity == null) {
updatedCity = addresses.get(0).getAdminArea();
}
updatedCountry = addresses.get(0).getCountryName();
double latitude=location.getLatitude();
double longitude=location.getLongitude();
语言环境=新语言环境(“en”)//或Locale.getDefault()
Geocoder Geocoder=新的Geocoder(MainActivity.this,locale);
列表地址=geocoder.getFromLocation(纬度,经度,1);
updatedCity=addresses.get(0.GetLocation();
如果(updatedCity==null){
updatedCity=addresses.get(0.getAdminArea();
}
updatedCountry=addresses.get(0.getCountryName();
使用此选项并将lat long值传递给它
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(getContext(), Locale.getDefault());
try {
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();else return NULL
} catch (IOException e) {
e.printStackTrace();
}
Geocoder-Geocoder;
列出地址;
geocoder=新的geocoder(getContext(),Locale.getDefault());
试一试{
addresses=geocoder.getFromLocation(纬度,经度,1);//这里1表示返回的最大位置结果,由它建议的1到5个文档表示
String address=addresses.get(0).getAddressLine(0);//如果存在除此之外的任何其他地址行,请通过getMaxAddressLineIndex()检查最大可用地址行
字符串city=addresses.get(0.getLocation();
字符串状态=addresses.get(0.getAdminArea();
字符串country=addresses.get(0).getCountryName();否则返回NULL
}捕获(IOE异常){
e、 printStackTrace();
}
查看文档:参考[1]问题及其答案[1]:记录纬度和经度,并检查它们是否为0。我认为@Mayank Saini的答案更适合了解细节。地址行最终可能包含的信息比需要的多(如子位置)addresses.get(0).getCountryName()`也有助于获取国家名称。如果您只需要城市名称,则应使用addresses.get(0).getLocation()
,addresses.get(0).getAddressLine(0)
可以根据地址为您的城市名称提供更多信息(请阅读Geocoder
文档)。但是当地人给你的城市名称是准确的。:)不要使用Geocoder。它会在很多设备上抛出异常。@kiShan它收到了问题ni一些设备找不到吗?你能帮我个忙吗..有时候地理编码器会返回空值。您可以检查结果,然后再次致电查询结果。一些解释会很好,请编辑您的答案
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Locale locale = new Locale("en"); //OR Locale.getDefault()
Geocoder geocoder = new Geocoder(MainActivity.this, locale);
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
updatedCity = addresses.get(0).getLocality();
if (updatedCity == null) {
updatedCity = addresses.get(0).getAdminArea();
}
updatedCountry = addresses.get(0).getCountryName();
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(getContext(), Locale.getDefault());
try {
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();else return NULL
} catch (IOException e) {
e.printStackTrace();
}