Android 笔记:Maps, Geocoding, and LBS

本章主要内容
正向和反向地址匹配
用Map Views 和 Map Activities 建立交互地图
向地图添加标记
利用LBS找到位置(LBS就是基于位置的服务)
使用近邻提醒


[b]使用LBS[/b]
主要的两个LBS组件包括
Location Manager
Location Providers

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


[b]LOCATION PROVIDER[/b]
能提供位置服务的往往有多家,他们的价格,耗电,准确性等也各有不同。下面的代码是用于选择某个位置服务的。

String providerName = LocationManager.GPS_PROVIDER;
LocationProvider gpsProvider;
gpsProvider = locationManager.getProvider(providerName);


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

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


boolean enabledOnly = true;
List<String> providers = locationManager.getProviders(enabledOnly);


你也可以使用筛选项来找到符合你要求的

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
String bestProvider = locationManager.getBestProvider(criteria, true);



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


String serviceString = Context.LOCATION_SERVICE;
LocationManager locationManager;
locationManager = (LocationManager)getSystemService(serviceString);


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


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


下面这段代码可以知道你的上一次的位置,使用了GPS来提供位置服务。

String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
double lat = location.getLatitude();
double lng = location.getLongitude();



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


String provider = LocationManager.GPS_PROVIDER;
int t = 5000; // milliseconds
int distance = 5; // meters
LocationListener myLocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Update application based on new location.
}
public void onProviderDisabled(String provider){
// Update application if provider disabled.
}
public void onProviderEnabled(String provider){
// Update application if provider enabled.
}
public void onStatusChanged(String provider, int status,
Bundle extras){
// Update application if provider hardware status changed.
}
};
locationManager.requestLocationUpdates(provider, t, distance,
myLocationListener);




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

Reverse Geocoding


location =
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
List<Address> addresses = null;
Geocoder gc = new Geocoder(this, Locale.getDefault());
try {
addresses = gc.getFromLocation(latitude, longitude, 10);
} catch (IOException e) {}


Forward Geocoding


Geocoder fwdGeocoder = new Geocoder(this, Locale.US);
String streetAddress = "160 Riverside Drive, New York, New York";
List<Address> locations = null;
try {
locations = fwdGeocoder.getFromLocationName(streetAddress, 10);
} catch (IOException e) {}



[b]创建一个基于地图的 ACTIVITIE[/b]
需要使用到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。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/map_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="mymapapikey"
/>
</LinearLayout>


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

mapView.setSatellite(true);
mapView.setStreetView(true);
mapView.setTraffic(true);


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


MapController mapController = myMapView.getController();
Double lat = 37.422006*1E6;
Double lng = -122.084095*1E6;
GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
mapController.setCenter(point);
mapController.setZoom(1);
//mapController.animateTo(point);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值