天天看點

Android Map Api 使用和開發(1) 添加地圖和界面

(全部源碼位址:http://download.csdn.net/detail/totogo2010/4335701  ) 

 最近正在做和地圖相關的項目,想記錄和整理一下的這方面的内容發出來,既是自己整理總結,也是和别人分享經驗。 

做過android 地圖相關項目的同學估計都會有一些相同的需求,這些需求在android 上谷歌自己做的地圖軟體都做得很好,很多人想模仿參考來做,比如:

1、彈出浮動的搜尋框,并能搜尋位址并定位

2、長按地圖出現目前位置的泡泡(popup),泡泡裡有标題和内容,有詳細位址和詳細資訊 

3、自動定位到目前位置

4、顯示各種圖層

這麼多需求不是一下子都能做出來的,而且做好了也不容易。

那這篇先寫一些怎麼把google地圖添加到android程式中,還有把主界面顯示做一下。

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

Android Map Api 使用和開發(1) 添加地圖和界面

這張圖怎麼樣? 是不是長得和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全貼出來,上面加注釋就好了

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns: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 -->

<Button android: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"/>

<!-- 下面是三個圖檔按鈕了,看效果圖就知道是哪三個按鈕了 -->

<ImageButton android: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"

/>

<ImageButton android: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"

/>

<ImageButton android: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>

<TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent"

android:orientation="vertical"

>

<TableRow android: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" /> 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.android.fzmap"

android:versionCode="1"

android:versionName="1.0">

<uses-sdk android:minSdkVersion="7" />

<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"/>

<application android:icon="@drawable/icon" android:label="@string/app_name">

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

<activity android:name="FzMapActivity" android:screenOrientation="portrait"

android:label="@string/app_name">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest> 

四、FzMapActivity 

到這裡就剩下 Activity的顯示了。  這個Activity要繼承MapActivity,不然你會死的很難看的。

不啰嗦,直接上代碼

package com.android.fzmap;

import java.util.List;

import android.graphics.Point;

import android.graphics.drawable.Drawable;

import android.location.Location;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.Window;

import android.widget.ImageButton;

import android.widget.TextView;

import com.android.fzmap.map.FzLocationManager;

import com.android.fzmap.map.FzLocationManager.LocationCallBack;

import com.android.fzmap.map.MyItemizedOverlay;

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;

import com.google.android.maps.OverlayItem;

public class FzMapActivity extends MapActivity implements LocationCallBack ,OnClickListener{

/** Called when the activity is first created. */

private final String TAG = "FzMapActivity";

public MapView mapView;

public MapController mMapCtrl;

public View popView;

private Drawable myLocationDrawable;

private FzLocationManager fzLocation;

private MyItemizedOverlay myLocationOverlay;

private List<Overlay> mapOverlays;

private OverlayItem overlayitem = null;

ImageButton loction_Btn;

ImageButton layer_Btn;

ImageButton pointwhat_Btn;

@Override

public void onCreate(Bundle savedInstanceState) {

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 = new MyItemizedOverlay(myLocationDrawable,this);

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);

FzLocationManager.init(FzMapActivity.this.getApplicationContext() , FzMapActivity.this);

fzLocation = FzLocationManager.getInstance();

initPopView();

}

private void initPopView(){

if(null == popView){

popView = getLayoutInflater().inflate(R.layout.overlay_popup, null);

mapView.addView(popView, new MapView.LayoutParams(

MapView.LayoutParams.WRAP_CONTENT,

MapView.LayoutParams.WRAP_CONTENT, null,

MapView.LayoutParams.BOTTOM_CENTER));

popView.setVisibility(View.GONE);

}

}

@Override

protected boolean isRouteDisplayed() {

// TODO Auto-generated method stub

return false;

}

@Override

public void onCurrentLocation(Location location) {

Log.d(TAG, "onCurrentLocationy");

GeoPoint point = new GeoPoint(

(int) (location.getLatitude() * 1E6),

(int) (location.getLongitude() * 1E6));

overlayitem = new OverlayItem(point, "目前位置", "");

mMapCtrl.setZoom(16);

myLocationOverlay.addOverlay(overlayitem);

mapOverlays.add(myLocationOverlay);

mMapCtrl.animateTo(point);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.loction:

{

Location location = fzLocation.getMyLocation();

GeoPoint point = new GeoPoint(

(int) (location.getLatitude() * 1E6),

(int) (location.getLongitude() * 1E6));

overlayitem = new OverlayItem(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);//設定地圖目前等級大小

這裡面有寫代碼可能是重複,可能是廢棄的,還沒做整理,也有的是後面要用到的。

五、圖檔資源

 這個先給個截圖吧

Android Map Api 使用和開發(1) 添加地圖和界面

這些是最上面看到的截圖用到的資源,自己可以用PS摳圖。 我都這麼幹的。

這篇就先寫到這吧。

繼續閱讀