android获取经纬度3种方法

APP中可能会遇到一种需求,就是将当前所在位置的坐标传到服务器上,今天我提供三种途径去获取经纬度坐标信息,第一种是通过Android API来实现,第二种通过百度地图API来实现,第三种通过天地图API来实现。

第一种方法(Android API实现),废话不多说,上代码。

MainActivity代码如下:

001. public class MainActivity extends Activity {
002. private static final String TAG = MainActivity.class.getSimpleName();
003. private double latitude = 0.0;
004. private double longitude = 0.0;
005. private TextView info;
006. private LocationManager locationManager;
007.  
008. @Override
009. protected void onCreate(Bundle savedInstanceState) {
010. super.onCreate(savedInstanceState);
011. setContentView(R.layout.main);
012. info = (TextView) findViewById(R.id.tv);
013. locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
014. if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
015. getLocation();
016. //gps已打开
017. else {
018. toggleGPS();
019. new Handler() {
020. }.postDelayed(new Runnable() {
021. @Override
022. public void run() {
023. getLocation();
024. }
025. }, 2000);
026.  
027. }
028. }
029.  
030. private void toggleGPS() {
031. Intent gpsIntent = new Intent();
032. gpsIntent.setClassName("com.android.settings""com.android.settings.widget.SettingsAppWidgetProvider");
033. gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
034. gpsIntent.setData(Uri.parse("custom:3"));
035. try {
036. PendingIntent.getBroadcast(this0, gpsIntent, 0).send();
037. catch (CanceledException e) {
038. e.printStackTrace();
039. locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, locationListener);
040. Location location1 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
041. if (location1 != null) {
042. latitude = location1.getLatitude(); // 经度
043. longitude = location1.getLongitude(); // 纬度
044. }
045. }
046. }
047.  
048. private void getLocation() {
049. Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
050. if (location != null) {
051. latitude = location.getLatitude();
052. longitude = location.getLongitude();
053. else {
054.  
055. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, locationListener);
056. }
057. info.setText("纬度:" + latitude + "\n" "经度:" + longitude);
058. }
059.  
060. LocationListener locationListener = new LocationListener() {
061. // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
062. @Override
063. public void onStatusChanged(String provider, int status, Bundle extras) {
064. }
065.  
066. // Provider被enable时触发此函数,比如GPS被打开
067. @Override
068. public void onProviderEnabled(String provider) {
069. Log.e(TAG, provider);
070. }
071.  
072. // Provider被disable时触发此函数,比如GPS被关闭
073. @Override
074. public void onProviderDisabled(String provider) {
075. Log.e(TAG, provider);
076. }
077.  
078. // 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
079. @Override
080. public void onLocationChanged(Location location) {
081. if (location != null) {
082. Log.e("Map""Location changed : Lat: " + location.getLatitude() + " Lng: " + location.getLongitude());
083. latitude = location.getLatitude(); // 经度
084. longitude = location.getLongitude(); // 纬度
085. }
086. }
087. };
088.  
089. /*
090. *
091. * 打开和关闭gps第二种方法
092. * private void openGPSSettings() {
093. //获取GPS现在的状态(打开或是关闭状态)
094. boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER);
095. if (gpsEnabled) {
096. //关闭GPS
097. Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, false);
098. } else {
099. //打开GPS 
100. Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);
101. }
102. }*/
103. }
main.xml布局如下

01. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03. android:id="@+id/layout"
04. android:layout_width="match_parent"
05. android:layout_height="match_parent"
06. android:background="@android:color/white"
07. android:orientation="vertical" >
08.  
09. <TextView
10. android:id="@+id/tv"
11. android:layout_width="wrap_content"
12. android:layout_height="wrap_content"
13. android:text="经纬度信息:"
14. android:textColor="#660000"
15. android:textSize="20sp" />
16.  
17. </LinearLayout>
清单文件如下:

01. <?xml version="1.0" encoding="utf-8"?>
02. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
03. package="com.example.tqmapdemo"
04. android:versionCode="1"
05. android:versionName="1.0" >
06. <uses-sdk
07. android:minSdkVersion="8"
08. android:targetSdkVersion="18" />
09.  
10. <!-- 连接<a href="http://www.it165.net/news/nhlw/" target="_blank" class="keylink">互联网</a>Internet权限 -->
11. <uses-permission android:name="android.permission.INTERNET" />
12. <!-- GPS定位权限 -->
13. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
14. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
15.  
16. <application
17. android:allowBackup="true"
18. android:icon="@drawable/ic_launcher"
19. android:label="@string/app_name"
20. android:theme="@android:style/Theme.Black" >
21. <activity
22. android:name="com.example.tqmapdemo.MainActivity"
23. android:label="@string/app_name" >
24. <intent-filter>
25. <action android:name="android.intent.action.MAIN" />
26.  
27. <category android:name="android.intent.category.LAUNCHER" />
28. </intent-filter>
29. </activity>
30. </application>
31. </manifest>
运行结果如下

\

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中,我们可以使用LocationManager和LocationListener来获取当前的经纬度。 首先,我们需要在AndroidManifest.xml文件中添加相应的权限: <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 然后,在你的Activity中,在onCreate方法获取LocationManager的实例,并检查是否获得了权限: LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); boolean isCoarseLocationEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); boolean isFineLocationEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 然后,我们需要创建一个LocationListener来监听位置变化事件: LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { // 当位置变化时触发该方法 double latitude = location.getLatitude(); //获取纬度 double longitude = location.getLongitude(); //获取经度 // 将经纬度保存到变量中,或者进行其他需要的处理 } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} }; 接下来,我们需要请求位置更新: if (isCoarseLocationEnabled) { locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); } else if (isFineLocationEnabled) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); } 最后,当不再需要获取位置信息时,要记得取消位置更新: locationManager.removeUpdates(locationListener); 这样,当我们点击按钮时,就能实时获得当前的经纬度了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值