Android在按钮点击中获取位置
我尝试在按钮单击中获取位置。我写了Android在按钮点击中获取位置,android,android-location,android-gps,Android,Android Location,Android Gps,我尝试在按钮单击中获取位置。我写了LocationListener方法,一切都很完美。这是我的源代码 private class MyLocationListener implements LocationListener { @Override public void onLocationChanged(Location loc) { editLocation.setText(""); pb.setVisibility(View.INVISIB
LocationListener
方法,一切都很完美。这是我的源代码
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
editLocation.setText("");
pb.setVisibility(View.INVISIBLE);
Toast.makeText(
getBaseContext(),
"Location changed: Lat: " + loc.getLatitude() + " Lng: "
+ loc.getLongitude(), Toast.LENGTH_SHORT).show();
String longitude = "Longitude: " + loc.getLongitude();
Log.v(TAG, longitude);
String latitude = "Latitude: " + loc.getLatitude();
Log.v(TAG, latitude);
/*------- To get city name from coordinates -------- */
String cityName = null;
Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(loc.getLatitude(),
loc.getLongitude(), 1);
if (addresses.size() > 0) {
System.out.println(addresses.get(0).getLocality());
cityName = addresses.get(0).getLocality();
}
}
catch (IOException e) {
e.printStackTrace();
}
String s = longitude + "\n" + latitude + "\n\nMy Current City is: "
+ cityName;
editLocation.setText(s);
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
私有类MyLocationListener实现LocationListener{
@凌驾
位置更改后的公共无效(位置loc){
editLocation.setText(“”);
pb.setVisibility(视图不可见);
Toast.makeText(
getBaseContext(),
位置已更改:纬度:“+loc.getLatitude()+”液化天然气:
+loc.getLongitude(),Toast.LENGTH_SHORT).show();
String longitude=“longitude:+loc.getLongitude();
Log.v(标签、经度);
String latitude=“latitude:+loc.getLatitude();
对数v(标签、纬度);
/*-------要从坐标中获取城市名称--------*/
字符串cityName=null;
Geocoder gcd=新的地理编码器(getBaseContext(),Locale.getDefault());
列出地址;
试一试{
地址=gcd.getFromLocation(loc.getLatitude(),
loc.getLongitude(),1);
如果(地址.size()>0){
System.out.println(addresses.get(0.getLocality());
cityName=addresses.get(0.getLocality();
}
}
捕获(IOE异常){
e、 printStackTrace();
}
字符串s=经度+“\n”+纬度+“\n\n我当前的城市是:”
+城市名称;
editLocation.setText;
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){}
@凌驾
公共无效onProviderEnabled(字符串提供程序){}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){}
}
我可以使用LocationListener
检查纬度和经度,但如何在按钮单击中获取当前位置 您是从
这是我的代码,适用于我的应用程序。您可以尝试获取该位置
ro_gps_icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(RODetailsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(RODetailsActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
} locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, (LocationListener) RODetailsActivity.this);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location == null) {
Toast.makeText(getApplicationContext(), "GPS signal not found", Toast.LENGTH_SHORT).show();
}
if (location != null) {
Log.e("locatin", "location--" + location);
Log.e("latitude at beginning",
"@@@@@@@@@@@@@@@" + location.getLatitude());
onLocationChanged(location);
}
}
});
以及获取数据的方法
public void onLocationChanged(Location location) {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Log.e("latitude", "latitude--" + latitude);
try {
Log.e("latitude", "inside latitude--" + latitude);
addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses != null && addresses.size() > 0) {
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();
ro_gps_location.setText(state + " , " + city + " , " + country);
ro_address.setText(address + " , " + knownName + " , " + postalCode);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onLocation已更改(位置){
地理编码器;
列出地址;
geocoder=新的geocoder(这个,Locale.getDefault());
双纬度=location.getLatitude();
double longitude=location.getLongitude();
Log.e(“纬度”、“纬度--”+纬度);
试一试{
Log.e(“纬度”,“内纬度--”+纬度);
地址=地理编码器.getFromLocation(纬度,经度,1);
if(addresses!=null&&addresses.size()>0){
字符串地址=地址.get(0).getAddressLine(0);
字符串city=addresses.get(0.getLocation();
字符串状态=addresses.get(0.getAdminArea();
字符串country=addresses.get(0.getCountryName();
字符串postalCode=addresses.get(0.getPostalCode();
字符串knownName=addresses.get(0.getFeatureName();
ro_gps_location.setText(州+”、“+城市+”、“+国家);
ro_address.setText(地址+”,“+knownName+”,“+postalCode);
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
您可以将纬度和经度设置为全局……这样您就可以在任何其他功能中使用它……如何@Rafsanahmad007最好的方法是使用回调,在获取位置时会通知您;有时候你不能期望在你需要它的时候就得到它——就像在快餐店点餐一样
public void onLocationChanged(Location location) {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Log.e("latitude", "latitude--" + latitude);
try {
Log.e("latitude", "inside latitude--" + latitude);
addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses != null && addresses.size() > 0) {
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();
ro_gps_location.setText(state + " , " + city + " , " + country);
ro_address.setText(address + " , " + knownName + " , " + postalCode);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}