【发布时间】:2011-08-09 10:41:45
【问题描述】:
我正在尝试使用 gps 或网络在编辑框中单击按钮来检索我当前的位置..我已经尝试了我在教程和旧帖子中找到的所有可能的方法..但是我的位置始终为空.. 我没有明白我错在哪里。? 我正在有网络但没有互联网/gprs 的真实设备上对其进行测试...
这是我正在使用的代码..我也尝试了与 GPS_PROVIDER 相同的代码..请帮助我..
protected void showCurrentLocation()
{
geocoder = new Geocoder(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationListener()
);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
System.out.println("loc.."+location);
if (location != null)
{
String message = String.format("Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(ShowActivity.this, message,
Toast.LENGTH_LONG).show();
//acTextView.setText(message);
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10>
for (Address address : addresses) {
System.out.println("my location .."+address.getAddressLine(0));
acTextView.setText(address.getAddressLine(0));
}
} catch (IOException e) {
Log.e("LocateMe", "Could not get Geocoder data", e);
}
}
else
{
AlertDialog.Builder alertbox1 = new AlertDialog.Builder(this);
alertbox1.setMessage("No GPS or network ..Signal please fill the location manually!");
alertbox1.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1)
{}});
alertbox1.show();
}
}
我只是在编辑问题而不是问新问题..因为我的问题仍然与相同的问题有关...我没有更改代码中的任何内容,现在它工作得很好.....但问题是当我使用 GPS_PROVIDER 时,它会返回准确的地址以及经度和纬度的值。但是当我使用 NETWORK_PROVIDER 时,它只返回经度和纬度的值,没有地址……我想检索完整的使用 NETWORK_PROVIDER 的地址,因为 GPS 不能在室内工作......我该怎么做?
谢谢...!!
【问题讨论】:
-
你在模拟器上运行代码吗???那么你在 lastKnownLocaton 中总是会得到 null ......所以从开始尝试默认值或使用共享首选项。
-
no..我在问题中提到“我正在有网络但没有互联网/gprs的真实设备上测试它......”
-
Jainal 回答后你还有问题吗??
-
是的..我已经添加了所有必需的权限..
-
你能告诉我为什么我的 gps 的 getLastKnownLocation 总是返回 null 吗?你有没有得到任何解决方案?请回复我是否自 3 周以来一直被此问题困扰。
标签: android geolocation geocoding locationmanager