最近正在做和地图相关的项目,想记录和整理一下的这方面的内容发出来,既是自己整理总结,也是和别人分享经验。

做过android 地图相关项目的同学估计都会有一些相同的需求,这些需求在android 上谷歌自己做的地图软件都做得很好,很多人想模仿参考来做,比如:

1、弹出浮动的搜索框,并能搜索地址并定位

2、长按地图出现当前位置的泡泡(popup),泡泡里有标题和内容,有详细地址和详细信息

3、自动定位到当前位置

4、显示各种图层

这么多需求不是一下子都能做出来的,而且做好了也不容易。

那这篇先写一些怎么把google地图添加到android程序中,还有把主界面显示做一下。

先看下主界面出来的效果:

这张图怎么样? 是不是长得和Google自己的地图软件一样啊,这个其实是我模仿做出来的,咱们山寨有力量,这点模仿算不了什么。

那开始进入代码阶段吧 。

一、申请key

网上有不少教你怎么添加地图的教程,我这里就不啰嗦太多了 ,简单的说一下

首先需要申请Android Map API Key,因为我们现在只要是进行测试熟悉Google map api的应用,所以可以使用Debug版的证明书即可

在不同的操作系统中,keystore位于如下位置:

  · Windows Vista: C:/Users//.android/debug.keystore

  · Windows XP: C:/Documents and Settings//.android/debug.keystore

  · OS X and Linux: ~/.android/debug.keystore

最后打开申请Key的网站:申请链接。

也可以参考这篇文章去申请Key :http://hb.qq.com/a/20110221/000009.htm

那到这里就假设拿到了Key了。

二、main.xml layout

我直接把mail.xml全贴出来,上面加注释就好了

[xhtml] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"android:layout_height="fill_parent"
  4. android:orientation="vertical">
  5. <LinearLayout
  6. android:layout_width="fill_parent"android:layout_height="wrap_content"
  7. android:orientation="horizontal"android:background="@drawable/searchbg">
  8. <!--这个就是搜索按钮了,你别看它想个输入框,其实是个button-->
  9. <Buttonandroid:id="@+id/search"android:background="@drawable/searchbtn"
  10. android:layout_marginLeft="12dp"
  11. android:layout_marginTop="5dp"
  12. android:layout_width="150dp"
  13. android:layout_height="35dp"
  14. android:hint="搜索地图"
  15. android:textSize="17sp"
  16. android:singleLine="true"
  17. android:gravity="center_vertical"
  18. android:textColor="#000000"/>
  19. <!--下面是三个图片按钮了,看效果图就知道是哪三个按钮了-->
  20. <ImageButtonandroid:background="@drawable/maptitlebtn"android:layout_marginLeft="15dp"
  21. android:id="@+id/pointwhat"android:src="@drawable/pointwhat"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. />
  25. <ImageButtonandroid:background="@drawable/maptitlebtn"android:layout_marginLeft="5dp"
  26. android:id="@+id/layer"android:src="@drawable/layer"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. />
  30. <ImageButtonandroid:background="@drawable/maptitlebtn"android:layout_marginLeft="5dp"
  31. android:id="@+id/loction"android:src="@drawable/loction"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. />
  35. </LinearLayout>
  36. <TableLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"
  37. android:orientation="vertical"
  38. >
  39. <TableRowandroid:layout_weight="1">
  40. <!--这里才是重点,放置map的地方-->
  41. <com.google.android.maps.MapView
  42. xmlns:android="http://schemas.android.com/apk/res/android"
  43. android:id="@+id/map_view"
  44. android:layout_width="fill_parent"
  45. android:layout_height="fill_parent"
  46. android:clickable="true"
  47. android:enabled="true"
  48. android:apiKey="0yRjCrET3v9ZkAhfn3wkRNzBPI_gHpkvx1iWT7g"/>
  49. </TableRow>
  50. <TableRow>
  51. </TableRow>
  52. </TableLayout>
  53. </LinearLayout>

里面的key要替换成你自己申请到的Key,要不然map在软件里出不来。

