`
yhz61010
  • 浏览: 569807 次
  • 来自: -
博客专栏
63c13ecc-ef01-31cf-984e-de461c7dfde8
libgdx 游戏开发
浏览量:12419
社区版块
存档分类
最新评论

[原创] 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中添加位置服务权限:
   
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    然后再看如下代码例:
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信息也要更新。那么我们需要怎么做呢?

    还先看如下代码例:
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.
0
1
分享到:
评论

相关推荐

    原创gps定位器

    "有侦测卫星数量及信号等等"意味着该应用不仅能够获取用户的位置信息,还能显示当前连接到设备的GPS卫星数量以及它们的信号强度,这对于评估定位精度和诊断GPS问题非常有用。 【标签】"android" 指出这个项目是基于...

    基于Android的餐饮位置服务系统开发毕业论文.doc

    2. **位置服务(LBS)**:讨论了如何利用地理位置信息为用户提供定制化服务,包括获取用户位置、搜索附近餐厅及路径规划。 3. **服务器端开发**:可能涉及到ASP.NET技术,用于构建后台管理系统,发布和管理餐饮信息...

    基于Android手机平台的自助旅游系统的设计与实现

    4:GPS定位:该子系统能获取旅游者当前所在的位置信息,能对设定的区域在进入或离开时予以音乐提醒,能将旅游者所走过的路径、平均速度等信息保存至数据库,供随时查看并动画回放。 ——————————————...

    基于Android的车位共享系统的设计

    定位导航模块:定位导航模块分为用户位置实时定位和停车路线实时导航,基于百度地图API和GPS技术为用户提供当前位置到停车位路径导航。 出租预约车位模块:车位主在App上出租自己的车位,并提供车位的相关信息、地址...

    基于android平台的天气预报系统的设计与开发大学本科毕业论文.doc

    在设计和开发这款天气预报系统时,开发者可能利用了Android提供的定位服务,以便确定用户位置并推荐当地的天气信息。同时,系统可能集成了第三方天气API,如OpenWeatherMap或AccuWeather,以获取实时的气象数据。 ...

Global site tag (gtag.js) - Google Analytics