如何在Android手机上进行Google Map的开发。

1.题记

提起谷歌Map相信大家都不会陌生,那进入我们今天的话题,如何在Android手机上进行Google Map的开发。

2.Map应用程序的开发

2.1 准备工作

2.1.1 申请Android Map API KEY

         步骤一: 找到你的debug.keystore文件,在Eclipse 首选项中可以看到该文件。如下图:

 


如何在Android手机上进行Google Map的开发。

             步骤二:取得debug.keystore的MD5值

             在命令行下进入debug.keystore文件所在的路径,执行命令:keytool -list -keystore debug.keystore,会提示输入密码,输入默认密码“android”,即可取得MD5值。

              步骤三:申请Android Map的API key。

              在浏览器重输入网址:http://code.google.com/intl/zh-CN/android/maps-api-signup.html,登录Google账号,输入步骤2中得到的MD5值,即可申请到API Key。记下API Key。

2.1.2 创建基于Google APIs的AVD

       在Eclipse中打开AVD 界面,创建AVD,选择Target为Google APIs的项,如下图:


如何在Android手机上进行Google Map的开发。

若在Target处无Google APIs选项,请自行添加maps.jar文件。

2.1.3 创建基于Google APIs的工程(略),即选择Build Target为Google APIs。

2.2 Google Map API的使用

  其类均在com.google.android.maps包中,一下是该包中几个重要的类:

MapActivity用于显示Google Map的Activity类,该类为抽象类,开发时请继承该类,并实现onCreate()方法。在该类中必须创建一个MapView实例。

MapView 用户显示地图的View组件.

MapController 用于控制地图的移动、缩放等

Overlay 这是一个可显示于地图上的可绘制的对象

GeoPoint 一个包含经纬度位置的对象

2.3 实例

2.3.1 创建工程,注意选择Build Target为“Google APIs”

2.3.2 修改AndroidManifest.xml文件,增加访问网络的权限

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

2.3.3 创建Map View,代码如下:

Xml代码  如何在Android手机上进行Google Map的开发。
  1. <com.google.android.maps.MapView  
  2.     android:id="@+id/MapView01"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:apiKey="0u7spJisVnJZmy3X6nX1M01SirYWYgNm-EQZbhQ"/>  

    其中APIKEY即为之前得到的APIkey。

2.3.4 实现MapActivity,代码和讲解如下:

Java代码  如何在Android手机上进行Google Map的开发。
  1. package com.sulang.android.map;  
  2.   
  3. import java.util.List;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Paint;  
  8. import android.graphics.Point;  
  9. import android.os.Bundle;  
  10.   
  11. import com.google.android.maps.GeoPoint;  
  12. import com.google.android.maps.MapActivity;  
  13. import com.google.android.maps.MapController;  
  14. import com.google.android.maps.MapView;  
  15. import com.google.android.maps.Overlay;  
  16.   
  17.   
  18. public class Activity01 extends MapActivity  
  19. {  
  20.     private MapView  mMapView;  
  21.     private MapController mMapController;   
  22.     private GeoPoint mGeoPoint;  
  23.     /** Called when the activity is first created. */  
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState)  
  26.     {  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.         mMapView = (MapView) findViewById(R.id.MapView01);  
  30.         //设置为交通模式  
  31.         //mMapView.setTraffic(true);  
  32.         //设置为卫星模式  
  33.         //mMapView.setSatellite(true);   
  34.         //设置为街景模式  
  35.         mMapView.setStreetView(false);  
  36.         //取得MapController对象(控制MapView)  
  37.         mMapController = mMapView.getController();   
  38.         mMapView.setEnabled(true);  
  39.         mMapView.setClickable(true);  
  40.         //设置地图支持缩放  
  41.         mMapView.setBuiltInZoomControls(true);   
  42.         //设置起点为成都  
  43.         mGeoPoint = new GeoPoint((int) (30.659259 * 1000000), (int) (104.065762 * 1000000));  
  44.         //定位到成都  
  45.         mMapController.animateTo(mGeoPoint);   
  46.         //设置倍数(1-21)  
  47.         mMapController.setZoom(12);   
  48.         //添加Overlay,用于显示标注信息  
  49.         MyLocationOverlay myLocationOverlay = new MyLocationOverlay();  
  50.         List<Overlay> list = mMapView.getOverlays();  
  51.         list.add(myLocationOverlay);  
  52.     }  
  53.     protected boolean isRouteDisplayed()  
  54.     {  
  55.         return false;  
  56.     }  
  57.     class MyLocationOverlay extends Overlay  
  58.     {  
  59.         @Override  
  60.         public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)  
  61.         {  
  62.             super.draw(canvas, mapView, shadow);  
  63.             Paint paint = new Paint();  
  64.             Point myScreenCoords = new Point();  
  65.             // 将经纬度转换成实际屏幕坐标  
  66.             mapView.getProjection().toPixels(mGeoPoint, myScreenCoords);  
  67.             paint.setStrokeWidth(1);  
  68.             paint.setARGB(25525500);  
  69.             paint.setStyle(Paint.Style.STROKE);  
  70.             Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.home);  
  71.             canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);  
  72.             canvas.drawText("**广场", myScreenCoords.x, myScreenCoords.y, paint);  
  73.             return true;  
  74.         }  
  75.     }  
  76. }  

