我开发了一个android应用程序,其中gps随时打开。现在我怎么能找到用户的经度和纬度。请帮助我如何查找GPS上的用户的经度和纬度
-1
A
回答
0
要通过位置提供者访问当前位置信息,我们需要使用android清单文件设置权限。
<manifest ... >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission. ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
当我们为我们的Android应用程序使用网络位置提供程序时,会使用ACCESS_COARSE_LOCATION。但是,ACCESS_FINE_LOCATION正在为这两个提供商提供权限。 INTERNET权限是必须为网络提供者使用的。
创建的LocationManager实例作为参考位置服务
对于任何背景的Android服务,我们需要使用它得到参考。同样,位置服务引用将使用getSystemService()方法创建。该引用将与新创建的LocationManager实例一起添加,如下所示。
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
请求当前位置从的LocationManager
创建位置服务参考后,位置更新使用requestLocationUpdates的LocationManager()方法请求。对于此功能,我们需要发送位置提供程序的类型,秒数,距离和要更新位置的LocationListener对象。
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
从LocationListener的上位置
LocationListener的将基于指定的距离间隔或数秒被通知的变化,接收位置更新。
本示例使用GPS提供程序提供当前位置更新。整个Android应用程序代码如下,在如下图所示
public class MainActivity extends Activity implements LocationListener{
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView txtLat;
String lat;
String provider;
protected String latitude,longitude;
protected boolean gps_enabled,network_enabled;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtLat = (TextView) findViewById(R.id.textview1);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
txtLat = (TextView) findViewById(R.id.textview1);
txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
}
@Override
public void onProviderDisabled(String provider) {
Log.d("Latitude","disable");
}
@Override
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude","status");
}
}
布局和Android清单XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
的
相关问题
- 1. 查找经度和纬度
- 2. 如何查找纬度和经度
- 3. 如何在不使用GPS的情况下每秒查找经度和纬度
- 4. 如何从WP7上的GPS获取经度和纬度?
- 5. 如何使用GPS纬度和经度计算海拔高度
- 6. 通过GPS获取纬度和经度
- 7. 比较当前GPS(经纬度)点保存GPS(经纬度)点?
- 8. 检查表格上的用户的经度和纬度
- 9. 如何使用纬度和经度查找最近的商店?
- 10. 使用用户当前的纬度,经度和查看对象的方向来查找经纬度集合
- 11. 如何从给定的经度和纬度查找左下角和右上角的经纬度?
- 12. 如何找到经度和纬度
- 13. 查找包含经度和纬度
- 14. 用户通过GPS获取纬度和经度值
- 15. 获取经度和纬度而不让用户打开GPS
- 16. 如何查找我的Windows Phone 7.5的纬度和经度?
- 17. 设置纬度,经度的GPS
- 18. 如何比较当前用户的纬度和经度与其他用户纬度和经度
- 19. 从GPS获取高度/纬度/经度
- 20. 显示gps按钮上的纬度和经度点击
- 21. 如何查找多个地址的经度和纬度
- 22. 如何查找距某点X距离的经度和纬度?
- 23. 如何从iOS中的经度和纬度查找地址?
- 24. 如何查找特定位置的经度和纬度?
- 25. 如何在xcode中查找mapview的经度和纬度?
- 26. 如何查找node.js中的经度和纬度?
- 27. 如何通过GPS和/或网络获取纬度和经度?
- 28. 如何使用jVector地图插件查找纬度和经度
- 29. 如何使用纬度和经度在iPhone中查找地址?
- 30. 如何使用google Api查找经度和纬度?
可能的复制[我如何获得当前GPS位置编程中的Android?( http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android) –