Android 笔记:Maps, Geocoding, and LBS

来源:互联网 发布:nvsip相似软件 编辑:程序博客网 时间:2024/04/28 18:45
本章主要内容 
正向和反向地址匹配 
用Map Views 和 Map Activities 建立交互地图 
向地图添加标记 
利用LBS找到位置(LBS就是基于位置的服务) 
使用近邻提醒 


使用LBS 
主要的两个LBS组件包括 
Location Manager 
Location Providers 

使用Location Manager,你可以 
得到现在的位置 
跟踪移动 
接近某个区域时设置提醒 
找到可用的Location Providers 


LOCATION PROVIDER 
能提供位置服务的往往有多家,他们的价格,耗电,准确性等也各有不同。下面的代码是用于选择某个位置服务的。 
Java代码 
  1. String providerName = LocationManager.GPS_PROVIDER;  
  2. LocationProvider gpsProvider;  
  3. gpsProvider = locationManager.getProvider(providerName);  


LocationManager的常量包含了最常用的位置服务名 
LocationManager.GPS_PROVIDER 
LocationManager.NETWORK_PROVIDER 

如果想知道所有的位置服务提供者,可以使用以下代码 

Java代码 
  1. boolean enabledOnly = true;  
  2. List<String> providers = locationManager.getProviders(enabledOnly);  


你也可以使用筛选项来找到符合你要求的 
Java代码 
  1. Criteria criteria = new Criteria();  
  2. criteria.setAccuracy(Criteria.ACCURACY_COARSE);  
  3. criteria.setPowerRequirement(Criteria.POWER_LOW);  
  4. criteria.setAltitudeRequired(false);  
  5. criteria.setBearingRequired(false);  
  6. criteria.setSpeedRequired(false);  
  7. criteria.setCostAllowed(true);  
  8. String bestProvider = locationManager.getBestProvider(criteria, true);  



找到你的位置 
你可以使用getSystemService()方法,传入Context.LOCATION_SERVICE参数来获得一个LocationManager的实例。 

Java代码 
  1. String serviceString = Context.LOCATION_SERVICE;  
  2. LocationManager locationManager;  
  3. locationManager = (LocationManager)getSystemService(serviceString);  


在manifest文件中你需要加入相应的许可 

Java代码 
  1. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  
  2. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>  


下面这段代码可以知道你的上一次的位置,使用了GPS来提供位置服务。 
Java代码 
  1. String provider = LocationManager.GPS_PROVIDER;  
  2. Location location = locationManager.getLastKnownLocation(provider);  
  3. double lat = location.getLatitude();  
  4. double lng = location.getLongitude();  



下面这段代码把一个位置监听器注册到了位置服务提供者上面,并传入了间隔时间和最小距离。 
这个间隔时间只是为了省电,实际中更新的间隔可能比这个值大也可能比这个小。GPS非常耗电,所以这个值一定不能太小。 
当最小时间和距离被超出的时候,监听器中的onLocationChanged方法会被调用。 

Java代码 
  1. String provider = LocationManager.GPS_PROVIDER;  
  2. int t = 5000// milliseconds  
  3. int distance = 5// meters  
  4. LocationListener myLocationListener = new LocationListener() {  
  5. public void onLocationChanged(Location location) {  
  6. // Update application based on new location.  
  7. }  
  8. public void onProviderDisabled(String provider){  
  9. // Update application if provider disabled.  
  10. }  
  11. public void onProviderEnabled(String provider){  
  12. // Update application if provider enabled.  
  13. }  
  14. public void onStatusChanged(String provider, int status,  
  15. Bundle extras){  
  16. // Update application if provider hardware status changed.  
  17. }  
  18. };  
  19. locationManager.requestLocationUpdates(provider, t, distance,  
  20. myLocationListener);  



使用GEOCODER 
Geocoding是用于街道地址和经纬度之间的转换的。下面两个概念的意思: 
Forward geocoding  为一个地址找到经纬度 
Reverse geocoding  为一个经纬度找到地址 

Reverse Geocoding 

Java代码 
  1. location =  
  2. locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);  
  3. double latitude = location.getLatitude();  
  4. double longitude = location.getLongitude();  
  5. List<Address> addresses = null;  
  6. Geocoder gc = new Geocoder(this, Locale.getDefault());  
  7. try {  
  8. addresses = gc.getFromLocation(latitude, longitude, 10);  
  9. catch (IOException e) {}  


Forward Geocoding 

Java代码 
  1. Geocoder fwdGeocoder = new Geocoder(this, Locale.US);  
  2. String streetAddress = "160 Riverside Drive, New York, New York";  
  3. List<Address> locations = null;  
  4. try {  
  5. locations = fwdGeocoder.getFromLocationName(streetAddress, 10);  
  6. catch (IOException e) {}  



创建一个基于地图的 ACTIVITIE 
需要使用到MapView和MapActivity 
MapView 就是显示地图的view 
MapActivity MapView只能在MapActivity的子类中显示 

要显示地图,需要从以下地址得到一个API key 
http://code.google.com/android/maps-api-signup.html 

这个地址会要求你输入你的certificate's MD5 fingerprint,怎么得到呢。 
如果是用于开发环境,可以使用JAVA_HOME下面的keytool命令。<keystore_location>.keystore 
可以在eclipse的Windows ➪ Preferences ➪ Android ➪ build里找到 

keytool -list -alias androiddebugkey -keystore <keystore_location>.keystore 
-storepass android -keypass android 

如果是用于生产环境,可以使用下面的命令 

keytool -list -alias my-android-alias -keystore my-android-keystore 

其中my-android-keystore可以在你export一个android程序为.apk文件时产生。 

另外,在manifest文件中的Application节点下,需要加入 
<uses-library android:name="com.google.android.maps"/> 

因为这个包是不在android.jar里面的,而是在google api里面。 


在layout配置文件里面添加一个MapView,需要指定apiKey。 
Java代码 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3. xmlns:android="http://schemas.android.com/apk/res/android"  
  4. android:orientation="vertical"  
  5. android:layout_width="fill_parent"  
  6. android:layout_height="fill_parent">  
  7. <com.google.android.maps.MapView  
  8. android:id="@+id/map_view"  
  9. android:layout_width="fill_parent"  
  10. android:layout_height="fill_parent"  
  11. android:enabled="true"  
  12. android:clickable="true"  
  13. android:apiKey="mymapapikey"  
  14. />  
  15. </LinearLayout>  


可以选择设置你的地图是卫星图,街道图还是交通图。 

Java代码 
  1. mapView.setSatellite(true);  
  2. mapView.setStreetView(true);  
  3. mapView.setTraffic(true);  


介绍下MapController,这个是控制地图的比例和位置的。 
以下例子指定了经纬度,比例为最小(1是比例尺最小,21为比例尺最大) 
使用setCenter,会跳到指定的位置,而使用animateTo,则会滑到指定的地点。 

Java代码 
  1. MapController mapController = myMapView.getController();  
  2. Double lat = 37.422006*1E6;  
  3. Double lng = -122.084095*1E6;  
  4. GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());  
  5. mapController.setCenter(point);  
  6. mapController.setZoom(1);  
  7. //mapController.animateTo(point);