AndroidGPS获取当前经纬度坐标-成都快上网建站

AndroidGPS获取当前经纬度坐标

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

网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、重庆小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了德钦免费建站欢迎大家使用!

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

MainActivity代码如下:

public class MainActivity extends Activity {
 private static final String TAG = MainActivity.class.getSimpleName();
 private double latitude = 0.0;
 private double longitude = 0.0;
 private TextView info;
 private LocationManager locationManager;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 info = (TextView) findViewById(R.id.tv);
 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
 if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
 getLocation();
 //gps已打开
 } else {
 toggleGPS();
 new Handler() {
 }.postDelayed(new Runnable() {
 @Override
 public void run() {
  getLocation();
 }
 }, 2000);
 
 }
 }
 
 private void toggleGPS() {
 Intent gpsIntent = new Intent();
 gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
 gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
 gpsIntent.setData(Uri.parse("custom:3"));
 try {
 PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send();
 } catch (CanceledException e) {
 e.printStackTrace();
 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
 Location location1 = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
 if (location1 != null) {
 latitude = location1.getLatitude(); // 经度
 longitude = location1.getLongitude(); // 纬度
 }
 }
 }
 
 private void getLocation() {
 Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
 if (location != null) {
 latitude = location.getLatitude();
 longitude = location.getLongitude();
 } else {
 
 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
 }
 info.setText("纬度:" + latitude + "\n" + "经度:" + longitude);
 }
 
 LocationListener locationListener = new LocationListener() {
 // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
 @Override
 public void onStatusChanged(String provider, int status, Bundle extras) {
 }
 
 // Provider被enable时触发此函数,比如GPS被打开
 @Override
 public void onProviderEnabled(String provider) {
 Log.e(TAG, provider);
 }
 
 // Provider被disable时触发此函数,比如GPS被关闭
 @Override
 public void onProviderDisabled(String provider) {
 Log.e(TAG, provider);
 }
 
 // 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
 @Override
 public void onLocationChanged(Location location) {
 if (location != null) {
 Log.e("Map", "Location changed : Lat: " + location.getLatitude() + " Lng: " + location.getLongitude());
 latitude = location.getLatitude(); // 经度
 longitude = location.getLongitude(); // 纬度
 }
 }
 };
 
 /*
 * 
 * 打开和关闭gps第二种方法
 * private void openGPSSettings() {
 //获取GPS现在的状态(打开或是关闭状态)
 boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER);
 if (gpsEnabled) {
 //关闭GPS
 Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, false);
 } else {
 //打开GPS 
 Settings.Secure.setLocationProviderEnabled(getContentResolver(), LocationManager.GPS_PROVIDER, true);
 }
 }*/
}

main.xml布局如下


 
 
 

清单文件如下:

<?xml version="1.0" encoding="utf-8"?>

 
 
 
 
 
 
 
 
 
  
   
    
 
    
   
  
 

运行结果如下

Android GPS获取当前经纬度坐标

下载Demo请猛戳

第二种方法(百度地图API实现,注:需要自己申请apikey)

下载Demo请猛戳

Android GPS获取当前经纬度坐标

第三种方法(天地图API实现)

下载Demo请猛戳

Android GPS获取当前经纬度坐标

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


分享名称:AndroidGPS获取当前经纬度坐标
转载来于:http://kswjz.com/article/ijcioj.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流