三 ,AndroidManifest.xml 文件怎么写?

最重要的是加上权限

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

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

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

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

还有这句话,加上android map的库。

<uses-library android:name="com.google.android.maps" />

[c-sharp] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.android.fzmap"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdkandroid:minSdkVersion="7"/>
  7. <uses-permissionandroid:name="android.permission.INTERNET"/>
  8. <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
  9. <uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/>
  10. <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
  11. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  12. <uses-libraryandroid:name="com.google.android.maps"/>
  13. <activityandroid:name="FzMapActivity"android:screenOrientation="portrait"
  14. android:label="@string/app_name">
  15. <intent-filter>
  16. <actionandroid:name="android.intent.action.MAIN"/>
  17. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  18. </intent-filter>
  19. </activity>
  20. </application>
  21. </manifest>

四、FzMapActivity

到这里就剩下 Activity的显示了。 这个Activity要继承MapActivity,不然你会死的很难看的。

不啰嗦,直接上代码

[java] view plain copy
  1. packagecom.android.fzmap;
  2. importjava.util.List;
  3. importandroid.graphics.Point;
  4. importandroid.graphics.drawable.Drawable;
  5. importandroid.location.Location;
  6. importandroid.os.Bundle;
  7. importandroid.util.Log;
  8. importandroid.view.View;
  9. importandroid.view.View.OnClickListener;
  10. importandroid.view.Window;
  11. importandroid.widget.ImageButton;
  12. importandroid.widget.TextView;
  13. importcom.android.fzmap.map.FzLocationManager;
  14. importcom.android.fzmap.map.FzLocationManager.LocationCallBack;
  15. importcom.android.fzmap.map.MyItemizedOverlay;
  16. importcom.google.android.maps.GeoPoint;
  17. importcom.google.android.maps.MapActivity;
  18. importcom.google.android.maps.MapController;
  19. importcom.google.android.maps.MapView;
  20. importcom.google.android.maps.Overlay;
  21. importcom.google.android.maps.OverlayItem;
  22. publicclassFzMapActivityextendsMapActivityimplementsLocationCallBack,OnClickListener{
  23. /**Calledwhentheactivityisfirstcreated.*/
  24. privatefinalStringTAG="FzMapActivity";
  25. publicMapViewmapView;
  26. publicMapControllermMapCtrl;
  27. publicViewpopView;
  28. privateDrawablemyLocationDrawable;
  29. privateFzLocationManagerfzLocation;
  30. privateMyItemizedOverlaymyLocationOverlay;
  31. privateList<Overlay>mapOverlays;
  32. privateOverlayItemoverlayitem=null;
  33. ImageButtonloction_Btn;
  34. ImageButtonlayer_Btn;
  35. ImageButtonpointwhat_Btn;
  36. @Override
  37. publicvoidonCreate(BundlesavedInstanceState){
  38. super.onCreate(savedInstanceState);
  39. requestWindowFeature(Window.FEATURE_NO_TITLE);
  40. setContentView(R.layout.main);
  41. loction_Btn=(ImageButton)findViewById(R.id.loction);
  42. layer_Btn=(ImageButton)findViewById(R.id.layer);
  43. pointwhat_Btn=(ImageButton)findViewById(R.id.pointwhat);
  44. loction_Btn.setOnClickListener(this);
  45. layer_Btn.setOnClickListener(this);
  46. pointwhat_Btn.setOnClickListener(this);
  47. myLocationDrawable=getResources().getDrawable(R.drawable.point_where);
  48. myLocationOverlay=newMyItemizedOverlay(myLocationDrawable,this);
  49. mapView=(MapView)findViewById(R.id.map_view);
  50. mapView.setBuiltInZoomControls(true);
  51. mapView.setClickable(true);
  52. mMapCtrl=mapView.getController();
  53. mapOverlays=mapView.getOverlays();
  54. //以北京市中心为中心
  55. GeoPointcityLocPoint=newGeoPoint(39909230,116397428);
  56. mMapCtrl.animateTo(cityLocPoint);
  57. mMapCtrl.setZoom(12);
  58. FzLocationManager.init(FzMapActivity.this.getApplicationContext(),FzMapActivity.this);
  59. fzLocation=FzLocationManager.getInstance();
  60. initPopView();
  61. }
  62. privatevoidinitPopView(){
  63. if(null==popView){
  64. popView=getLayoutInflater().inflate(R.layout.overlay_popup,null);
  65. mapView.addView(popView,newMapView.LayoutParams(
  66. MapView.LayoutParams.WRAP_CONTENT,
  67. MapView.LayoutParams.WRAP_CONTENT,null,
  68. MapView.LayoutParams.BOTTOM_CENTER));
  69. popView.setVisibility(View.GONE);
  70. }
  71. }
  72. @Override
  73. protectedbooleanisRouteDisplayed(){
  74. //TODOAuto-generatedmethodstub
  75. returnfalse;
  76. }
  77. @Override
  78. publicvoidonCurrentLocation(Locationlocation){
  79. Log.d(TAG,"onCurrentLocationy");
  80. GeoPointpoint=newGeoPoint(
  81. (int)(location.getLatitude()*1E6),
  82. (int)(location.getLongitude()*1E6));
  83. overlayitem=newOverlayItem(point,"当前位置","");
  84. mMapCtrl.setZoom(16);
  85. myLocationOverlay.addOverlay(overlayitem);
  86. mapOverlays.add(myLocationOverlay);
  87. mMapCtrl.animateTo(point);
  88. }
  89. @Override
  90. publicvoidonClick(Viewv){
  91. switch(v.getId()){
  92. caseR.id.loction:
  93. {
  94. Locationlocation=fzLocation.getMyLocation();
  95. GeoPointpoint=newGeoPoint(
  96. (int)(location.getLatitude()*1E6),
  97. (int)(location.getLongitude()*1E6));
  98. overlayitem=newOverlayItem(point,"当前位置","");
  99. mMapCtrl.setZoom(16);
  100. myLocationOverlay.addOverlay(overlayitem);
  101. mapOverlays.add(myLocationOverlay);
  102. mMapCtrl.animateTo(point);
  103. }
  104. break;
  105. default:
  106. break;
  107. }
  108. }
  109. }

