目前的国家/地区可以获得如下:
String locale = context.getResources().getConfiguration().locale.getCountry();
但是现在的城市呢?有办法实现吗? 如果手机无法实现,那么可能通过拨打外部服务?
答案 0 :(得分:0)
Add these lines in manifest :
<manifest ... >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission. ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
在您的代码中
LocationManager locationManager ;
String provider;
Geocoder geocoder;
List<Address> addresses;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting LocationManager object
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
// Creating an empty criteria object
Criteria criteria = new Criteria();
// Getting the name of the provider that meets the criteria
provider = locationManager.getBestProvider(criteria, false);
if(provider!=null && !provider.equals("")){
// Get the location from the given provider
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 1, this);
if(location!=null)
onLocationChanged(location);
else
Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
// Getting reference to TextView tv_longitude
TextView tvLongitude = (TextView)findViewById(R.id.tv_longitude);
// Getting reference to TextView tv_latitude
TextView tvLatitude = (TextView)findViewById(R.id.tv_latitude);
// Setting Current Longitude
tvLongitude.setText("Longitude:" + location.getLongitude());
// Setting Current Latitude
tvLatitude.setText("Latitude:" + location.getLatitude() );
geocoder = new Geocoder(this, Locale.getDefault());
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);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
答案 1 :(得分:0)
请参阅问题/答案How to get current location in Android
它使用 GPSTracker 类来帮助获取当前的纬度,经度,当前街道,城市,州和国家。
以下代码对您有帮助:
LocationManager location = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
if (location.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
GPSTracker gpsTracker = new GPSTracker(getActivity());
gpsTracker.canGetLocation();
Lat = gpsTracker.getLatitude() + "";
Lon = gpsTracker.getLongitude() + "";
if (gpsTracker.canGetLocation()) {
String country = gpsTracker.getCountryName(getActivity());
String city = gpsTracker.getLocality(getActivity());
String postalCode = gpsTracker.getPostalCode(getActivity());
String addressLine = gpsTracker.getAddressLine(getActivity());
current_location = addressLine + ", " + city + ", " + postalCode + ", " + country + "";
Log.d("country", country);
Log.d("city", city);
Log.d("postalCode", postalCode);
Log.d("addressLine", addressLine);
Log.d("current_location", current_location);
}
} else {
Toast.makeText(getActivity(), "Please enable GPS from setting...", Toast.LENGTH_LONG).show();
}