Android Map Api 使用和开发(1) 添加地图和界面

来源:互联网 发布:asp数据库管理网页 编辑:程序博客网 时间:2024/05/21 07:46

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

 

做过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 plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns: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.            
  9.            <!-- 这个就是搜索按钮了,你别看它想个输入框,其实是个button -->  
  10.          <Button android:id="@+id/search" android:background="@drawable/searchbtn"  
  11.                    android:layout_marginLeft="12dp"  
  12.                    android:layout_marginTop="5dp"  
  13.                    android:layout_width="150dp"  
  14.                    android:layout_height="35dp"  
  15.                    android:hint="搜索地图"  
  16.                    android:textSize="17sp"  
  17.                    android:singleLine="true"  
  18.                    android:gravity="center_vertical"  
  19.                    android:textColor="#000000"/>  
  20.                      
  21.                    
  22.                  <!-- 下面是三个图片按钮了,看效果图就知道是哪三个按钮了 -->  
  23.          <ImageButton  android:background="@drawable/maptitlebtn"  android:layout_marginLeft="15dp"  
  24.              android:id="@+id/pointwhat"  android:src="@drawable/pointwhat"  
  25.              android:layout_width="wrap_content"  
  26.              android:layout_height="wrap_content"  
  27.             />  
  28.               
  29.             <ImageButton  android:background="@drawable/maptitlebtn"  android:layout_marginLeft="5dp"  
  30.              android:id="@+id/layer"  android:src="@drawable/layer"  
  31.              android:layout_width="wrap_content"  
  32.              android:layout_height="wrap_content"  
  33.             />  
  34.               
  35.              <ImageButton  android:background="@drawable/maptitlebtn" android:layout_marginLeft="5dp"  
  36.              android:id="@+id/loction"  android:src="@drawable/loction"  
  37.              android:layout_width="wrap_content"  
  38.              android:layout_height="wrap_content"  
  39.             />  
  40.                      
  41.     </LinearLayout>  
  42. <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent"  
  43.             android:orientation="vertical"  
  44.         >  
  45.         <TableRow  android:layout_weight="1">  
  46.           
  47.         <!-- 这里才是重点,放置map的地方 -->  
  48.             <com.google.android.maps.MapView  
  49.               xmlns:android="http://schemas.android.com/apk/res/android"  
  50.               android:id="@+id/map_view"  
  51.               android:layout_width="fill_parent"   
  52.               android:layout_height="fill_parent"  
  53.               android:clickable="true"   
  54.               android:enabled="true"   
  55.               android:apiKey="0yRjCrET3v9ZkAhfn3wkRNzBPI_gHpkvx1iWT7g" />  
  56.         </TableRow>  
  57.           
  58.         <TableRow>  
  59.         </TableRow>  
  60.     </TableLayout>   
  61. </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 plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.android.fzmap"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="7" />  
  7.     <uses-permission android:name="android.permission.INTERNET" />  
  8.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
  9.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>  
  10.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  
  11.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  12.     <uses-library android:name="com.google.android.maps" />   
  13.         <activity android:name="FzMapActivity"  android:screenOrientation="portrait"   
  14.                   android:label="@string/app_name">  
  15.             <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.                 <category android:name="android.intent.category.LAUNCHER" />  
  18.             </intent-filter>  
  19.         </activity>  
  20.     </application>  
  21. </manifest>  
 

 

 

 

四、FzMapActivity 

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

不啰嗦,直接上代码

