本文将分为两部分,第一部分是指导用户使用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

  如下图所示:

Android基于LBS的地图开发:(一)Google地图的调用_第1张图片

步骤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菜单中,如下图,选择安装新软件:

Android基于LBS的地图开发:(一)Google地图的调用_第2张图片

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

Android基于LBS的地图开发:(一)Google地图的调用_第3张图片

接下来会加载相关的安装程序,并显示用户协议,选择接受所有用户协议后进行安装,安装成功后重新启动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基于LBS的地图开发:(一)Google地图的调用_第4张图片

其中,输入密码默认为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 获取当前位置并监听位置变化的完整代码:

public class MallFinderActivity extends MapActivity implements LocationListener {private MapController mapController;private MapView mapView;private LocationManager locationManager;private Location currentLocation;private GeoPoint currentPoint;    /** Called when the activity is first created. */    @Override    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();        //地图的放大缩小倍数为13        mapController.setZoom(13);        getLastLocation();         animateToCurrentLocation();     }        @Override    protected void onResume() {         super.onResume();        //更新地理位置信息        locationManager.requestLocationUpdates(getBestProvider(), 1000, 1, this);     }           @Override    protected void onPause() {         super.onPause();        //取消更新地理位置信息        locationManager.removeUpdates(this);     }    @Override    //用来做路线导航 ,这里我们不需要,因此只需要简单返回false即可protected boolean isRouteDisplayed() {// TODO Auto-generated method stubreturn false;}        @Override    //使用location 监听器去检测我们的位置变化public void onLocationChanged(Location newLocation) {// TODO Auto-generated method stub    setCurrentLocation(newLocation); }@Overridepublic void onProviderDisabled(String provider) {// TODO Auto-generated method stub}@Overridepublic void onProviderEnabled(String provider) {// TODO Auto-generated method stub}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {// TODO Auto-generated method stub}        public void getLastLocation(){         String provider = getBestProvider();         currentLocation = locationManager.getLastKnownLocation(provider);         if(currentLocation != null){             setCurrentLocation(currentLocation);//当前经纬度*10的6次方         }         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);         criteria.setAccuracy(Criteria.ACCURACY_FINE);//使用最精确的provider        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);         currentPoint = new GeoPoint(currLatitude,currLongitude);               currentLocation = new Location("");         currentLocation.setLatitude(currentPoint.getLatitudeE6() / 1e6);         currentLocation.setLongitude(currentPoint.getLongitudeE6() / 1e6);     }}

  

小结

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


更多相关文章

  1. Android中的服务有几种,具体实现步骤,这几种之间的区别
  2. Android(OPhone) 学习笔记 - 地图(1)
  3. 百度地图定位SDKv4.2版本(android)的使用教程
  4. Android自定义View的基本步骤和使用自定义属性
  5. Android 位置服务——BaiduLocation的使用
  6. Android使用第三方SDK——百度地图
  7. 自定义RadioButton样式并去除默认样式位置【Android】
  8. ReactNative Android 实现加载本地图片

随机推荐

  1. Android 获取本机安装的应用程序
  2. Writing Efficient Android Code(转)
  3. Android 用java语言执行Shell命令
  4. android仿178折网购社区
  5. Android(安卓)mount/unmount SD卡
  6. Android中在UI中加入线条
  7. Android 打开TextView中的超链接
  8. Android 系统自带Activity效果
  9. Android 水平的ListView
  10. AndroidStudio 编译项目报错 Android(安