[原创] Android GPS (当前位置 & GPS信息更新)

    最近在做Android手机应用开发,还是很有意思的。其实如果只是做简单手机应用开发而不是手机游戏开发的话,还是很简单的。把主要的控件掌握了,就可以开发简单的应用了。

    下面主要说一下在Android中使用GPS功能。

    开发由于GPS功能时,常与Google Map相关,因此先推荐一篇讲解Google Map的文章:
http://mobiforge.com/developing/story/using-google-maps-android
该文章详细的讲解了Android中如何使用Google Map的各种功能。文章甚好,强烈推荐。

    看完了如上文章后,我们就来讲解下如何使用GPS。

    首先在AndroidManifest.xml中添加位置服务权限:
   

1
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    然后再看如下代码例:

1
2
3
4
5
6
7
8
LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location loc = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (loc != null) {
lat = loc.getLatitude();
Log.d(TAG, "latitude: " + lat);
lng = loc.getLongitude();
Log.d(TAG, "longitude: " + lng);
}

    先注册LocationManager,然后就可以通过访问getLastKnownLocation得到当前的GPS坐标。是不是很简单。

    既然是GPS,我们当然不只是想知道当前的位置,更重要的是要随着位置的移动,GPS信息也要更新。那么我们需要怎么做呢?

    还先看如下代码例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
LocationListener locLis = new MyLocationListener();
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10,
locLis);
...
...
...
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
p = new GeoPoint((int) (loc.getLatitude() * 1E6),
(int) (loc.getLongitude() * 1E6));
mc.animateTo(p);
mc.setZoom(14);
mc.setCenter(p);
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}

    声明自己的LocationListener后,调用requestLocationUpdates方法,就可以得到最新的GPS信息。

    常用方法说明:
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)

    当时间超过minTime(单位:毫秒),或者位置移动超过minDistance(单位:米),就会调用listener中的方法更新GPS信息。
    官方文档中有如下说明:
    1. minTime的值最好是不小于60000(即:1分钟),这样会更加高效且节电。
    2. 如果要尽可能实时的更新GPS信息,请将minTime和minDistance都设置成0。
以说明下来自官方文档:
    Registers the current activity to be notified periodically by the named provider. Periodically, the supplied LocationListener will be called with the current Location or with status updates.

    It may take a while to receive the most recent location. If an immediate location is required, applications may use the getLastKnownLocation(String) method.

    In case the provider is disabled by the user, updates will stop, and the onProviderDisabled(String) method will be called. As soon as the provider is enabled again, the onProviderEnabled(String) method will be called and location updates will start again.

    The frequency of notification may be controlled using the minTime and minDistance parameters. If minTime is greater than 0, the LocationManager could potentially rest for minTime milliseconds between location updates to conserve power. If minDistance is greater than 0, a location will only be broadcasted if the device moves by minDistance meters. To obtain notifications as frequently as possible, set both parameters to 0.

    Background services should be careful about setting a sufficiently high minTime so that the device doesn’t consume too much power by keeping the GPS or wireless radios on all the time. In particular, values under 60000ms are not recommended.

    The calling thread must be a Looper thread such as the main thread of the calling Activity.

坚持原创及高品质技术分享,您的支持将鼓励我继续创作!