gpt4 book ai didi

android - 当前位置始终为空

转载 作者:太空狗 更新时间:2023-10-29 16:23:54 24 4
gpt4 key购买 nike

我正在尝试使用 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 在室内不工作......我该怎么做?

谢谢...!!

最佳答案

请检查您的 list 文件。您是否添加了这些权限。

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>

查看更新后的答案:

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,
myLocationListener
);
timer = new Timer();
timer.schedule(new GetLastLocation(),20000);

}

class GetLastLocation extends TimerTask {

@Override
public void run() {
timer.cancel();
locationManager.removeUpdates(locationListener);
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();
}
}
return;
}
}

/** The location listener. */
LocationListener myLocationListener = new LocationListener() {

public void onLocationChanged(Location location) {

}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};

关于android - 当前位置始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6994904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com