Android在按钮点击中获取位置
Posted
技术标签:
【中文标题】Android在按钮点击中获取位置【英文标题】:Android get Location in Button click 【发布时间】:2017-08-10 23:05:15 【问题描述】:我尝试在按钮单击中获取位置。我写了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)
我可以使用LocationListener
检查纬度和经度,但是如何在按钮单击中获取当前位置?
【问题讨论】:
您可以将纬度和经度设为全局 ....因此您可以在任何其他功能中使用它... 怎么样? @rafsanahmad007 最好的方法是使用回调,在获取位置时通知您;有时,您不能只期望在需要时得到它——比如从快餐店订购 【参考方案1】:你从LocationManager::getLastKnownLocation()得到它
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
【讨论】:
【参考方案2】:这是我的代码,它适用于我的应用程序。您可以尝试获取位置。
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();
【讨论】:
以上是关于Android在按钮点击中获取位置的主要内容,如果未能解决你的问题,请参考以下文章
如何获取某个国家/地区的位置点取决于 Android 手机配置