一:申请key:

1.首先找到debugkeystore位置:

  打开Eclipse--->Windows--->Preferences--->Android--->Build

一般是这样的路径C:\DocumentsandSettings\Administrator\.android\debug.keystore

2.cmd中执行

keytool-list-aliasandroiddebugkey-keystore"C:\DocumentsandSettings\Administrator\.android\debug.keystore"-storepassandroid-keypassandroid


得到认证指纹(MD5)6F:C9:41:48:A5:F3:36:A5:D3:DD:B5:D1:CB:AC:47:88

3.打开申请key页面http://code.google.com/android/maps-api-signup.html

复制认证指纹(MD5):到下面的Mycertificate'sMD5fingerprint

4.然后点击GenerateApikey

5.等到apikey:0Mg_koWoyZUhlluO4-i6-bq9WYMFbxKodZZMz2Q

二:设计main.xml如下:(将申请到得key设置在com.google.android.maps.MapView中如下所示

Xml代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <FrameLayout
  7. android:id="@+id/map_layout"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:orientation="vertical">
  11. <com.google.android.maps.MapView
  12. android:id="@+id/map_view"
  13. android:layout_width="fill_parent"
  14. android:layout_height="fill_parent"
  15. android:apiKey="0Mg_koWoyZUhlluO4-i6-bq9WYMFbxKodZZMz2Q"
  16. android:clickable="true"
  17. android:enabled="true"/>
  18. <LinearLayout
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_gravity="center"
  22. android:orientation="vertical"
  23. android:paddingBottom="105dip">
  24. <TextView
  25. android:id="@+id/map_bubbleText"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:background="@drawable/location_tips"
  29. android:gravity="left|center"
  30. android:maxEms="12"
  31. android:paddingLeft="12dip"
  32. android:paddingRight="10dip"
  33. android:text="@string/load_tips"
  34. android:textColor="#cfcfcf"
  35. android:textSize="14sp"/>
  36. </LinearLayout>
  37. <LinearLayout
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:layout_gravity="center"
  41. android:orientation="vertical">
  42. <ImageView
  43. android:id="@+id/point_image"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:layout_gravity="center"
  47. android:layout_marginBottom="30dip"
  48. android:src="@drawable/point_start"/>
  49. </LinearLayout>
  50. </FrameLayout>
  51. </LinearLayout>

三:创建MyLocationManager类主要用于管理经纬度获取方法实现

Java代码
  1. packagecom.android.map;
  2. importandroid.content.Context;
  3. importandroid.location.Location;
  4. importandroid.location.LocationListener;
  5. importandroid.location.LocationManager;
  6. importandroid.os.Bundle;
  7. importandroid.util.Log;
  8. publicclassMyLocationManager{
  9. privatefinalStringTAG="FzLocationManager";
  10. privatestaticContextmContext;
  11. privateLocationManagergpsLocationManager;
  12. privateLocationManagernetworkLocationManager;
  13. privatestaticfinalintMINTIME=2000;
  14. privatestaticfinalintMININSTANCE=2;
  15. privatestaticMyLocationManagerinstance;
  16. privateLocationlastLocation=null;
  17. privatestaticLocationCallBackmCallback;
  18. publicstaticvoidinit(Contextc,LocationCallBackcallback){
  19. mContext=c;
  20. mCallback=callback;
  21. }
  22. privateMyLocationManager(){
  23. //Gps定位
  24. gpsLocationManager=(LocationManager)mContext
  25. .getSystemService(Context.LOCATION_SERVICE);
  26. LocationgpsLocation=gpsLocationManager
  27. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  28. gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
  29. MINTIME,MININSTANCE,locationListener);
  30. //基站定位
  31. networkLocationManager=(LocationManager)mContext
  32. .getSystemService(Context.LOCATION_SERVICE);
  33. LocationnetworkLocation=gpsLocationManager
  34. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  35. networkLocationManager.requestLocationUpdates(
  36. LocationManager.NETWORK_PROVIDER,MINTIME,MININSTANCE,
  37. locationListener);
  38. }
  39. publicstaticMyLocationManagergetInstance(){
  40. if(null==instance){
  41. instance=newMyLocationManager();
  42. }
  43. returninstance;
  44. }
  45. privatevoidupdateLocation(Locationlocation){
  46. lastLocation=location;
  47. mCallback.onCurrentLocation(location);
  48. }
  49. privatefinalLocationListenerlocationListener=newLocationListener(){
  50. publicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras){
  51. }
  52. publicvoidonProviderEnabled(Stringprovider){
  53. }
  54. publicvoidonProviderDisabled(Stringprovider){
  55. }
  56. publicvoidonLocationChanged(Locationlocation){
  57. Log.d(TAG,"onLocationChanged");
  58. updateLocation(location);
  59. }
  60. };
  61. publicLocationgetMyLocation(){
  62. returnlastLocation;
  63. }
  64. privatestaticintENOUGH_LONG=1000*60;
  65. publicinterfaceLocationCallBack{
  66. /**
  67. *当前位置
  68. *@paramlocation
  69. */
  70. voidonCurrentLocation(Locationlocation);
  71. }
  72. publicvoiddestoryLocationManager(){
  73. Log.d(TAG,"destoryLocationManager");
  74. gpsLocationManager.removeUpdates(locationListener);
  75. networkLocationManager.removeUpdates(locationListener);
  76. }
  77. }

