如何在android中通过纬度和经度获取城市名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22323974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Get City Name by Latitude &Longitude in android?
提问by crickpatel0024
I want to city name by current location but i have latitude and longitude.how to get it? i used button click then i get double value latitude and longitude. my code in below.please help me.
我想按当前位置命名城市名称,但我有纬度和经度。如何获取?我使用按钮单击然后我得到双值纬度和经度。我的代码在下面。请帮助我。
Thanks!!
谢谢!!
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();
}
}
});
I edited below code but not get cityname please help me !!!
我编辑了下面的代码但没有得到城市名称请帮助我!!!
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();
}
}
});
But I get Error in below::::
但我在下面收到错误::::
03-11 17:01:46.465: W/System.err(27587): java.io.IOException: Service not Available
03-11 17:01:46.465: W/System.err(27587): at android.location.Geocoder.getFromLocation(Geocoder.java:136)
03-11 17:01:46.465: W/System.err(27587): at com.example.gpstracking.AndroidGPSTrackingActivity.onClick(AndroidGPSTrackingActivity.java:81)
03-11 17:01:46.465: W/System.err(27587): at android.view.View.performClick(View.java:4191)
03-11 17:01:46.475: W/System.err(27587): at android.view.View$PerformClick.run(View.java:17229)
03-11 17:01:46.475: W/System.err(27587): at android.os.Handler.handleCallback(Handler.java:615)
03-11 17:01:46.475: W/System.err(27587): at android.os.Handler.dispatchMessage(Handler.java:92)
03-11 17:01:46.475: W/System.err(27587): at android.os.Looper.loop(Looper.java:137)
03-11 17:01:46.475: W/System.err(27587): at android.app.ActivityThread.main(ActivityThread.java:4960)
03-11 17:01:46.475: W/System.err(27587): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 17:01:46.475: W/System.err(27587): at java.lang.reflect.Method.invoke(Method.java:511)
03-11 17:01:46.475: W/System.err(27587): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
03-11 17:01:46.475: W/System.err(27587): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
03-11 17:01:46.475: W/System.err(27587): at dalvik.system.NativeStart.main(Native Method)
03-11 17:02:21.335: W/IInputConnectionWrapper(27587): getSelectedText on inactive InputConnection
03-11 17:02:21.335: W/IInputConnectionWrapper(27587): setComposingText on inactive InputConnection
03-11 17:02:21.425: W/IInputConnectionWrapper(27587): getExtractedText on inactive InputConnection
回答by Kishan Dhamat
Use This:
用这个:
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);
For more in detailed google map example you check the links below: http://www.demoadda.com/demo/android/load-googlemap_107
有关详细的谷歌地图示例的更多信息,请查看以下链接:http: //www.demoadda.com/demo/android/load-googlemap_107
And for the background location updates: http://www.demoadda.com/demo/android/download-android-background-location-update-service-demo_21
对于后台位置更新:http: //www.demoadda.com/demo/android/download-android-background-location-update-service-demo_21
回答by Mayank Saini
On your onClick add this:
在您的 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());
回答by Chandan Kumar Mishra
gps = new LocationAddress(getActivity());
if (gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
//List<Address> addresses =geocoder.getFromLocation(latitude, longitude, 1);
try {
List < Address > addresses = geocoder.getFromLocation(latitude, longitude, 1);
String address = addresses.get(0).getSubLocality();
String cityName = addresses.get(0).getLocality();
String stateName = addresses.get(0).getAdminArea();
txt_paddress.setText(address);
txt_city.setText(cityName);
txt_state.setText(stateName);
} catch (IOException e) {
e.printStackTrace();
}
} else {
gps.showSettingsAlert();
}
回答by Shadow
Use Geocoder. Pass the latitude and longitude and get city name and address.
使用地理编码器。通过纬度和经度并获得城市名称和地址。
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
}
回答by Marium Jawed
Use this and pass your lat long values to it.
使用它并将您的经纬度值传递给它。
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();
}
回答by Phan Van Linh
For my country (Vietnam) getLocality
will return null and addresses.get(0).getAddressLine(0)
will return the district not city
对于我的国家(越南)getLocality
将返回 nulladdresses.get(0).getAddressLine(0)
并将返回区而不是城市
So I will do like this
所以我会这样做
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);
回答by bir
I only need to show country and city name, so I am using following code
Here I am using "en" for always showing country and city name in English language instead of users regional language. Otherwise you can Locale.getDefault()
method.
我只需要显示国家和城市名称,所以我使用以下代码这里我使用“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();