我想按当前位置查看城市名称,但我有纬度和经度。如何获得它?我用按钮点击然后我得到双倍经纬度值。我的代码在下面。请帮助我。
谢谢!
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();
}
}
});
我编辑了以下代码,但没有得到cityname请帮助我!!!
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();
}
}
});
但我在下面得到错误::::
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$1.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
答案 0 :(得分:63)
使用此:
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);
有关详细的Google地图示例的更多信息,请查看以下链接: http://www.demoadda.com/demo/android/load-googlemap_107
对于后台位置更新: http://www.demoadda.com/demo/android/download-android-background-location-update-service-demo_21
答案 1 :(得分:18)
在你的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());
答案 2 :(得分:9)
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();
}
答案 3 :(得分:2)
使用Geocoder。通过纬度和经度,获取城市名称和地址。
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
}
答案 4 :(得分:2)
使用它并将您的经纬度值传递给它。
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();
}
答案 5 :(得分:1)
对于我的国家/地区(越南)getLocality
将返回null,addresses.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);
答案 6 :(得分:0)
我只需要显示国家和城市名称,所以我正在使用以下代码
在这里,我使用“ 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();