四:创建MyMapOverlay抽象类,并继承Overlay,创建抽象方法

changePoint(GeoPointnewPoint,inttype)用于回调重新获取到的GeoPoint重新定位地图,并获取地址信息

Java代码
  1. importandroid.view.MotionEvent;
  2. importcom.google.android.maps.GeoPoint;
  3. importcom.google.android.maps.MapView;
  4. importcom.google.android.maps.Overlay;
  5. //覆盖整个地图捕捉触控事件的OverLay
  6. publicabstractclassMyMapOverlayextendsOverlay{
  7. privateintpoint_X;
  8. privateintpoint_Y;
  9. privateGeoPointnewPoint;
  10. publicMyMapOverlay(intx,inty){
  11. point_X=x;
  12. point_Y=y;
  13. }
  14. booleanflagMove=false;
  15. //触控屏幕移动地图,重新根据屏幕中心点获取该点经纬度
  16. @Override
  17. publicbooleanonTouchEvent(MotionEventevent,MapViewmapView){
  18. System.out.println("X->"+event.getX()+":"+point_X);
  19. System.out.println("Y->"+event.getY()+":"+point_Y);
  20. if(event.getAction()==MotionEvent.ACTION_DOWN){
  21. changePoint(newPoint,1);
  22. }elseif(event.getAction()==MotionEvent.ACTION_UP){
  23. newPoint=mapView.getProjection().fromPixels(point_X,point_Y);
  24. changePoint(newPoint,2);
  25. }
  26. returnfalse;
  27. }
  28. publicabstractvoidchangePoint(GeoPointnewPoint,inttype);
  29. }

五:MyMapActivity继承MapActivity类并实现经纬度获取回调接口LocationCallBack。项目实现如下:

Java代码
  1. packagecom.android.googlemap;
  2. importjava.io.IOException;
  3. importjava.util.List;
  4. importjava.util.Locale;
  5. importandroid.graphics.Rect;
  6. importandroid.location.Address;
  7. importandroid.location.Geocoder;
  8. importandroid.location.Location;
  9. importandroid.os.Bundle;
  10. importandroid.os.Handler;
  11. importandroid.os.Message;
  12. importandroid.view.View;
  13. importandroid.view.Window;
  14. importandroid.widget.TextView;
  15. importcom.android.map.MyLocationManager;
  16. importcom.android.map.MyLocationManager.LocationCallBack;
  17. importcom.android.map.MyMapOverlay;
  18. importcom.google.android.maps.GeoPoint;
  19. importcom.google.android.maps.MapActivity;
  20. importcom.google.android.maps.MapController;
  21. importcom.google.android.maps.MapView;
  22. importcom.google.android.maps.Overlay;
  23. publicclassMyMapActivityextendsMapActivityimplementsLocationCallBack{
  24. privateMapViewmapView;
  25. privateMapControllermMapCtrl;
  26. privateMyLocationManagermyLocation;
  27. privateList<Overlay>mapOverlays;
  28. publicGeoPointlocPoint;
  29. privateMyMapOverlaymOverlay;
  30. privateTextViewdesText;
  31. privateStringlost_tips;
  32. privateintpoint_X;
  33. privateintpoint_Y;
  34. privateintstatusBarHeight;
  35. publicfinalintMSG_VIEW_LONGPRESS=10001;
  36. publicfinalintMSG_VIEW_ADDRESSNAME=10002;
  37. publicfinalintMSG_GONE_ADDRESSNAME=10003;
  38. @Override
  39. publicvoidonCreate(BundlesavedInstanceState){
  40. super.onCreate(savedInstanceState);
  41. requestWindowFeature(Window.FEATURE_NO_TITLE);
  42. setContentView(R.layout.main);
  43. mapView=(MapView)findViewById(R.id.map_view);
  44. desText=(TextView)this.findViewById(R.id.map_bubbleText);
  45. lost_tips=getResources().getString(R.string.load_tips);
  46. mapView.setBuiltInZoomControls(true);
  47. mapView.setClickable(true);
  48. mMapCtrl=mapView.getController();
  49. point_X=this.getWindowManager().getDefaultDisplay().getWidth()/2;
  50. point_Y=this.getWindowManager().getDefaultDisplay().getHeight()/2;
  51. mOverlay=newMyMapOverlay(point_X,point_Y){
  52. @Override
  53. publicvoidchangePoint(GeoPointnewPoint,inttype){
  54. if(type==1){
  55. mHandler.sendEmptyMessage(MSG_GONE_ADDRESSNAME);
  56. }else{
  57. locPoint=newPoint;
  58. mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS);
  59. }
  60. }
  61. };
  62. mapOverlays=mapView.getOverlays();
  63. if(mapOverlays.size()>0){
  64. mapOverlays.clear();
  65. }
  66. mapOverlays.add(mOverlay);
  67. mMapCtrl.setZoom(12);
  68. MyLocationManager.init(MyMapActivity.this.getApplicationContext(),
  69. MyMapActivity.this);
  70. myLocation=MyLocationManager.getInstance();
  71. }
  72. @Override
  73. protectedvoidonResume(){
  74. super.onResume();
  75. }
  76. @Override
  77. protectedvoidonPause(){
  78. super.onPause();
  79. }
  80. @Override
  81. protectedbooleanisRouteDisplayed(){
  82. //TODOAuto-generatedmethodstub
  83. returnfalse;
  84. }
  85. publicvoidonCurrentLocation(Locationlocation){
  86. if(locPoint==null){
  87. locPoint=newGeoPoint((int)(location.getLatitude()*1E6),
  88. (int)(location.getLongitude()*1E6));
  89. mHandler.sendEmptyMessage(MSG_VIEW_LONGPRESS);
  90. }
  91. }
  92. publicvoidchangePoint(GeoPointlocPoint){
  93. }
  94. /**
  95. *通过经纬度获取地址
  96. *
  97. *@parampoint
  98. *@return
  99. */
  100. privateStringgetLocationAddress(GeoPointpoint){
  101. Stringadd="";
  102. GeocodergeoCoder=newGeocoder(getBaseContext(),Locale.getDefault());
  103. try{
  104. List<Address>addresses=geoCoder.getFromLocation(
  105. point.getLatitudeE6()/1E6,point.getLongitudeE6()/1E6,
  106. 1);
  107. Addressaddress=addresses.get(0);
  108. intmaxLine=address.getMaxAddressLineIndex();
  109. if(maxLine>=2){
  110. add=address.getAddressLine(1)+address.getAddressLine(2);
  111. }else{
  112. add=address.getAddressLine(1);
  113. }
  114. }catch(IOExceptione){
  115. add="";
  116. e.printStackTrace();
  117. }
  118. returnadd;
  119. }
  120. /**
  121. *
  122. *用线程异步获取
  123. */
  124. RunnablegetAddressName=newRunnable(){
  125. publicvoidrun(){
  126. StringaddressName="";
  127. while(true){
  128. addressName=getLocationAddress(locPoint);
  129. if(!"".equals(addressName)){
  130. break;
  131. }
  132. }
  133. Messagemsg=newMessage();
  134. msg.what=MSG_VIEW_ADDRESSNAME;
  135. msg.obj=addressName;
  136. mHandler.sendMessage(msg);
  137. }
  138. };
  139. privateHandlermHandler=newHandler(){
  140. @Override
  141. publicvoidhandleMessage(Messagemsg){
  142. switch(msg.what){
  143. caseMSG_VIEW_LONGPRESS://处理长按时间返回位置信息
  144. {
  145. if(null==locPoint)
  146. return;
  147. newThread(getAddressName).start();
  148. desText.setVisibility(View.VISIBLE);
  149. desText.setText(lost_tips);
  150. mMapCtrl.animateTo(locPoint);
  151. mapView.invalidate();
  152. }
  153. break;
  154. caseMSG_VIEW_ADDRESSNAME:
  155. desText.setText((String)msg.obj);
  156. desText.setVisibility(View.VISIBLE);
  157. if(statusBarHeight==0){
  158. Rectframe=newRect();
  159. getWindow().getDecorView().getWindowVisibleDisplayFrame(
  160. frame);
  161. statusBarHeight=frame.top;
  162. point_Y-=statusBarHeight/2;
  163. }
  164. break;
  165. caseMSG_GONE_ADDRESSNAME:
  166. desText.setVisibility(View.GONE);
  167. break;
  168. }
  169. }
  170. };
  171. //关闭程序也关闭定位
  172. @Override
  173. protectedvoidonDestroy(){
  174. //TODOAuto-generatedmethodstub
  175. super.onDestroy();
  176. myLocation.destoryLocationManager();
  177. }
  178. }