[java] view plaincopy
  1. package com.android.fzmap;  
  2. import java.util.List;  
  3. import android.graphics.Point;  
  4. import android.graphics.drawable.Drawable;  
  5. import android.location.Location;  
  6. import android.os.Bundle;  
  7. import android.util.Log;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.Window;  
  11. import android.widget.ImageButton;  
  12. import android.widget.TextView;  
  13. import com.android.fzmap.map.FzLocationManager;  
  14. import com.android.fzmap.map.FzLocationManager.LocationCallBack;  
  15. import com.android.fzmap.map.MyItemizedOverlay;  
  16. import com.google.android.maps.GeoPoint;  
  17. import com.google.android.maps.MapActivity;  
  18. import com.google.android.maps.MapController;  
  19. import com.google.android.maps.MapView;  
  20. import com.google.android.maps.Overlay;  
  21. import com.google.android.maps.OverlayItem;  
  22.   
  23. public class FzMapActivity  extends MapActivity implements LocationCallBack ,OnClickListener{  
  24.     /** Called when the activity is first created. */  
  25.     private final String TAG = "FzMapActivity";  
  26.     public MapView mapView;  
  27.     public MapController mMapCtrl;  
  28.     public View popView;  
  29.     private Drawable myLocationDrawable;  
  30.     private FzLocationManager fzLocation;  
  31.     private MyItemizedOverlay myLocationOverlay;  
  32.     private List<Overlay> mapOverlays;  
  33.     private OverlayItem overlayitem = null;  
  34.       
  35.     ImageButton loction_Btn;  
  36.     ImageButton layer_Btn;  
  37.     ImageButton pointwhat_Btn;  
  38.       
  39.     @Override  
  40.     public void onCreate(Bundle savedInstanceState) {  
  41.         super.onCreate(savedInstanceState);  
  42.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  43.         setContentView(R.layout.main);  
  44.           
  45.         loction_Btn = (ImageButton)findViewById(R.id.loction);  
  46.         layer_Btn = (ImageButton)findViewById(R.id.layer);  
  47.         pointwhat_Btn = (ImageButton)findViewById(R.id.pointwhat);  
  48.           
  49.         loction_Btn.setOnClickListener(this);  
  50.         layer_Btn.setOnClickListener(this);  
  51.         pointwhat_Btn.setOnClickListener(this);  
  52.           
  53.         myLocationDrawable = getResources().getDrawable(R.drawable.point_where);  
  54.         myLocationOverlay = new MyItemizedOverlay(myLocationDrawable,this);  
  55.           
  56.         mapView = (MapView) findViewById(R.id.map_view);  
  57.         mapView.setBuiltInZoomControls(true);  
  58.         mapView.setClickable(true);  
  59.         mMapCtrl = mapView.getController();  
  60.         mapOverlays = mapView.getOverlays();  
  61.         //以北京市中心为中心  
  62.         GeoPoint cityLocPoint = new GeoPoint(39909230116397428);  
  63.         mMapCtrl.animateTo(cityLocPoint);  
  64.         mMapCtrl.setZoom(12);  
  65.         FzLocationManager.init(FzMapActivity.this.getApplicationContext() , FzMapActivity.this);  
  66.         fzLocation = FzLocationManager.getInstance();  
  67.         initPopView();  
  68.     }  
  69.       
  70.       
  71.     private void initPopView(){  
  72.         if(null == popView){  
  73.             popView = getLayoutInflater().inflate(R.layout.overlay_popup, null);  
  74.             mapView.addView(popView, new MapView.LayoutParams(  
  75.                     MapView.LayoutParams.WRAP_CONTENT,  
  76.                     MapView.LayoutParams.WRAP_CONTENT, null,  
  77.                     MapView.LayoutParams.BOTTOM_CENTER));  
  78.             popView.setVisibility(View.GONE);  
  79.         }  
  80.          
  81.     }  
  82.       
  83.       
  84.       
  85.     @Override  
  86.     protected boolean isRouteDisplayed() {  
  87.         // TODO Auto-generated method stub  
  88.         return false;  
  89.     }  
  90.       
  91.     @Override  
  92.     public void onCurrentLocation(Location location) {  
  93.         Log.d(TAG, "onCurrentLocationy");  
  94.         GeoPoint point = new GeoPoint(  
  95.                 (int) (location.getLatitude() * 1E6),  
  96.                 (int) (location.getLongitude() * 1E6));  
  97.         overlayitem = new OverlayItem(point, "当前位置""");  
  98.         mMapCtrl.setZoom(16);  
  99.         myLocationOverlay.addOverlay(overlayitem);  
  100.         mapOverlays.add(myLocationOverlay);  
  101.         mMapCtrl.animateTo(point);  
  102.     }  
  103.     @Override  
  104.     public void onClick(View v) {  
  105.         switch (v.getId()) {  
  106.         case R.id.loction:  
  107.         {  
  108.             Location  location = fzLocation.getMyLocation();  
  109.             GeoPoint point = new GeoPoint(  
  110.                     (int) (location.getLatitude() * 1E6),  
  111.                     (int) (location.getLongitude() * 1E6));  
  112.             overlayitem = new OverlayItem(point, "当前位置""");  
  113.             mMapCtrl.setZoom(16);  
  114.             myLocationOverlay.addOverlay(overlayitem);  
  115.             mapOverlays.add(myLocationOverlay);  
  116.             mMapCtrl.animateTo(point);  
  117.         }  
  118.             break;  
  119.           
  120.         default:  
  121.             break;  
  122.         }  
  123.     }  
  124. }  
 

 

重点说一下的是这几行

 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抠图。 我都这么干的。

原创粉丝点击