当前,Android手机应用的数量日益增多,其中很多应用已成为人们生活中不可缺少的助手。在众多的Android应用中,其中LBS(基于地理位置的的应用)深受人们的喜爱,主要原因是人们只需要使用手机,就能随时查看自己当前所在的位置,以及所处位置的相关其他信息,商家可以进入更深入的数据挖掘,如推销产品,基于LBS的交友聊天等等。

  在本系列教程中,将指导开发者搭建一个简单的LBS应用,其主要功能是找出给定位置附近的商店,并且将这些商店用图标标识出来。本系列教程的阅读对象为对Android 开发有初步认识的开发者。

  本系列教程将分为两部分,第一部分是指导用户使用Mapview控件进行编程,其中包括了如何获得Google Map API,如何使用该API进行简单的开发,如何获得用户当前所在的位置。第二部分则包括如何在地图上,用第三方的组件库,实现气球式显示若干指定位置的功能。

  步骤1 创建新的Android 工程

  首先打开eclipse新建立一个Android 工程,其中相关参数设置如下:

  Project name:MallFinder

  Build Target: Google APIs Platform – 2.1 API Level 7

  Application Name: Mall Finder

  Package Name: com.shawnbe.mallfinder

  Create Activity: MallFinderActivity

  MinimumSDK: 7

  如下图所示:

  步骤2 注册Google Map API key

  由于在使用google map的时候,需要使用google map api的key,因此需要先注册一个开发者key,可以到如下地址进行注册:http://code.google.com/android/add-ons/google-apis/mapkey.html ,其中需要我们先产生开发期间的md5 密纹才能完成注册,因此我们先学习如何生成一个MD5密纹。

  我们需要使用keytool工具,不使用传统的命令行方式下那枯燥的证书签名生成办法,而是直接在eclipse下通过插件进行完成,详细见步骤3

  步骤3 安装keytool插件

  在eclipse的Help菜单中,如下图,选择安装新软件:

  在安装地址中输入如下地址:http://www.keytool.sourceforge.net/update ,如下图

  接下来会加载相关的安装程序,并显示用户协议,选择接受所有用户协议后进行安装,安装成功后重新启动eclipse即可生效。

  步骤4 产生开发期间的MD5密钥

  在重新启动eclipse后,会发现工具栏多了如下图的keytool菜单

  现在,我们打开debug.keystore,注意其位置回因操作系统不同而有不同

  Windows Vista : C:\Users\\.android\debug.keystore

  Windows XP : C:\Documents and Settings\\.android\debug.keystore

  OS X 和 Linux : ~/.android/debug.keystore

  点keytool菜单中,选择open keystore,根据提示,选择当前系统所在的debug.keystore位置并打开,如下图,

  其中,输入密码默认为android即可,并点Load加载。之后会在eclipse中出现新的keytool的视图,如下图所示:

  双击打开androiddebugkey,复制其md5密纹,然后访问页面

  http://code.google.com/android/maps-api-signup.html ,接受其协议,将md5密纹复制进去,再点Generate API Key,即产生google map的api key,记得保存好这个KEY,在接下来的步骤中要使用。

  步骤5 增加MapView控件

  接下来,我们可以往布局文件中增加MapView控件。我们在main.xml中添加如下代码:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="你的GOOGLE MAP API KEY "/>
</FrameLayout>
</LinearLayout>
  在这个控件中,请注意要在android:apiKey的位置填入刚申请的google map api key。

  步骤6 设置相关的权限

  由于我们的应用需要调用Google Map的数据,以及通过手机的GPS获得相关的其他地理位置数据,因此我们必须在Android的Manifest文件中进行权限的设置。

  我们打开AndroidManifest.xml文件,然后增加如下代码所示的权限设置,注意添加在标签后,但要在标签前。

<uses-feature android:name="android.hardware.location.gps"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET"/>
  在这里,我们分别调用了gps位置服务权限,使用互联网的权限以及使用最佳位置查找的权限(FINE_LOCATION)。在本文中,其实没用到FINE_LOCATION,但只是告诉开发者可以调整使用不同的地理位置提供者(providers)以提供准确的位置服务,如果要有比较精确的地理位置服务,那么可以设置android.permisson.ACCESS_FINE_LOCATION服务。要注意的是在开发中,不要过多引用一些不需要使用的权限设置,否则会给用户带来担忧安全等问题。要是只想调用一般的位置服务,可以使用普通的android.permission.ACCESS_COARSE_LOCATION权限。

  为了在应用中使用Google Map,需要在Manifest文件中包含相关的类库文件,所以加入如下代码:

<uses-library android:required="true" android:name="com.google.android.maps" />
  为了视觉的美观,我们把标题栏也去掉,以便留给地图更大的空间,所以设置为

  android:theme="@android:style/Theme.NoTitleBar"

  下面是完整的Manifest文件代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shawnbe.mallfinder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<activity
