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(this, 0, gpsIntent, 0).send();
037.
} catch (CanceledException e) {
038.
e.printStackTrace();
039.
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, 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, 1000, 0, 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布局如下
view source
print
?
01.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
02.
xmlns:tools="http://schemas.android.com/tools"
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>
清单文件如下:
view source
print
?
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>
运行结果如下