




已阅读5页,还剩19页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Android Map API使用文档 文档编号:名 称:编 写:编写日期:2010-11-30审 核:修 改 人:审核日期:修改日期:目 录1准备工作31.1申请 Android Map API key31.2 创建基于 Google APIs AVD51.3 创建基于 Google APIs 工程61.4 连接手机及驱动的安装62 Google Map API的使用82.1编写最简单的android谷歌地图应用92.2在android map overlay中使用图片103 定位功能173.1定位API中主要的类173.2如何进行定位183.3添加权限204搜索的使用201准备工作要想编写Google Map的应用首先需要 Google APIs 。需要在项目中导入Google map api。默认情况是android某个版本比如android 2.1,现在需要改为对应版本的Google apis,版本要和android版本一致。在默认的情况下Google API是没有的。这个google apis是同版本的android超集,包含了google的私有应用api。比如:这样就可以在项目中使用比如:import com.google.android.maps.MapActivity;1.1申请 Android Map API keygoogle需要一个签名指纹的机制,要先到google注册,并把这个指纹包含在应用中,才可以下载到地图信息。也就是说每次下载地图信息要带着这个指纹信息。指纹信息的注册和获取都是免费的。首先,必须要有Google的账号,如果没有可以申请一个,下面是如何申请Android MapAPI key。步骤1:找到debug.keystore文件。使用Eclipse开发时,选择Windows-Preference如图:步骤2:取得debug.keystore 的MD5值 在cmd中执行: (可以先定位到jdk的bin目录下)keytool -list -alias androiddebugkey -keystore C:Usershey_Jude.androiddebug.keystore -storepass android -keypass android01:E4:D2:09:E4:9F:96:3F:B5:79:80:22:C2:37:C7:9F 双引号中的为你keystore位置, (一定要有双引号),否则就会报错。如果系统提示找不到keytool,需要将jdk中bin目录加入到环境变量中。于是就得到了指纹认证。步骤3: 申请 Android Map的API key打开网页:/intl/zh-CN/android/maps-api-signup.html49:1B:7C:E0:41:31:52:2F:FD:26:6C:EC:46:E3:30:850S4StgK_VfPCC6nrszX2fMlDeJbymTsTleEAxyQ认证指纹粘贴到如下图的文本域中点击按钮于是就申请到了我们想要的Android Map的API key。1.2 创建基于 Google APIs AVD打开AVD 管理界面,进行创建,如图:1.3 创建基于 Google APIs 工程创建基于Google apis的工程如图:1.4 连接手机及驱动的安装连接手机进行测试时,需要安装驱动,大多数手机的驱动在如图中的目录中都能够找的到,在usb_driver目录中,但有些手机在此目录中提示无法找到驱动,此时就需要修改一下android_winusb.inf的配置文件:图一的上面为修改过的,下面的是没有修改的,如图1只是修改了%SingleAdbInterface% = USB_Install, USBVID_413C&PID_B005这里的部分,在安装驱动是可以看到.也可以在设备管理器中找到如图2 (1)(2) (3)2 Google Map API的使用下面的连接是Google Map API的地址:/intl/ja/android/add-ons/google-apis/reference/index.html这里面有一些重要的类:LocationManager: 本类提供访问定位服务的功能,也提供 获取最佳定位提供者的功能。另外,临近警报功能 (前面所说的那种功能)也可以借助该类来实现。LocationProvider: 该类是定位提供者的抽象类。定位提供者具备周期性报告设备地理位置的功能。LocationListener: 提供定位信息发生改变时的回调功能。必须事先在定位管理器中注册监听器对象。Criteria:该类使得应用能够通过在LocationProvider中设置的属性来选择合适的定位提供者。Android也提供了一组访问 Google MAP的API,借助Google MAP及定位API,我们就能在地图上显示用户当前的地理位置:Google Map APIAndroid中定义了一个名为com.google.android.maps的包,其中包含了一系列用于在Google Map上显示,控制和层叠信息的功能类,以下是该包中最重要的几个类:MapActivity: 这个类是用于显示Google MAP的Activity类,它需要连接底层网络。MapView: MapView是用于显示地图的View组件。它必须和MapActivity配合使用。MapController : MapController用于控制地图的移动。Overlay:这是一个可显示于地图之上的可绘制的对象。GeoPoint: 这个一个包含经纬度位置的对象。2.1编写最简单的android谷歌地图应用这是在布局文件中的代码:android:apiKey这个标签指的是在上面申请的API key 代码其实很简单:public class LocationActivity extends MapActivity private MapView mapView; private MapController mapController; Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); mapView = (MapView) findViewById(R.id.map_view); Log.i(welcome, created map activity.); Override protected boolean isRouteDisplayed() return false; 在模拟器上的正常显示,在家的时候。但公司就不出来了,可能是和代理这里有一定的关系。连接好手机后就可以进行发布。发布的过程的也遇到了小小的麻烦,要注意手机的系统也需要包含Google APIs 。第一次使用的是dell的手机,操作系统是oms的是移动的定制机,就遇到了这样的问题。然后换了一个摩托的里程碑,是发布上去了,但地图始终不显示,没有找到原因。最后用了个G1 简单的地图总算出来了。有了这个简单的应用就可以对它进行更多的操作了。2.2在android map overlay中使用图片2.2.1 继承Overlay在android编写最简单的地标提示Overlay中使用的是文字提示,还可以设置图片,比如这样:代码如下:package com.lingtu.gmap.activity;import java.util.List;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Point;import android.os.Bundle;import com.google.android.maps.GeoPoint;import com.google.android.maps.MapActivity;import com.google.android.maps.MapController;import com.google.android.maps.MapView;import com.google.android.maps.Overlay;public class Activity01 extends MapActivityprivate MapView mMapView;private MapController mMapController; private GeoPoint mGeoPoint;/* Called when the activity is first created. */public void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.main);mMapView = (MapView) findViewById(R.id.MapView01);/设置为交通模式/mMapView.setTraffic(true);/设置为卫星模式mMapView.setSatellite(true); /设置为街景模式/mMapView.setStreetView(false);/取得MapController对象(控制MapView)mMapController = mMapView.getController(); mMapView.setEnabled(true);mMapView.setClickable(true);/设置地图支持缩放mMapView.setBuiltInZoomControls(true); / 北京的位置mGeoPoint = new GeoPoint(int) (39.9 * 1E6), (int) (116.3 * 1E6);/定位到北京mapController.animateTo(mGeoPoint);/设置倍数(1-21)mMapController.setZoom(12); /添加Overlay,用于显示标注信息 MyLocationOverlay myLocationOverlay = new MyLocationOverlay(); List list = mMapView.getOverlays(); list.add(myLocationOverlay);protected boolean isRouteDisplayed()return false;class MyLocationOverlay extends Overlaypublic boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)super.draw(canvas, mapView, shadow);Paint lingtupaint = new Paint();Point lingtuScreenCoords = new Point();/ 将经纬度转换成实际屏幕坐标GeoPoint lingtuGeoPoint = new GeoPoint(int) (40.05122 * 1E6),(int) (116.28923 * 1E6);mapView.getProjection().toPixels(lingtuGeoPoint, lingtuScreenCoords);lingtupaint.setStrokeWidth(1);lingtupaint.setARGB(255, 255, 0, 0);lingtupaint.setStyle(Paint.Style.STROKE);Bitmap bmpLingtu = BitmapFactory.decodeResource(getResources(),R.drawable.lingtu);/绘制图片canvas.drawBitmap(bmpLingtu, lingtuScreenCoords.x,lingtuScreenCoords.y, lingtupaint);/绘制文字canvas.drawText(灵图, lingtuScreenCoords.x, lingtuScreenCoords.y, lingtupaint);2.2.2 继承ItemizedOverlay当有多个点需要在地图上绘制时,自己的类就不继承Overlay,而是要继承ItemizedOverlaypackage com.lingtu.gmap.activity;import java.util.ArrayList;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Point;import android.graphics.drawable.Drawable;import com.google.android.maps.ItemizedOverlay;import com.google.android.maps.MapView;import com.google.android.maps.OverlayItem;import com.google.android.maps.Projection;public class GeoItemizedOverlay extends ItemizedOverlay private ArrayList mOverlays = new ArrayList();private Context context;public GeoItemizedOverlay(Drawable defaultMarker) super(defaultMarker);Overrideprotected OverlayItem createItem(int i) return mOverlays.get(i);Overridepublic int size() / TODO Auto-generated method stubreturn mOverlays.size();public void addOverlay(OverlayItem overlay) mOverlays.add(overlay);/ 在一个新ItemizedOverlay上执行所有处理的工具方法。populate();public void draw(Canvas canvas, MapView mapView, boolean shadow) Projection projection = mapView.getProjection();for (int index = size() - 1; index = 0; index-) OverlayItem overLayItem = getItem(index);/* 题目 */String title = overLayItem.getTitle();/* 简介 */String snippet = overLayItem.getSnippet();/* 象素点取得转换 */Point point = projection.toPixels(overLayItem.getPoint(), null);/* 目标城市圈出来 */这里画出了一个黄点/Paint paintCircle = new Paint();/paintCircle.setColor(Color.YELLOW);/canvas.drawCircle(point.x, point.y, 5, paintCircle);Paint paint = new Paint();paint.setStrokeWidth(1);paint.setARGB(255, 255, 0, 0);paint.setStyle(Paint.Style.STROKE);/ 消除锯齿paint.setFlags(Paint.ANTI_ALIAS_FLAG);paint.setTextSize(16);Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.mark);canvas.drawBitmap(bmp, point.x - bmp.getWidth() / 2, point.y- bmp.getHeight() / 2, paint);/* 文字设置 */Paint paintText = new Paint();paintText.setColor(Color.RED);paintText.setTextSize(25);canvas.drawText(title + snippet, point.x, point.y, paintText);super.draw(canvas, mapView, shadow);public void setContent(Context context) this.context=context;此时就可以把这个类添加到MapView中:GeoItemizedOverlay itemizedOverlay;private List list;public void onCreate(Bundle savedInstanceState)super.onCreate(savedInstanceState);setContentView(R.layout.main);mMapView = (MapView) findViewById(R.id.MapView01);/设置为交通模式/mMapView.setTraffic(true);/设置为卫星模式mMapView.setSatellite(true); /设置为街景模式/mMapView.setStreetView(false);/取得MapController对象(控制MapView)mMapController = mMapView.getController(); mMapView.setEnabled(true);mMapView.setClickable(true);/设置地图支持缩放mMapView.setBuiltInZoomControls(true); / 北京的位置mGeoPoint = new GeoPoint(int) (39.9 * 1E6), (int) (116.3 * 1E6);/定位到北京mapController.animateTo(mGeoPoint);/设置倍数(1-21)mMapController.setZoom(12); List list = mMapView.getOverlays();itemizeOverlay(); public void itemizeOverlay() drawable = this.getResources().getDrawable(R.drawable.mark);itemizedOverlay = new GeoItemizedOverlay(drawable);itemizedOverlay.setContent(this);GeoPoint point1 = new GeoPoint(int) (39.97 * 1000000),(int) (116.39 * 1000000);GeoPoint point2 = new GeoPoint(int) (39.93 * 1000000),(int) (116.33 * 1000000);/ 设置初始地图的中心位置GeoPoint geoBeijing = new GeoPoint(int) (39.95 * 1000000),(int) (116.37 * 1000000);OverlayItem bjItem = new OverlayItem(geoBeijing, JQQ,Bei jing Welcome you !);OverlayItem overlayitem1 = new OverlayItem(point1, DHZ,what are you doing now ?);overlayitem1.setMarker(drawable);OverlayItem overlayitem2 = new OverlayItem(point2, SRH,where are you from ?);overlayitem1.setMarker(drawable);itemizedOverlay.addOverlay(overlayitem1);itemizedOverlay.addOverlay(overlayitem2);itemizedOverlay.addOverlay(bjItem);list.add(itemizedOverlay);3 定位功能3.1定位API中主要的类关于地理定位的API全部位于android.location的包内。其中包含以下几个重要的功能: LocationManager:Android中通过LocationManager来获取地理位置等相关信息的。另外,临近警报的功能也可以借助该类来实现。LoctionProvider: 该类是定位提供者的抽象类。定位提供者具备周期性报告设备地理位置的功能。LocationListener:提供定位信息发生改变时的回调功能。必须事先在定位管理器中注册监听器对象。Criteria:该类使得应用能够通过在LoctionProvider中设置属性来选择合适的定位提供者。Geocoder:用于处理地理编码和反向地理编码的类。地理编码是指将地址或其它描述转换变为经度和纬度,反向编码则相反,通俗讲就是输入经纬度,找到相应的地址语言描述。3.2如何进行定位要使用地理定位首先需要取得LocationManager的实例,在android中,获得LocationManager的唯一方法是通过getSystemService()方法的调用。通过LocationManager,我们可以获得一个位置的提供者的列表。在一个真实的手持设备中,这个列表包含了一些GPs的服务。我们也可以选择更强大更精准的,不带其他附加服务的GPS。代码如下:String context = Context.LOCATION_SERVICE;LocationManager locationManager = (LocationManager) getSystemService(context);取得之后,我们还需要注册一个周期性的更新视图,调用其上的requestLocationUpdates方法,注意:这里需要GPS和NetWork都要请求更新location,前者为WIFI/基站定位,后者为GPS定位,当一种定位方式不能使用的使用,还可以使用另外一种:locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10, mLocationListener01);locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, mLocationListener01);编写获取location信息的方法,在这里先使用GPS定位方式获取location信息,如果获取不到就使用WIFI/基站方式获取:public static Location getLocation(Context context) LocationManager locMan = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); Location location = locMan .getLastKnownLocation(LocationManager.GPS_PROVIDER); if(location=null) location = locMan .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); Log.i(TAG, -location: + location); return location; requestLocationUpdates方法,第一个参数设置服务的提供者,第二个是周期,第三个最小的距离间隔,最后一个是个监听器:.创建一个LocationListener接口的实现类,可以使用匿名类,代码如下:private final LocationListener locationListener = new LocationListener() /当坐标改变时触发此函数,如果传入的相同它就不会被触发public void onLocationChanged(Location location) /Provider禁用时触发此函数,比如GPS关闭。public void onProviderDisabled(String provider) /和上面相反public void onProviderEnabled(String provider) /Provider的状态在可用,暂时不可用和无服务三个主题直接切换时触发此函数。public void onStatusChanged(String provider, int status, Bundle extras) ;3.3添加权限要使用定位的API,首先需要在AndroidManifext.xml文件中添加器权限,具体代码如下: 4搜索的使用在menu中添加了搜索的菜单当点击搜索时会出现这样的文本输入框。如图:/搜索的方法,传入名字进行查找public void searchName(String nameStr) try /1指的是查到的最大结果数是一个List addresses = mGeocoder.getFromLocationName(nameStr, 1);if (addresses.size() != 0) Address address = addresses.get(0);GeoPoint geoPoint = new GeoPoint(int) (address.getLatitude() * 1E6), (int) (address.getLongitude() * 1E6);/ 复制一个新的Location对象Location temLoaction = new Location(location);temLoaction.setLatitude(address.getLatitude();temLoaction.setLongitude(address.getLongitude();/ 搜索到位置的图层searchLocationOverlay = new LocationOverlay();searchLocationOverlay.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论