重点说一下的是这几行

mapView = (MapView) findViewById(R.id.map_view);

mapView.setBuiltInZoomControls(true); //这个把地图设置成可缩放

mapView.setClickable(true);、//地图可点

mMapCtrl = mapView.getController();//获得控制器

mapOverlays = mapView.getOverlays();

//以北京市中心为中心

GeoPoint cityLocPoint = new GeoPoint(39909230, 116397428);

mMapCtrl.animateTo(cityLocPoint);//移动这个点为中心的地图区域

mMapCtrl.setZoom(12);//设置地图当前等级大小

这里面有写代码可能是重复,可能是废弃的,还没做整理,也有的是后面要用到的。

五、图片资源

这个先给个截图吧

这些是最上面看到的截图用到的资源,自己可以用PS抠图。 我都这么干的。

更多相关文章

  1. Android(安卓)开发之集成百度地图的定位与地图展示
  2. android实现点击按钮控制图片切换
  3. 百度地图开发Marker|Polyline隐藏或显示
  4. Android(安卓)studio 43 文件存储到sdcard download文件夹下
  5. robotium获取本地文档内容
  6. Android(安卓)百度地图蓝点定位
  7. 【学习Android(安卓)NDK开发】native code通过JNI调用Java方法
  8. Android(安卓)使用 TableLayout 布局拉伸宽度
  9. Windows下Qt 5.6.3 for Android开发环境搭建

随机推荐

  1. No command 'mmm' found
  2. android 背景圆角以及图片圆角处理
  3. 通过API级别进行文档内容过滤
  4. [Android学习]ImageView的scaletype属性
  5. Android布局图标中心化
  6. android IApplicationToken分析
  7. 11月29日
  8. Android中接口的使用及类使用
  9. Android 版本号和分支查看
  10. Android使用VideoView全屏播放视频拉伸变