全球定位系统(Global Positioning System,GPS)又称为全球卫星定位系统,是一个中距离圆型轨道卫星导航系统,它可以为地球表面的绝大部分地区(98%)提供准确的定位、测速和高精度的时间标准。该系统由美国国防部研制和维护,可满足位于全球任何地方或近地空间的军事用户连续、精确地确定三维位置、三维运动和时间的需要。该系统包括太空中的24颗GPS卫星,地面上的1个主控站、3个数据注入站和5个监测站及作为用户端的GPS接收机。最少只需其中3颗卫星,就能迅速确定用户端在地球上所处的位置及海拔高度。所能连接到的卫星数越多,解码出来的位置就越精确。GPS广泛应用于军事、物流、地理、移动电话、数码相机、航空等领域,具有非常强大的功能,主要包括:
•精确定时:广泛应用在天文台、通信系统基站、电视台中。
•工程施工:道路、桥梁、隧道的施工中大量采用GPS设备进行工程测量。
•勘探测绘:野外勘探及城区规划中都有用到。
•导航。
•••武器导航:精确制导导弹、巡航导弹。
•••车辆导航:车辆调度、监控系统。
•••船舶导航:远洋导航、港口/内河引水。
•••飞机导航:航线导航、进场着陆控制。
•••星际导航:卫星轨道定位。
•••个人导航:个人旅游及野外探险。
•定位。
•••车辆防盗系统。
•••手机、PDA、PPC等通信移动设备防盗以及电子地图、定位系统。
•••儿童及特殊人群的防走失系统。
•精准农业:农机具导航、自动驾驶以及土地高精度平整。
Android 支持地理定位服务的API。该地理定位服务可以用来获取当前设备的地理位置,应用程序可以定时请求更新设备当前的地理定位信息。比如应用程序可以借助一个Intent接收器来实现如下功能:以经纬度和半径划定一个区域,当设备出入该区域时,发出提醒信息,还可以和Google Map API一起使用,完成更多的任务。关于地理定位系统的API全部位于android.location包内,其中包括以下几个重要的功能类:
•LocationManager:本类提供访问定位服务的功能,也提供获取最佳定位提供者的功能。另外,临近警报功能也可以借助该类来实现。
•LocationProvider:该类是定位提供者的抽象类。定位提供者具备周期性报告设备地理位置的功能。
•LocationListener:提供定位信息发生改变时的回调功能。必须事先在定位管理器中注册监听器对象。
•Criteria:该类使得应用能够通过在LocationProvider中设置的属性来选择合适的定位提供者。
•Geocoder:用于处理地理编码和反向地理编码的类。地理编码是指将地址或其他描述转变为经度和纬度,反向地理编码则是将经度和纬度转变为地址或描述语言,其中包含了两个构造函数,需要传入经度和纬度的坐标。getFromLocation方法可以得到一组关于地址的数组。
要使用地理定位,首先需要取得LocationManager的实例,在Android中,获得LocationManager的唯一方法是通过getSystemService()方法的调用。通过使用LocationManager,我们可以获得一个位置提供者的列表。在一个真实的手持设备中,这个列表包含了一些GPS服务。我们也可以选择更强大、更精确、不带有其他附加服务的GPS。代码如下:
LocationManager?locationManager?=?(LocationManager)?getSystemService(Context.LOC-
ATION_SERVICE);?
取得LocationManager对象之后,我们还需要注册一个周期性的更新视图,代码如下:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 0,
locationListener);
其中第一个参数是设置服务提供者,第二个参数是周期,这里需要重点说明一下最后一个参数locationListener,它用来监听定位信息的改变,所以我们必须实现以下几个方法:
•onLocationChanged(Location location):当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发。
•onProviderDisabled(String provider):Provider禁用时触发此函数,比如GPS被关闭。
•onProviderEnabled(String provider):Provider启用时触发此函数,比如GPS被打开。
•onStatusChanged(String provider, int status, Bundle extras):Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数。
下面我们通过更改上一节的例子(本书所附代码:第9章\Examples_09_04)来实现自动通过定位系统获取用户当前的坐标,然后加载并显示地图,将坐标信息显示在一个TextView中,运行效果如图9-15所示。
图9-15 地图定位
要使用定位的API,首先需要在AndroidManifest.xml文件中添加其权限,具体代码如代码清单9-5所示。
代码清单9-5 第9章\Examples_09_04\AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yarin.android.Examples_09_04"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".Activity01"
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>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-sdk android:minSdkVersion="5" />
</manifest>
由于我们在模拟器上测试,所以需要人为设置一个坐标。可以通过两种方法来设置一个模拟的坐标值。第一种方法是通过DDMS,我们可以在Eclipse的ADT插件中使用这种方法,只要启动Eclipse,选择“Window”->“Show View”,打开“Emulator Control”界面即可看到如下的设置窗口,我们可以手动或者通过KML和GPX文件来设置一个坐标。如图9-16所示。
图9-16 设置显示的坐标
另一种方法是使用geo命令,我们需要telnet到本机的5554端口,然后在命令行下输入类似于geo fix-121.45356 46.51119 4392 这样的命令,后面3个参数分别代表了经度、纬度和(可选的)海拔。设置之后在Android模拟器屏幕上便多出一个如图9-17所示的标志,表示模拟了GPS权限。
图9-17 GPS使用标志
现在我们可以使用位置管理器(LocationManager)和位置提供者进行getFromLocation的调用。这个方法返回本机当前位置的一个快照,这个快照将以 Location对象形式提供。在手持设备中,我们可以获得当前位置的经度和纬度;调用getFromLocationName方法可能返回一个数据,表示一个地方的名称。该例中我们还创建了一个菜单用来缩放地图,这时就使用地图控制器(MapController)的zoomIn和zoomOut方法来放大和缩小视图,具体实现如代码清单9-6所示。
代码清单9-6 第9章\Examples_09_04\src\com\yarin\android\Examples_09_04\Activity01.java
public class Activity01 extends MapActivity
{
public MapController mapController;
public MyLocationOverlay myPosition;
public MapView myMapView;
private static final int ZOOM_IN=Menu.FIRST;
private static final int ZOOM_OUT=Menu.FIRST+1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得LocationManager实例
LocationManager locationManager;
String context=Context.LOCATION_SERVICE;
locationManager=(LocationManager)getSystemService(context);
myMapView=(MapView)findViewById(R.id.MapView01);
//取得MapController实例,控制地图
mapController=myMapView.getController();
//设置显示模式
myMapView.setSatellite(true);
myMapView.setStreetView(true);
//设置缩放控制,这里我们自己实现缩放菜单
myMapView.displayZoomControls(false);
//设置使用MyLocationOverlay来绘图
mapController.setZoom(17);
myPosition=new MyLocationOverlay();
List<Overlay> overlays=myMapView.getOverlays();
overlays.add(myPosition);
//设置Criteria(服务商)的信息
Criteria criteria =new Criteria();
//经度要求
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//取得效果最好的criteria
String provider=locationManager.getBestProvider(criteria, true);
//得到坐标相关的信息
Location location=locationManager.getLastKnownLocation(provider);
//更新坐标
updateWithNewLocation(location);
//注册一个周期性的更新,3000ms更新一次
//locationListener用来监听定位信息的改变
locationManager.requestLocationUpdates(provider, 3000, 0,locationListener);
}
private void updateWithNewLocation(Location location)
{
String latLongString;
TextView myLocationText = (TextView)findViewById(R.id.TextView01);
String addressString="没有找到地址\n";
if(location!=null)
{
//为绘制标志的类设置坐标
myPosition.setLocation(location);
//取得经度和纬度
Double geoLat=location.getLatitude()*1E6;
Double geoLng=location.getLongitude()*1E6;
//将其转换为int型
GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue());
//定位到指定坐标
mapController.animateTo(point);
double lat=location.getLatitude();
double lng=location.getLongitude();
latLongString="经度:"+lat+"\n纬度:"+lng;
double latitude=location.getLatitude();
double longitude=location.getLongitude();
//根据地理环境来确定编码
Geocoder gc=new Geocoder(this,Locale.getDefault());
try
{
//取得地址相关的一些信息、经度、纬度
List<Address> addresses=gc.getFromLocation(latitude, longitude,1);
StringBuilder sb=new StringBuilder();
if(addresses.size()>0)
{
Address address=addresses.get(0);
for(int i=0;i<address.getMaxAddressLineIndex();i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
addressString=sb.toString();
}
}catch(IOException e){}
}
else
{
latLongString="没有找到坐标.\n";
}
//显示
myLocationText.setText("你当前的坐标如下:\n"+latLongString+"\n"+addressString);
}
private final LocationListener locationListener=new LocationListener()
{
//当坐标改变时触发此函数
public void onLocationChanged(Location location)
{
updateWithNewLocation(location);
}
//Provider禁用时触发此函数,比如GPS被关闭
public void onProviderDisabled(String provider)
{
updateWithNewLocation(null);
}
//Provider启用时触发此函数,比如GPS被打开
public void onProviderEnabled(String provider){}
//Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
public void onStatusChanged(String provider,int status,Bundle extras){}
};
protected boolean isRouteDisplayed()
{
return false;
}
//为应用程序添加菜单
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0, ZOOM_IN, Menu.NONE, "放大");
menu.add(0, ZOOM_OUT, Menu.NONE, "缩小");
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case (ZOOM_IN):
//放大
mapController.zoomIn();
return true;
case (ZOOM_OUT):
//缩小
mapController.zoomOut();
return true;
}
return true;
}
class MyLocationOverlay extends Overlay
{
Location mLocation;
//在更新坐标时,设置该坐标,以便画图
public void setLocation(Location location)
{
mLocation = location;
}
@Override
public boolean draw(Canvas canvas,MapView mapView,boolean shadow,long when)
{
super.draw(canvas, mapView, shadow);
Paint paint = new Paint();
Point myScreenCoords = new Point();
// 将经纬度转换成实际屏幕坐标
GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.
getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));
mapView.getProjection().toPixels(tmpGeoPoint,myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 0, 0);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.home);
canvas.drawBitmap(bmp,myScreenCoords.x,myScreenCoords.y,paint);
canvas.drawText("Here am I",myScreenCoords.x,myScreenCoords.
y, paint);
return true;
}
}
}
•精确定时:广泛应用在天文台、通信系统基站、电视台中。
•工程施工:道路、桥梁、隧道的施工中大量采用GPS设备进行工程测量。
•勘探测绘:野外勘探及城区规划中都有用到。
•导航。
•••武器导航:精确制导导弹、巡航导弹。
•••车辆导航:车辆调度、监控系统。
•••船舶导航:远洋导航、港口/内河引水。
•••飞机导航:航线导航、进场着陆控制。
•••星际导航:卫星轨道定位。
•••个人导航:个人旅游及野外探险。
•定位。
•••车辆防盗系统。
•••手机、PDA、PPC等通信移动设备防盗以及电子地图、定位系统。
•••儿童及特殊人群的防走失系统。
•精准农业:农机具导航、自动驾驶以及土地高精度平整。
Android 支持地理定位服务的API。该地理定位服务可以用来获取当前设备的地理位置,应用程序可以定时请求更新设备当前的地理定位信息。比如应用程序可以借助一个Intent接收器来实现如下功能:以经纬度和半径划定一个区域,当设备出入该区域时,发出提醒信息,还可以和Google Map API一起使用,完成更多的任务。关于地理定位系统的API全部位于android.location包内,其中包括以下几个重要的功能类:
•LocationManager:本类提供访问定位服务的功能,也提供获取最佳定位提供者的功能。另外,临近警报功能也可以借助该类来实现。
•LocationProvider:该类是定位提供者的抽象类。定位提供者具备周期性报告设备地理位置的功能。
•LocationListener:提供定位信息发生改变时的回调功能。必须事先在定位管理器中注册监听器对象。
•Criteria:该类使得应用能够通过在LocationProvider中设置的属性来选择合适的定位提供者。
•Geocoder:用于处理地理编码和反向地理编码的类。地理编码是指将地址或其他描述转变为经度和纬度,反向地理编码则是将经度和纬度转变为地址或描述语言,其中包含了两个构造函数,需要传入经度和纬度的坐标。getFromLocation方法可以得到一组关于地址的数组。
要使用地理定位,首先需要取得LocationManager的实例,在Android中,获得LocationManager的唯一方法是通过getSystemService()方法的调用。通过使用LocationManager,我们可以获得一个位置提供者的列表。在一个真实的手持设备中,这个列表包含了一些GPS服务。我们也可以选择更强大、更精确、不带有其他附加服务的GPS。代码如下:
LocationManager?locationManager?=?(LocationManager)?getSystemService(Context.LOC-
ATION_SERVICE);?
取得LocationManager对象之后,我们还需要注册一个周期性的更新视图,代码如下:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 0,
locationListener);
其中第一个参数是设置服务提供者,第二个参数是周期,这里需要重点说明一下最后一个参数locationListener,它用来监听定位信息的改变,所以我们必须实现以下几个方法:
•onLocationChanged(Location location):当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发。
•onProviderDisabled(String provider):Provider禁用时触发此函数,比如GPS被关闭。
•onProviderEnabled(String provider):Provider启用时触发此函数,比如GPS被打开。
•onStatusChanged(String provider, int status, Bundle extras):Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数。
下面我们通过更改上一节的例子(本书所附代码:第9章\Examples_09_04)来实现自动通过定位系统获取用户当前的坐标,然后加载并显示地图,将坐标信息显示在一个TextView中,运行效果如图9-15所示。
图9-15 地图定位
要使用定位的API,首先需要在AndroidManifest.xml文件中添加其权限,具体代码如代码清单9-5所示。
代码清单9-5 第9章\Examples_09_04\AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yarin.android.Examples_09_04"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".Activity01"
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>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-sdk android:minSdkVersion="5" />
</manifest>
由于我们在模拟器上测试,所以需要人为设置一个坐标。可以通过两种方法来设置一个模拟的坐标值。第一种方法是通过DDMS,我们可以在Eclipse的ADT插件中使用这种方法,只要启动Eclipse,选择“Window”->“Show View”,打开“Emulator Control”界面即可看到如下的设置窗口,我们可以手动或者通过KML和GPX文件来设置一个坐标。如图9-16所示。
图9-16 设置显示的坐标
另一种方法是使用geo命令,我们需要telnet到本机的5554端口,然后在命令行下输入类似于geo fix-121.45356 46.51119 4392 这样的命令,后面3个参数分别代表了经度、纬度和(可选的)海拔。设置之后在Android模拟器屏幕上便多出一个如图9-17所示的标志,表示模拟了GPS权限。
图9-17 GPS使用标志
现在我们可以使用位置管理器(LocationManager)和位置提供者进行getFromLocation的调用。这个方法返回本机当前位置的一个快照,这个快照将以 Location对象形式提供。在手持设备中,我们可以获得当前位置的经度和纬度;调用getFromLocationName方法可能返回一个数据,表示一个地方的名称。该例中我们还创建了一个菜单用来缩放地图,这时就使用地图控制器(MapController)的zoomIn和zoomOut方法来放大和缩小视图,具体实现如代码清单9-6所示。
代码清单9-6 第9章\Examples_09_04\src\com\yarin\android\Examples_09_04\Activity01.java
public class Activity01 extends MapActivity
{
public MapController mapController;
public MyLocationOverlay myPosition;
public MapView myMapView;
private static final int ZOOM_IN=Menu.FIRST;
private static final int ZOOM_OUT=Menu.FIRST+1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得LocationManager实例
LocationManager locationManager;
String context=Context.LOCATION_SERVICE;
locationManager=(LocationManager)getSystemService(context);
myMapView=(MapView)findViewById(R.id.MapView01);
//取得MapController实例,控制地图
mapController=myMapView.getController();
//设置显示模式
myMapView.setSatellite(true);
myMapView.setStreetView(true);
//设置缩放控制,这里我们自己实现缩放菜单
myMapView.displayZoomControls(false);
//设置使用MyLocationOverlay来绘图
mapController.setZoom(17);
myPosition=new MyLocationOverlay();
List<Overlay> overlays=myMapView.getOverlays();
overlays.add(myPosition);
//设置Criteria(服务商)的信息
Criteria criteria =new Criteria();
//经度要求
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//取得效果最好的criteria
String provider=locationManager.getBestProvider(criteria, true);
//得到坐标相关的信息
Location location=locationManager.getLastKnownLocation(provider);
//更新坐标
updateWithNewLocation(location);
//注册一个周期性的更新,3000ms更新一次
//locationListener用来监听定位信息的改变
locationManager.requestLocationUpdates(provider, 3000, 0,locationListener);
}
private void updateWithNewLocation(Location location)
{
String latLongString;
TextView myLocationText = (TextView)findViewById(R.id.TextView01);
String addressString="没有找到地址\n";
if(location!=null)
{
//为绘制标志的类设置坐标
myPosition.setLocation(location);
//取得经度和纬度
Double geoLat=location.getLatitude()*1E6;
Double geoLng=location.getLongitude()*1E6;
//将其转换为int型
GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue());
//定位到指定坐标
mapController.animateTo(point);
double lat=location.getLatitude();
double lng=location.getLongitude();
latLongString="经度:"+lat+"\n纬度:"+lng;
double latitude=location.getLatitude();
double longitude=location.getLongitude();
//根据地理环境来确定编码
Geocoder gc=new Geocoder(this,Locale.getDefault());
try
{
//取得地址相关的一些信息、经度、纬度
List<Address> addresses=gc.getFromLocation(latitude, longitude,1);
StringBuilder sb=new StringBuilder();
if(addresses.size()>0)
{
Address address=addresses.get(0);
for(int i=0;i<address.getMaxAddressLineIndex();i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
addressString=sb.toString();
}
}catch(IOException e){}
}
else
{
latLongString="没有找到坐标.\n";
}
//显示
myLocationText.setText("你当前的坐标如下:\n"+latLongString+"\n"+addressString);
}
private final LocationListener locationListener=new LocationListener()
{
//当坐标改变时触发此函数
public void onLocationChanged(Location location)
{
updateWithNewLocation(location);
}
//Provider禁用时触发此函数,比如GPS被关闭
public void onProviderDisabled(String provider)
{
updateWithNewLocation(null);
}
//Provider启用时触发此函数,比如GPS被打开
public void onProviderEnabled(String provider){}
//Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
public void onStatusChanged(String provider,int status,Bundle extras){}
};
protected boolean isRouteDisplayed()
{
return false;
}
//为应用程序添加菜单
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0, ZOOM_IN, Menu.NONE, "放大");
menu.add(0, ZOOM_OUT, Menu.NONE, "缩小");
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId())
{
case (ZOOM_IN):
//放大
mapController.zoomIn();
return true;
case (ZOOM_OUT):
//缩小
mapController.zoomOut();
return true;
}
return true;
}
class MyLocationOverlay extends Overlay
{
Location mLocation;
//在更新坐标时,设置该坐标,以便画图
public void setLocation(Location location)
{
mLocation = location;
}
@Override
public boolean draw(Canvas canvas,MapView mapView,boolean shadow,long when)
{
super.draw(canvas, mapView, shadow);
Paint paint = new Paint();
Point myScreenCoords = new Point();
// 将经纬度转换成实际屏幕坐标
GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.
getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));
mapView.getProjection().toPixels(tmpGeoPoint,myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 0, 0);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.home);
canvas.drawBitmap(bmp,myScreenCoords.x,myScreenCoords.y,paint);
canvas.drawText("Here am I",myScreenCoords.x,myScreenCoords.
y, paint);
return true;
}
}
}