APP中可能会遇到一种需求,就是将当前所在位置的坐标传到server上,今天我提供三种途径去获取经纬度坐标信息,第一种是通过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布局例如以下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" > <TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="经纬度信息:"
android:textColor="#660000"
android:textSize="20sp" /> </LinearLayout>

清单文件例如以下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tqmapdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <!-- 连接互联网Internet权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GPS定位权限 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black" >
<activity
android:name="com.example.tqmapdemo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

执行结果例如以下

下载Demo请猛戳

另外一种方法(百度地图API实现,注:须要自己申请apikey)

下载Demo请猛戳





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

下载Demo请猛戳

Android GPS获取当前经纬度坐标的更多相关文章

  1. Android 使用GPS获取到经纬度后 无法在Android8.0上使用Geocoder类获取位置信息

    由于我的应用在获取到经纬度后在Android8.0不能使用如下代码获取位置信息.只好使用百度地图 WEB服务API 通过调接口的方式获取位置信息. Geocoder geocoder = new Ge ...

  2. 批量调用百度地图API获取地址经纬度坐标

    1 申请密匙 注册百度地图API:http://lbsyun.baidu.com/index.php?title=webapi 点击左侧 “获取密匙” ,经过填写个人信息.邮箱注册等,成功之后在开放平 ...

  3. Android GPS获取当前位置信息

    package com.example.gpstest; import org.apache.http.util.LangUtils; import android.content.Context; ...

  4. Android模拟器手动设置经纬度坐标

    第一种方式可以在eclipse的DDMS中的Emulator control中设置,如下图 另一种是在cmd中输入telnet localhost 5554(注:5554是模拟器在本机的端口,有可能不 ...

  5. H5获取的经纬度,该怎么在百度地图中查看?

    之前理所当然的的到百度的坐标拾取系统, 输入H5获取的经纬度坐标,然后查询,然后发现老是有误差,而且误差都是一样的规律:偏实际位置西南方约1000~1500米左右. 以为是H5获取经纬度必然会产生这么 ...

  6. MacOS下Terminal获取GPS经纬度坐标

    通过命令行直接获取经纬度坐标MacOS 首先下载WhereAmI,最新版本: https://github.com/robmathers/WhereAmI/releases/download/v1.1 ...

  7. GPS获取Location 获取所在地点的经纬度

    利用手机获取所在地点的经纬度: Location 在Android 开发中还是经常用到的,比如 通过经纬度获取天气,根据Location 获取所在地区详细Address (比如Google Map 开 ...

  8. ionic 3 安卓手机获取经纬度坐标

    现在有个需求:每隔一段时间需向后台服务器返回当前用户的经纬度坐标. ionic 官方提供的有定位插件cordova-plugin-geolocation,兼容ios和android版本,网上查资料说最 ...

  9. 利用百度地图API,获取经纬度坐标

    利用百度地图API,获取经纬度坐标 代码很简单,但在网上没找到现成的获取地图经纬度的页面. 就是想,给当前页面传递一个经纬度,自动定位到此经纬度.然后可以重新选择,选择完返回经纬度. 效果如下: 源代 ...

随机推荐

  1. 重新想象 Windows 8 Store Apps (5) - 控件之集合控件: ComboBox, ListBox, FlipView, ItemsControl, ItemsPresenter

    原文:重新想象 Windows 8 Store Apps (5) - 控件之集合控件: ComboBox, ListBox, FlipView, ItemsControl, ItemsPresente ...

  2. Autofac 入门

    Autofac 入门文档 原文链接:http://docs.autofac.org/en/latest/getting-started/index.html 在程序中使用Autofac的基本模式是: ...

  3. 2014在百度之星资格赛的第二个问题Disk Schedule

    事实上,我认为它可以用来费用流问题.但光建地图上加班. ..不科学啊.. . 因副作用太大,否则,必然在.最后,想啊想,或者使用dp对.... 别想了一维dp... .我不知道我是怎么想.无论如何,这 ...

  4. unity3d中让物体显示和隐藏

    unity3d中让物体显示和隐藏的方法 gameObject.renderer.enabled //是控制一个物体是否在屏幕上渲染或显示  而物体实际还是存在的 仅仅是想当于隐身 而物体本身的碰撞体还 ...

  5. c# winfrom DataGridView使行高不可改变,使列头高度不可改变,

    // 禁止用户改变DataGridView1的所有列的列宽 //DataGridView1.AllowUserToResizeColumns = false; //禁止用户改变DataGridView ...

  6. 全栈JavaScript之路(十七)HTML5 新增字符集属性

    HTML5 添加�了几个文档字符集属性. document.charset : 表示文档的实际使用的字符集. document.defaultCharset: 表示默认的字符集,跟浏览器以及操作系统设 ...

  7. gdb经常使用的命令

    在调试程序的时候,gdb是一柄利器,恰当的使用gdb能够解决掉程序的很多bug. gdb并不检查语法错误.那是gcc或者g++的事情,gdb干的是调试的事情. 说明: (1)gdb 程序名 [core ...

  8. zoj 3696 Alien&#39;s Organ(泊松分布)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3696 Alien's Organ Time Limit: 2 S ...

  9. IOS-QQ登陆之苹果程序流程

    1.新建项目,通过main函数循环执行代码,直到应用被关闭. 2.点击项目,建立storyboard文件,并在info文件夹中指定第一个storyboard文件 3.建立Controller文件. 组 ...

  10. apache-maven-3.2.1设备

    maven 是一个项目管理工具,并建立自己主动.本文所讲apache-maven-3.2.1设备. 它的下载: http://maven.apache.org/download.cgi ,选apach ...