2.3.5 启动模拟器,看效果图:


如何在Android手机上进行Google Map的开发。

 

至此关于Google Map的开发已完成,下面是GPS的开发。


3.GPS应用开发

3.1相关API说明

     关于地理定位系统的API全部位于android.location包内,其中包括以下几个重要的功能类:

      LocationManager:本类提供访问定位服务的功能,也提供了获取最佳定位提供者的功能。

      LocationProvider:该类是定位提供者的抽象类。定位提供者具备周期性报告设备地理位置的功能

      LocationListener:提供定位信息发生改变时的回调功能。必须事先在定位管理器中注册监听器对象。

      Criteria:该类是的应用能够通过在LocationProvider中设置的属性来选择合适的定位提供者.

      Geocider:用于处理地理编码和反向地理编码的类。

      要使用地理定位,首先需要取得LocationManager的实例:

Java代码  如何在Android手机上进行Google Map的开发。
  1. locationManager = (LocationManager) getSystemService(context);  

      取得LocationManager对象之后,还需要注册一个周期性的更新视图:

Java代码  如何在Android手机上进行Google Map的开发。
  1. locationManager.requestLocationUpdates(provider, 30000,locationListener);  

      其中第一个参数是设置服务提供者,第二个参数是周期。最后一个参数是用来监听定位信息的改变的。

3.2 具体实例

3.2.1 在AndroidManifest.xml文件中添加权限,代码如下:

 

Xml代码  如何在Android手机上进行Google Map的开发。
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.sulang.android.map" android:versionCode="1"  
  4.     android:versionName="1.0">  
  5.     <uses-sdk android:minSdkVersion="3" />  
  6.   
  7.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  8.         <uses-library android:name="com.google.android.maps" />  
  9.         <activity android:name=".Activity01" android:label="@string/app_name">  
  10.             <intent-filter>  
  11.                 <action android:name="android.intent.action.MAIN" />  
  12.                 <category android:name="android.intent.category.LAUNCHER" />  
  13.             </intent-filter>  
  14.         </activity>  
  15.     </application>  
  16.     <uses-permission android:name="android.permission.INTERNET" />  
  17.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
  18.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
  19. </manifest>  

3.2.2 给模拟器设置个默认的坐标值

 启动Eclipse ,选择Window ->Show View 打开 Emulator Control 界面即可进行设置。

3.2.3 实现MapActivity

 具体代码和讲解如下:

Java代码  如何在Android手机上进行Google Map的开发。
  1. package com.sulang.android.map;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.List;  
  5. import java.util.Locale;  
  6.   
  7. import android.content.Context;  
  8. import android.graphics.Bitmap;  
  9. import android.graphics.BitmapFactory;  
  10. import android.graphics.Canvas;  
  11. import android.graphics.Paint;  
  12. import android.graphics.Point;  
  13. import android.location.Address;  
  14. import android.location.Criteria;  
  15. import android.location.Geocoder;  
  16. import android.location.Location;  
  17. import android.location.LocationListener;  
  18. import android.location.LocationManager;  
  19. import android.os.Bundle;  
  20. import android.view.Menu;  
  21.   
  22. import com.google.android.maps.GeoPoint;  
  23. import com.google.android.maps.MapActivity;  
  24. import com.google.android.maps.MapController;  
  25. import com.google.android.maps.MapView;  
  26. import com.google.android.maps.Overlay;  
  27.   
  28. /* 
  29.  *@author 七里香的悔恨,2011-3-16 
  30.  *MyMapActivity.java 
  31.  *Blog:[url]http://bigboy.iteye.com/[/url] 
  32.  */  
  33. public class MyMapActivity extends MapActivity {  
  34.     public MapController mapController;  
  35.     public MyLocationOverlay myPosition;  
  36.     public MapView myMapView;  
  37.     private static final int ZOOM_IN = Menu.FIRST;  
  38.     private static final int ZOOM_OUT = Menu.FIRST + 1;  
  39.   
  40.     @Override  
  41.     protected boolean isRouteDisplayed() {  
  42.         return false;  
  43.     }  
  44.   
  45.     class MyLocationOverlay extends Overlay {  
  46.         Location mLocation;  
  47.   
  48.         // 在更新坐标时,设置该坐标,一边画图  
  49.         public void setLocation(Location location) {  
  50.             mLocation = location;  
  51.         }  
  52.   
  53.         @Override  
  54.         public boolean draw(Canvas canvas, MapView mapView, boolean shadow,  
  55.                 long when) {  
  56.             super.draw(canvas, mapView, shadow);  
  57.             Paint paint = new Paint();  
  58.             Point myScreenCoords = new Point();  
  59.             // 将经纬度转换成实际屏幕坐标  
  60.             GeoPoint tmpGeoPoint = new GeoPoint(  
  61.                     (int) (mLocation.getLatitude() * 1E6), (int) (mLocation  
  62.                             .getLongitude() * 1E6));  
  63.             mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);  
  64.             paint.setStrokeWidth(1);  
  65.             paint.setARGB(25525500);  
  66.             paint.setStyle(Paint.Style.STROKE);  
  67.             Bitmap bmp = BitmapFactory.decodeResource(getResources(),  
  68.                     R.drawable.home);  
  69.             canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);  
  70.             canvas.drawText("Here am I", myScreenCoords.x, myScreenCoords.y,  
  71.                     paint);  
  72.             return true;  
  73.   
  74.         }  
  75.   
  76.     }  
  77.   
  78.     @Override  
  79.     protected void onCreate(Bundle savedInstanceState) {  
  80.         super.onCreate(savedInstanceState);  
  81.         setContentView(R.layout.main);  
  82.         // 取得LocationManager实例  
  83.         LocationManager locationManager;  
  84.         String context = Context.LOCATION_SERVICE;  
  85.         locationManager = (LocationManager) getSystemService(context);  
  86.         myMapView = (MapView) findViewById(R.id.MapView01);  
  87.         // 取得MapController实例,控制地图  
  88.         mapController = myMapView.getController();  
  89.         myMapView.setEnabled(true);  
  90.         myMapView.setClickable(true);  
  91.         // 设置显示模式  
  92.         myMapView.setSatellite(true);  
  93.         myMapView.setStreetView(true);  
  94.         // 设置缩放控制  
  95.         myMapView.setBuiltInZoomControls(true);   
  96.         myMapView.displayZoomControls(true);  
  97.         // 设置使用MyLocationOverlay来绘图  
  98.         mapController.setZoom(17);  
  99.         myPosition = new MyLocationOverlay();  
  100.         List<Overlay> overlays = myMapView.getOverlays();  
  101.         overlays.add(myPosition);  
  102.         // 设置Criteria(服务商)的信息  
  103.         Criteria criteria = new Criteria();  
  104.         // 经度要求  
  105.         criteria.setAccuracy(Criteria.ACCURACY_FINE);  
  106.         criteria.setAltitudeRequired(false);  
  107.         criteria.setBearingRequired(false);  
  108.         criteria.setCostAllowed(false);  
  109.         criteria.setPowerRequirement(Criteria.POWER_LOW);  
  110.         // 取得效果最好的criteria  
  111.         String provider = locationManager.getBestProvider(criteria, true);  
  112.         // 得到坐标相关的信息  
  113.         Location location = locationManager.getLastKnownLocation(provider);  
  114.         // 更新坐标  
  115.         updateWithNewLocation(location);  
  116.         // 注册一个周期性的更新,3000ms更新一次  
  117.         // locationListener用来监听定位信息的改变  
  118.         locationManager.requestLocationUpdates(provider, 30000,  
  119.                 locationListener);  
  120.   
  121.     }  
  122.   
  123.     private void updateWithNewLocation(Location location) {  
  124.         String latLongString;  
  125.   
  126.         String addressString = "没有找到地址\n";  
  127.   
  128.         if (location != null) {  
  129.             // 为绘制标志的类设置坐标  
  130.             myPosition.setLocation(location);  
  131.             // 取得经度和纬度  
  132.             Double geoLat = location.getLatitude() * 1E6;  
  133.             Double geoLng = location.getLongitude() * 1E6;  
  134.             // 将其转换为int型  
  135.             GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue());  
  136.             // 定位到指定坐标  
  137.             mapController.animateTo(point);  
  138.             double lat = location.getLatitude();  
  139.             double lng = location.getLongitude();  
  140.             latLongString = "经度:" + lat + "\n纬度:" + lng;  
  141.   
  142.             double latitude = location.getLatitude();  
  143.             double longitude = location.getLongitude();  
  144.             // 更具地理环境来确定编码  
  145.             Geocoder gc = new Geocoder(this, Locale.getDefault());  
  146.             try {  
  147.                 // 取得地址相关的一些信息\经度、纬度  
  148.                 List<Address> addresses = gc.getFromLocation(latitude,  
  149.                         longitude, 1);  
  150.                 StringBuilder sb = new StringBuilder();  
  151.                 if (addresses.size() > 0) {  
  152.                     Address address = addresses.get(0);  
  153.                     for (int i = 0; i < address.getMaxAddressLineIndex(); i++)  
  154.                         sb.append(address.getAddressLine(i)).append("\n");  
  155.   
  156.                     sb.append(address.getLocality()).append("\n");  
  157.                     sb.append(address.getPostalCode()).append("\n");  
  158.                     sb.append(address.getCountryName());  
  159.                     addressString = sb.toString();  
  160.                 }  
  161.             } catch (IOException e) {  
  162.             }  
  163.         } else {  
  164.             latLongString = "没有找到坐标.\n";  
  165.         }  
  166.         // 显示  
  167.         // myLocationText.setText("你当前的坐标如下:\n"+latLongString+"\n"+addressString);  
  168.     }  
  169.   
  170.     private final LocationListener locationListener = new LocationListener() {  
  171.         // 当坐标改变时触发此函数  
  172.         public void onLocationChanged(Location location) {  
  173.             updateWithNewLocation(location);  
  174.         }  
  175.   
  176.         // Provider被disable时触发此函数,比如GPS被关闭  
  177.         public void onProviderDisabled(String provider) {  
  178.             updateWithNewLocation(null);  
  179.         }  
  180.   
  181.         // Provider被enable时触发此函数,比如GPS被打开  
  182.         public void onProviderEnabled(String provider) {  
  183.         }  
  184.   
  185.         // Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数  
  186.         public void onStatusChanged(String provider, int status, Bundle extras) {  
  187.         }  
  188.     };  
  189. }  

  
 

至此 GPS 应用开发完毕。

源代码

如何在Android手机上进行Google Map的开发。,布布扣,bubuko.com

如何在Android手机上进行Google Map的开发。

上一篇:手把手教你uiautomator_android自动化测试第一个示例


下一篇:Android基础类之BaseAdapter