我想在Android应用程序中找到我当前位置的纬度和经度.当我用手机稳定时,我只需要它.
我的代码是:
LocationManager locationManager;
locationManager = (LocationManager)getSystemService
(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation
(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,0,0,(LocationListener) this);
updateWithNewLocation(location);
}
private void updateWithNewLocation(Location location) {
TextView myLocationText = (TextView)findViewById(R.id.myLocationText);
String latLongString;
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
} else {
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" +
latLongString);
}
Run Code Online (Sandbox Code Playgroud)
在模拟器上运行后,我总是找到"找不到位置".为什么会这样?
我需要获取设备当前的地理位置.
场景:如果我在US...:
my device location should be US
Run Code Online (Sandbox Code Playgroud)
与案件相同,当我在 UK:
my device location should be UK
Run Code Online (Sandbox Code Playgroud)
我需要示例代码来查找此地理位置.(location determined by Network Location Provider)
我写了以下代码,返回我当前的位置地址.但是只有手动打开我的Wifi才能获得这些值.但我想打开Gps并从GPS获取我的地址,而不是从Wifi.有人请告诉我这段代码中要修改的是什么.以下是用于查找当前地址的代码.提前致谢.
public String myloc()
{
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(criteria, true);
// Update the GUI with the last known
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null)
{
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
Geocoder gc = new Geocoder(this, Locale.getDefault());
try
{
List<Address> addresses = gc.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0)
{
Address address = …Run Code Online (Sandbox Code Playgroud)