Android中GPS定位(获取经纬度)

移动开发 Android
本文通过作者自己的理解与实践为大家呈现了AndroidGPS定位获取经纬度的实现方法与代码,把作者自己对这一知识点的领悟和实行分享给大家。

AndroidGPS定位问题,众所周知是一个蛮麻烦的问题.当初我是新手,现在我也是新手,也搞了我头大,网上搜索了很多的例子,一直处于僵持阶段,而现在终于搞定了,因为我现在只需要获取到经纬度就可以了,反正获取经纬度可以从我这篇文章中看看;上代码。

在AndroidManifest.xml中加入权限:
<uses-permission android:name="android.permission.ACCESSFINELOCATION"/>
<uses-permission android:name="android.permission.ACCESSCOARSELOCATION"/>

  1. package com.example.tt; 
  2.  
  3. import android.location.Location; 
  4. import android.location.LocationListener; 
  5. import android.location.LocationManager; 
  6. import android.os.Bundle; 
  7. import android.app.Activity; 
  8. import android.content.Context; 
  9. import android.view.View; 
  10. import android.view.View.OnClickListener; 
  11. import android.widget.Button; 
  12. import android.widget.TextView; 
  13. import android.widget.Toast; 
  14.  
  15. public class MainActivity extends Activity { 
  16.      
  17.     @Override 
  18.     protected void onCreate(Bundle savedInstanceState) { 
  19.         super.onCreate(savedInstanceState); 
  20.         setContentView(R.layout.activity_main); 
  21.                  
  22.         Button button=(Button)findViewById(R.id.button1); 
  23.         button.setOnClickListener(new OnClickListener() { 
  24.              
  25.             @Override 
  26.             public void onClick(View arg0) { 
  27.                 // TODO Auto-generated method stub 
  28.                 String serviceString=Context.LOCATION_SERVICE; 
  29.                 LocationManager locationManager=(LocationManager)getSystemService(serviceString); 
  30.                 String provider=LocationManager.GPS_PROVIDER; 
  31.                 Location location=locationManager.getLastKnownLocation(provider); 
  32.                 getLocationInfo(location); 
  33.                                 locationManager.requestLocationUpdates(provider, 20000, locationListener); 
  34.             } 
  35.         });          
  36.     } 
  37.     private void getLocationInfo(Location location) { 
  38.         String latLongInfo; 
  39.         TextView lo=(TextView)findViewById(R.id.textView1); 
  40.         if(location!=null){ 
  41.             double lat=location.getLatitude(); 
  42.             double lng=location.getLongitude(); 
  43.             latLongInfo="Lat:"+lat+"\nLong:"+lng; 
  44.             lo.setText(latLongInfo); 
  45.         }else { 
  46.             latLongInfo="No location found"
  47.             lo.setText(latLongInfo); 
  48.         }        
  49.     } 
  50.     private final LocationListener locationListener =new LocationListener() {        
  51.         @Override 
  52.         public void onStatusChanged(String provider, int status, Bundle extras) { 
  53.             // TODO Auto-generated method stub 
  54.              
  55.         }        
  56.         @Override 
  57.         public void onProviderEnabled(String provider) { 
  58.             getLocationInfo(null); 
  59.              
  60.         }        
  61.         @Override 
  62.         public void onProviderDisabled(String provider) { 
  63.             getLocationInfo(null);           
  64.         }        
  65.         @Override 
  66.         public void onLocationChanged(Location location) { 
  67.             getLocationInfo(location); 
  68.             Toast.makeText(MainActivity.this"位置改变了::::::::::::"3000).show(); 
  69.         } 
  70.     }; 

当需要使用基站定位时,可以将String provider=LocationManager.GPS_PROVIDER;改为**String provider=LocationManager.NETWORK_PROVIDER;
**
具体如果还要判断GPS搜索不到时切换基站定位,那样的功能就不要我写了,新手都应该会.
还有就是如果用到Google定位到哪个城市地点什么的,也easy了。

以上就是我个人对AndroidGPS定位的理解和实现方法。

责任编辑:闫佳明 来源: my.eoe.cn
相关推荐

2013-05-23 14:43:15

Android开发IP地址经纬度坐标

2023-03-13 22:01:15

ChatGPTPython

2012-06-14 09:37:45

Google地图

2023-12-11 07:37:08

mongodb经纬度性能

2011-05-27 15:56:30

Android

2021-04-18 16:34:13

PythonAPI接口

2011-10-21 09:28:25

百度地图API

2014-07-14 13:03:26

2014-11-13 14:07:04

2017-11-13 15:46:07

2011-05-24 09:23:00

中电信定位云

2013-04-01 13:19:43

iOS定位与坐标算法

2011-05-23 10:06:15

2011-07-18 13:37:53

2021-01-01 19:02:08

GPS定位欺骗网络安全

2023-10-20 16:25:30

Python

2017-09-01 13:41:20

Android定位服务

2012-08-13 14:03:25

Google Map

2012-02-01 09:33:36

百度地图API
点赞
收藏

51CTO技术栈公众号