android:label="@string/app_name"
android:name=".MallFinderActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:required="true" android:name="com.google.android.maps" />
</application>
<uses-feature android:name="android.hardware.location.gps"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
  步骤7 设置MapView

  下面对主activity程序(MallFinderActivity.java)进行修改,首先让其继承MapActivity类,如下:

  public class MallFinderActivity extends MapActivity {

  由于继承了MapActivity类,因此必须实现isRouteDisplayed方法,这个方法是用来做路线导航用的,这里我们不需要,因此只需要简单返回false即可:

  @Override

  protected boolean isRouteDisplayed() {

  // TODO Auto-generated method stub

  return false;

  }

  接下来我们就可以声明MapController控件,并对其进行相关属性的设置了,首先是声明控件

  private MapController mapController;

  private MapView mapView;

  并在oncreate方法中进行属性设置如下:

mapView = (MapView)findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
mapView.setStreetView(true);
mapController = mapView.getController();
mapController.setZoom(13);
  在上面的代码中,设置了地图显示的方式为街道模式(通过设置mapview的setStreetView属性值为true),并且通过mapView.setBuiltInZoomControls(true); 使用了系统内置的放大缩小功能,并设置了地图的放大缩小倍数为13。

  这个时候我们就可以选择运行应用,看下是否符合我们的初步预期效果,运行后效果如下图:

  步骤8 获得当前所在位置

  在LBS应用中,十分重要的工作是要获得当前设备所在的位置,这个可以使用locationManager类实现,在获得当前位置的时候是需要花费一些时间的。我们继续声明两个参数变量如下:

  private LocationManager locationManager;

  private GeoPoint currentLocation;

  分别声明了LocationManager类的实例和GeoPoint类的实例(用于下文中的经纬度的计算)。

  再增加如下几个方法:

public void getLastLocation(){
String provider = getBestProvider();
currentLocation = locationManager.getLastKnownLocation(provider);
if(currentLocation != null){
setCurrentLocation(currentLocation);
}
else
{
Toast.makeText(this, "Location not yet acquired", Toast.LENGTH_LONG).show();
}
}

public void animateToCurrentLocation(){
if(currentPoint!=null){
mapController.animateTo(currentPoint);
}
}

public String getBestProvider(){
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
String bestProvider = locationManager.getBestProvider(criteria, true);
return bestProvider;
}

public void setCurrentLocation(Location location){
int currLatitude = (int) (location.getLatitude()*1E6);
int currLongitude = (int) (location.getLongitude()*1E6);
currentLocation = new GeoPoint(currLatitude,currLongitude);

currentLocation = new Location("");
currentLocation.setLatitude(currentPoint.getLatitudeE6() / 1e6);
currentLocation.setLongitude(currentPoint.getLongitudeE6() / 1e6);
}
  并且在oncreate方法中,添加对以上方法的调用代码如下:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
mapView.setStreetView(true);
mapController = mapView.getController();
mapController.setZoom(13);
getLastLocation();
animateToCurrentLocation();
}
  首先,在getLastLocation这个方法中,创建了locationManager类的实例并且根据设置的条件返回一个合适的地理位置提供方法。在这里,我们并没有指定对地理位置提供者的查询条件,在实际应用中,开发者可以通过设置criteria.setAccuracy()和criteria.setPowerRequirement()方法进行查找。如果要使用最精确的provider的话,可以设置使用ACCURACY_FINE方法进行搜索,如下代码所示:

  criteria.setAccuracy(Criteria.ACCURACY_FINE);

  在本文中之所以不选择使用最准确的位置provider主要是因为是没在室外进行测试,都是在室内调试程序,建议开发完后换成GPS模式provider到室外进行调试那样将看到很好的效果。

  接下来代码中使用 currentLocation = locationManager.getLastKnownLocation(provider); 一句,获得最新的位置信息,并且在setCurrentLocation方法中,对通过prodiver获得的地理位置信息Location对象进行经纬度的转换为GeoPoint对象。然后调用animateToCurrentLocation方法,将地图的中心点定位到我们当前的位置上去。

  此外,由于我们的位置是会发生变化的,所以需要使用location 监听器去检测我们的位置变化,这时需要实现locationListener接口,如下所示:

  public class MallFinderActivity extends MapActivity implements LocationListener{

  同时我们需要实现LocationListener类的以下几个方法:

@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}

@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}

@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
  其中这里我们关心的是需要实现onLocationChanged方法,这里我们调用之前编写好的setlocation方法,获得当前的最新位置,如下:

@Override
public void onLocationChanged(Location newLocation) {
// TODO Auto-generated method stub
setCurrentLocation(newLocation);
}
  为了完善程序,在程序处在onResume及onPause状态下都能及时更新地理位置信息以及取消更新,加上如下代码:

@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(getBestProvider(), 1000, 1, this);
}

@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
  小结

  在本系列的第一讲中,分步讲解了如何注册Google API KEY以及Mapview控件基本方法的使用,还有让读者了解到使用google map的初步步骤,在下一讲中,将指导读者如何对地图上的位置进行标注。

更多相关文章

  1. 不同手机在豌豆夹上显示设备同名的解决方法 && Android(安卓)获
  2. 设置Android沉浸式状态栏颜色以及更改字体颜色 AndroidStatusBar
  3. 2016年末,Android岗位BAT等大厂面试题知识点小结(一)Android基础部
  4. 关于android各种双卡手机获取imei,imsi的处理(mtk,展讯,高通等)
  5. 【Android应用开发】-(21)Android中巧用反射解决程序兼容性问题
  6. Android(安卓)APN的设置问题
  7. Android更新UI的几种方式
  8. Android(安卓)记一次解决问题的过程:从源码中分析永远是解决问题
  9. Android大屏项目中的数字键盘输入界面的功能实现

随机推荐

  1. Android(安卓)开发最佳实践
  2. AndroidStudio 3.0 加载Android(安卓)sup
  3. ProgressBar的简单使用
  4. android防止屏幕关闭后后台服务停止运行
  5. ANDROID应用程序的混淆打包
  6. 2011.09.26(2)——— android sample之Note
  7. android开机动画bootanimation
  8. Android(安卓)编程下 Touch 事件的分发和
  9. 阅读《Android(安卓)从入门到精通》(2)——
  10. Android使用MQTT协议