测试开发者手机上的 GPS 坐标
我的代码是在给定时间获取某个位置的坐标,然后显示纬度/经度坐标。我认为我的代码是正确的,但问题出在给手机授予 GPS 权限。我使用的是 Samsung Nexus S,并且安装了所有正确的驱动程序,但手机上没有网络。我被告知 GPS 应该仍然可以工作并且应该能够检索坐标。我启用了“使用 GPS 卫星”设置,并在清单中授予了权限,如清单.xml 中所示,
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
我输入了所有可能的位置权限,以防万一我丢失了某些内容,希望这不是一个问题。
至于我的代码,我在 onCreate 方法(类?我不确定它的正确名称)中有这个
double[] gps = getGPS();
TextView tv = (TextView) this.findViewById(R.id.gpsCoordinates);
tv.setText("Latitude: " + gps[0] + "\nLongitude: " + gps[1]);
,然后在 GPS 类中使用一个私有方法来实际获取坐标
private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
,我相信我从 stackoverflow 和一些代码中提取了这段代码其他论坛,然后稍微调整了一下。
My code is taking coordinates of a position at one given time, and then displaying Lat/Long coordinates. I think my code is right, but where the problem lies is giving GPS permission to the phone. I am using a Samsung Nexus S, and I have all proper drivers installed, but there is no network on the phone. I was told that GPS should still work and should be able to retrieve coordinates. I have the, "Use GPS Satellites" setting enabled, and have given the permission in the manifest, as seen here in the manifest.xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
I threw in all possible location permissions just in case I was missing something, hopefully that isn't a problem.
As for my code I have this in the onCreate method (class? I'm not sure of its proper name)
double[] gps = getGPS();
TextView tv = (TextView) this.findViewById(R.id.gpsCoordinates);
tv.setText("Latitude: " + gps[0] + "\nLongitude: " + gps[1]);
And then a private method in the GPS class to actually get the coordinates
private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
return gps;
I believe I pulled this code off of stackoverflow and a few other forums and then tweaked it a bit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加以上权限。也许这会对您有所帮助。
Add above permissions.May be this will help you.
您需要添加以下权限
Ref:
http://developer.android.com/reference/android/Manifest.permission .html#ACCESS_FINE_LOCATION
You need to put the following permission
Ref:
http://developer.android.com/reference/android/Manifest.permission.html#ACCESS_FINE_LOCATION