---
title: "android基于Gps 定位和基站定位获取经纬度"
author: "hack_zhang"
date: "2013-05-20T15:10:45Z"
tags: []
categories: ["移动开发"]
slug: "android-gps"
draft: false
lastmod: "2013-05-20T15:10:45Z"
id: 1872149
url: "https://hack-zhang.iteye.com/blog/1872149"
---

一：新建MyLocationManager.java类，本类是为了代码架构方便把地位经纬度的代码在这类中实现然后通过回调方法，在activity中显示；

 

Java代码  [![收藏代码](http://104zz.iteye.com/images/icon_star.png)]()

01. package com.android.location;
02. import android.content.Context;
03. import android.location.Location;
04. import android.location.LocationListener;
05. import android.location.LocationManager;
06. import android.os.Bundle;
07. public class MyLocationManager {
08. private static Context mContext;
09. private LocationManager gpsLocationManager;
10. private LocationManager networkLocationManager;
11. private static final int MINTIME = 2000;
12. private static final int MININSTANCE = 2;
13. private static MyLocationManager instance;
14. private Location lastLocation = null;
15. private static LocationCallBack mCallback;
16. public static void init(Context c, LocationCallBack callback) {
17. mContext = c;
18. mCallback = callback;
19. }
20. private MyLocationManager() {
21. // Gps 定位
22. gpsLocationManager = (LocationManager) mContext
23. .getSystemService(Context.LOCATION\_SERVICE);
24. Location gpsLocation = gpsLocationManager
25. .getLastKnownLocation(LocationManager.GPS\_PROVIDER);
26. gpsLocationManager.requestLocationUpdates(LocationManager.GPS\_PROVIDER,
27. MINTIME, MININSTANCE, locationListener);
28. // 基站定位
29. networkLocationManager = (LocationManager) mContext
30. .getSystemService(Context.LOCATION\_SERVICE);
31. Location networkLocation = gpsLocationManager
32. .getLastKnownLocation(LocationManager.GPS\_PROVIDER);
33. networkLocationManager.requestLocationUpdates(
34. LocationManager.NETWORK\_PROVIDER, MINTIME, MININSTANCE,
35. locationListener);
36. }
37. public static MyLocationManager getInstance() {
38. if (null == instance) {
39. instance = new MyLocationManager();
40. }
41. return instance;
42. }
43. private void updateLocation(Location location) {
44. lastLocation = location;
45. mCallback.onCurrentLocation(location);
46. }
47. private final LocationListener locationListener = new LocationListener() {
48. public void onStatusChanged(String provider, int status, Bundle extras) {
49. }
50. public void onProviderEnabled(String provider) {
51. }
52. public void onProviderDisabled(String provider) {
53. }
54. public void onLocationChanged(Location location) {
55. updateLocation(location);
56. }
57. };
58. public Location getMyLocation() {
59. return lastLocation;
60. }
61. private static int ENOUGH\_LONG = 1000 * 60;
62. public interface LocationCallBack {
63. /\**
64. * 当前位置
65. \*
66. * @param location
67. \*/
68. void onCurrentLocation(Location location);
69. }
70. public void destoryLocationManager() {
71. gpsLocationManager.removeUpdates(locationListener);
72. networkLocationManager.removeUpdates(locationListener);
73. }
74. }

   

 

二：在LocationActivity 中实现LocationCallBack接口，如下：

 

Java代码  [![收藏代码](http://104zz.iteye.com/images/icon_star.png)]()

01. package com.android.location;
02. import android.app.Activity;
03. import android.location.Location;
04. import android.os.Bundle;
05. import android.widget.TextView;
06. import com.android.fzmap.R;
07. import com.android.location.MyLocationManager.LocationCallBack;
08. public class LocationActivity extends Activity implements LocationCallBack {
09. private TextView desText;
10. private MyLocationManager mLocation;
11. @Override
12. public void onCreate(Bundle savedInstanceState) {
13. super.onCreate(savedInstanceState);
14. setContentView(R.layout.main);
15. desText = (TextView) this.findViewById(R.id.text);
16. MyLocationManager.init(LocationActivity.this.getApplicationContext(),
17. LocationActivity.this);//初始化
18. mLocation = MyLocationManager.getInstance();//获取实例
19. }
20. //回调定位信息
21. public void onCurrentLocation(Location location) {
22. if (location != null) {
23. // 显示定位结果
24. desText.setText("当前经度：" + location.getLongitude() + "\\n当前纬度："
25. + location.getLatitude());
26. }
27. }
28. // 关闭程序也关闭定位
29. @Override
30. protected void onDestroy() {
31. // TODO Auto-generated method stub
32. super.onDestroy();
33. mLocation.destoryLocationManager();
34. }
35. }

   

三：在AndroidManifest.xml中不要忘了要添加访问网络和启动定位等的几个权限

 

Xml代码  [![收藏代码](http://104zz.iteye.com/images/icon_star.png)]()

1. &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
2. &lt;uses-permission android:name="android.permission.ACCESS\_NETWORK\_STATE"/&gt;
3. &lt;uses-permission android:name="android.permission.ACCESS\_COARSE\_LOCATION"/&gt;
4. &lt;uses-permission android:name="android.permission.ACCESS\_FINE\_LOCATION"/&gt;

 

 

四：效果如下图：

![](http://dl.iteye.com/upload/attachment/0074/0133/71900e9c-d535-3e80-b032-7001060daac7.png)