Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android应用程序上获取当前位置有什么不对_Java_Android_Gps_Location - Fatal编程技术网

Java 在Android应用程序上获取当前位置有什么不对

Java 在Android应用程序上获取当前位置有什么不对,java,android,gps,location,Java,Android,Gps,Location,我正试图通过GPS获取我当前的位置,但似乎没有任何效果,我在这里回答了很多问题,但没有什么结果 这是我的密码: private GeoPoint getActualPosition() { LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, th

我正试图通过GPS获取我当前的位置,但似乎没有任何效果,我在这里回答了很多问题,但没有什么结果

这是我的密码:

private GeoPoint getActualPosition()
{
    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

    Location current = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    actualLat = (int)(current.getLatitude()*1E6);
    actualLong = (int)(current.getLongitude()*1E6);

    GeoPoint point = new GeoPoint(actualLat, actualLong);

    return point;
}
现在,该方法的“this”部分(在requestLocationUpdates上)引用了实现LocationListener接口的活动,其中其onLocationChanged方法是:

@Override
public void onLocationChanged(Location location) {
    actualLat = (int)(location.getLatitude()*1E6);
    actualLong = (int)(location.getLongitude()*1E6);

}
我的问题是在当前位置行,其中“current”总是,总是空的,我找不到原因

我的清单具有所需的权限:

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

所以我真的不知道我的代码出了什么问题

谢谢你抽出时间

getLastKnownLocation()
返回最后一个缓存位置。如果没有这样的位置将返回null(例如,如果手机已关闭),如果提供商当前处于禁用状态,则返回null

获得GPS定位需要时间
onLocationChanged()
只有在得到正确的修复后才会被调用,这段时间可能从20秒到几分钟不等

只有在至少调用一次
onLocationChanged()
之后,
getLastKnownLocation()
才会返回最后一个有效位置。

getLastKnownLocation()
返回最后一个缓存位置。如果没有这样的位置将返回null(例如,如果手机已关闭),如果提供商当前处于禁用状态,则返回null

获得GPS定位需要时间
onLocationChanged()
只有在得到正确的修复后才会被调用,这段时间可能从20秒到几分钟不等


只有在至少调用一次
onLocationChanged()
之后,
getLastKnownLocation()
才会返回最后一个有效位置。

根据以下文档:

如果提供程序当前已禁用,则返回null


因此,如果您在测试此功能的设备上没有启用GPS,您将得到null。如果您使用的是android emulator,请参见本节了解如何模拟特定位置。

根据以下文档:

如果提供程序当前已禁用,则返回null


因此,如果您在测试此功能的设备上没有启用GPS,您将得到null。如果您使用的是android emulator,请参阅本节了解如何模拟特定位置。

您是在模拟器或物理设备上运行应用程序/代码?如果它是一个模拟器,并且您以前从未设置过位置(在eclipse中使用DDMS视图),那么该位置将为空


希望这有帮助。

您是在模拟器或物理设备上运行应用程序/代码?如果它是一个模拟器,并且您以前从未设置过位置(在eclipse中使用DDMS视图),那么该位置将为空


希望这有帮助。

欢迎来到stackoverflow。如果你觉得某个答案有帮助,请投票表决。如果你发现某个答案是正确的,请接受它。欢迎来到stackoverflow。如果你觉得某个答案有帮助,请投票表决。如果你发现某个答案是正确的,请接受它。