Android(安卓)Map Api 使用和开发(1) 添加地图和界面
最近正在做和地图相关的项目,想记录和整理一下的这方面的内容发出来,既是自己整理总结,也是和别人分享经验。
做过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- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"android:layout_height="fill_parent"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="fill_parent"android:layout_height="wrap_content"
- android:orientation="horizontal"android:background="@drawable/searchbg">
- <!--这个就是搜索按钮了,你别看它想个输入框,其实是个button-->
- <Buttonandroid:id="@+id/search"android:background="@drawable/searchbtn"
- android:layout_marginLeft="12dp"
- android:layout_marginTop="5dp"
- android:layout_width="150dp"
- android:layout_height="35dp"
- android:hint="搜索地图"
- android:textSize="17sp"
- android:singleLine="true"
- android:gravity="center_vertical"
- android:textColor="#000000"/>
- <!--下面是三个图片按钮了,看效果图就知道是哪三个按钮了-->
- <ImageButtonandroid:background="@drawable/maptitlebtn"android:layout_marginLeft="15dp"
- android:id="@+id/pointwhat"android:src="@drawable/pointwhat"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <ImageButtonandroid:background="@drawable/maptitlebtn"android:layout_marginLeft="5dp"
- android:id="@+id/layer"android:src="@drawable/layer"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <ImageButtonandroid:background="@drawable/maptitlebtn"android:layout_marginLeft="5dp"
- android:id="@+id/loction"android:src="@drawable/loction"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- <TableLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"
- android:orientation="vertical"
- >
- <TableRowandroid:layout_weight="1">
- <!--这里才是重点,放置map的地方-->
- <com.google.android.maps.MapView
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/map_view"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:clickable="true"
- android:enabled="true"
- android:apiKey="0yRjCrET3v9ZkAhfn3wkRNzBPI_gHpkvx1iWT7g"/>
- </TableRow>
- <TableRow>
- </TableRow>
- </TableLayout>
- </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- <?xmlversion="1.0"encoding="utf-8"?>
- <manifestxmlns:android="http://schemas.android.com/apk/res/android"
- package="com.android.fzmap"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdkandroid:minSdkVersion="7"/>
- <uses-permissionandroid:name="android.permission.INTERNET"/>
- <uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
- <uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/>
- <uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
- <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
- <uses-libraryandroid:name="com.google.android.maps"/>
- <activityandroid:name="FzMapActivity"android:screenOrientation="portrait"
- android:label="@string/app_name">
- <intent-filter>
- <actionandroid:name="android.intent.action.MAIN"/>
- <categoryandroid:name="android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- </application>
- </manifest>
四、FzMapActivity
到这里就剩下 Activity的显示了。 这个Activity要继承MapActivity,不然你会死的很难看的。
不啰嗦,直接上代码
[java] view plain copy- packagecom.android.fzmap;
- importjava.util.List;
- importandroid.graphics.Point;
- importandroid.graphics.drawable.Drawable;
- importandroid.location.Location;
- importandroid.os.Bundle;
- importandroid.util.Log;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.view.Window;
- importandroid.widget.ImageButton;
- importandroid.widget.TextView;
- importcom.android.fzmap.map.FzLocationManager;
- importcom.android.fzmap.map.FzLocationManager.LocationCallBack;
- importcom.android.fzmap.map.MyItemizedOverlay;
- importcom.google.android.maps.GeoPoint;
- importcom.google.android.maps.MapActivity;
- importcom.google.android.maps.MapController;
- importcom.google.android.maps.MapView;
- importcom.google.android.maps.Overlay;
- importcom.google.android.maps.OverlayItem;
- publicclassFzMapActivityextendsMapActivityimplementsLocationCallBack,OnClickListener{
- /**Calledwhentheactivityisfirstcreated.*/
- privatefinalStringTAG="FzMapActivity";
- publicMapViewmapView;
- publicMapControllermMapCtrl;
- publicViewpopView;
- privateDrawablemyLocationDrawable;
- privateFzLocationManagerfzLocation;
- privateMyItemizedOverlaymyLocationOverlay;
- privateList<Overlay>mapOverlays;
- privateOverlayItemoverlayitem=null;
- ImageButtonloction_Btn;
- ImageButtonlayer_Btn;
- ImageButtonpointwhat_Btn;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.main);
- loction_Btn=(ImageButton)findViewById(R.id.loction);
- layer_Btn=(ImageButton)findViewById(R.id.layer);
- pointwhat_Btn=(ImageButton)findViewById(R.id.pointwhat);
- loction_Btn.setOnClickListener(this);
- layer_Btn.setOnClickListener(this);
- pointwhat_Btn.setOnClickListener(this);
- myLocationDrawable=getResources().getDrawable(R.drawable.point_where);
- myLocationOverlay=newMyItemizedOverlay(myLocationDrawable,this);
- mapView=(MapView)findViewById(R.id.map_view);
- mapView.setBuiltInZoomControls(true);
- mapView.setClickable(true);
- mMapCtrl=mapView.getController();
- mapOverlays=mapView.getOverlays();
- //以北京市中心为中心
- GeoPointcityLocPoint=newGeoPoint(39909230,116397428);
- mMapCtrl.animateTo(cityLocPoint);
- mMapCtrl.setZoom(12);
- FzLocationManager.init(FzMapActivity.this.getApplicationContext(),FzMapActivity.this);
- fzLocation=FzLocationManager.getInstance();
- initPopView();
- }
- privatevoidinitPopView(){
- if(null==popView){
- popView=getLayoutInflater().inflate(R.layout.overlay_popup,null);
- mapView.addView(popView,newMapView.LayoutParams(
- MapView.LayoutParams.WRAP_CONTENT,
- MapView.LayoutParams.WRAP_CONTENT,null,
- MapView.LayoutParams.BOTTOM_CENTER));
- popView.setVisibility(View.GONE);
- }
- }
- @Override
- protectedbooleanisRouteDisplayed(){
- //TODOAuto-generatedmethodstub
- returnfalse;
- }
- @Override
- publicvoidonCurrentLocation(Locationlocation){
- Log.d(TAG,"onCurrentLocationy");
- GeoPointpoint=newGeoPoint(
- (int)(location.getLatitude()*1E6),
- (int)(location.getLongitude()*1E6));
- overlayitem=newOverlayItem(point,"当前位置","");
- mMapCtrl.setZoom(16);
- myLocationOverlay.addOverlay(overlayitem);
- mapOverlays.add(myLocationOverlay);
- mMapCtrl.animateTo(point);
- }
- @Override
- publicvoidonClick(Viewv){
- switch(v.getId()){
- caseR.id.loction:
- {
- Locationlocation=fzLocation.getMyLocation();
- GeoPointpoint=newGeoPoint(
- (int)(location.getLatitude()*1E6),
- (int)(location.getLongitude()*1E6));
- overlayitem=newOverlayItem(point,"当前位置","");
- mMapCtrl.setZoom(16);
- myLocationOverlay.addOverlay(overlayitem);
- mapOverlays.add(myLocationOverlay);
- mMapCtrl.animateTo(point);
- }
- break;
- default:
- break;
- }
- }
- }
重点说一下的是这几行
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抠图。 我都这么干的。
更多相关文章
- Android(安卓)开发之集成百度地图的定位与地图展示
- android实现点击按钮控制图片切换
- 百度地图开发Marker|Polyline隐藏或显示
- Android(安卓)studio 43 文件存储到sdcard download文件夹下
- robotium获取本地文档内容
- Android(安卓)百度地图蓝点定位
- 【学习Android(安卓)NDK开发】native code通过JNI调用Java方法
- Android(安卓)使用 TableLayout 布局拉伸宽度
- Windows下Qt 5.6.3 for Android开发环境搭建