六:AndroidManifest.xml中不要忘了要添加访问网络和启动定位等的几个权限已经google地图库

Xml代码
  1. <uses-permissionandroid:name="android.permission.INTERNET"/>
  2. <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
  3. <uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/>
  4. <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
  5. <!--在application里面添加google地图库如下(一定要记得添加):-->
  6. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  7. <uses-libraryandroid:name="com.google.android.maps"/>
  8. <activityandroid:name="com.android.googlemap.MyMapActivity"android:screenOrientation="portrait"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <actionandroid:name="android.intent.action.MAIN"/>
  12. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  13. </intent-filter>
  14. </activity>
  15. </application>

七:运行效果:

1


2以后后重新获取如下:


提示:如果没有出现地图只出现地址信息背景是网格的,那就是key的问题,根据开始的方法找到自己开发的eclipse中sdk的debugkeystore位置重新申请key即可

更多相关文章

  1. Android(安卓)呼吸灯流程分析(一)
  2. 巧解Android时区加载过慢的问题
  3. Android(安卓)Native C development (2) -- framebuffer
  4. [置顶] Android(安卓)《手机卫士》随听笔记
  5. android studio 获取证书指纹 SHA1
  6. Android(安卓)开发常用代码
  7. Android(安卓)Fragment的增加,删除,添加
  8. Android基础笔记(五)-网络编程
  9. android与javascript交互调用

随机推荐

  1. 错误 'roundIcon' in package 'android'
  2. 读取android手机流量信息
  3. ViewModel、ViewModelProviders、ViewMod
  4. android 状态栏和导航栏(status and navi
  5. Android(安卓)Studio 启动 无缘故报 Erro
  6. Android中改变Dialog背景透明度
  7. android中的状态栏
  8. Android中文API(95)——SimpleExpandableLi
  9. 在设置里面增加关闭和打开GPS ,数据流量的
  10. 原:Android(安卓)命令行手动编译打包详解