MapView使用示例

一、MapView申请apiKey
打开Eclipse--->Windows--->Preferences--->Android--->Build
查看默认的debug keystore位置,我的是 C:\Users\wh\.android\debug.keystore


在cmd中执行

keytool -list -alias androiddebugkey -keystore "C:\Users\wh\.android\debug.keystore" -storepass android -keypass android


双引号中的为你keystore位置

执行结果:
androiddebugkey, 2009-2-17, keyEntry,
认证指纹 (MD5): XX:XX:XX:XX:XX:XX:XX:XX.............


打开http://code.google.com/intl/zh-CN/android/maps-api-signup.html

填入你的认证指纹(MD5)即可获得apiKey了

layout中加入MapView
<com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="XXXXXXXXXXXXXXXXXXXXXXXXXXXX" />



二、MapView控件的使用
1.建立mapview.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.google.android.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:apiKey="0NmBYax5wIVpt1zmwv7RnvNczoF2Ftwzd9yDduA" />

    <LinearLayout
        android:id="@+id/zoomView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/mapView"
        android:layout_centerHorizontal="true" >
    </LinearLayout>
   
</RelativeLayout>

1.新建MapViewActivity继承MapActivity:
package com.willawn.androidtest.test;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ZoomControls;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class MapViewActivity extends MapActivity {
// 线性布局
private LinearLayout linearLayout;
// 地图视图
private MapView mapView;
// 缩放控制
private ZoomControls mZoom;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.mapview);
linearLayout = (LinearLayout) findViewById(R.id.zoomView);
mapView = (MapView) findViewById(R.id.mapView);
mapView.getController().setCenter(new GeoPoint(39971036,116314659));
mapView.getController().setZoom(10);
mZoom = (ZoomControls) mapView.getZoomControls();
linearLayout.addView(mZoom);
}

@Override
protected boolean isRouteDisplayed() {
return false;
}

}

你可能感兴趣的:(MapView)