Android MapView简单使用

简介: 1. 首先先要获取你的debug keystore位置: 打开Eclipse--->Windows---> preferences--->Android--->Build 查看默认的debug keystore位置,我的是C:\Documents and Settings\Administrator\.android\debug.keystore 2.

1. 首先先要获取你的debug keystore位置:

打开Eclipse--->Windows---> preferences--->Android--->Build
查看默认的debug keystore位置,我的是C:\Documents and Settings\Administrator\.android\debug.keystore

2.
C:\Program Files\Java\jdk1.6.0_04\jre\bin>keytool -list -alias androiddebugkey -keystore "C:\Documents and Settings\Administrator\.android\debug.keystore" -storepass android -keypass android
androiddebugkey, 2009-7-25, PrivateKeyEntry,
认证指纹 (MD5): DA:D5:6E:C2:80:D1:0F:0D:F8:2A:58:6A:74:7C:CE:2D

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

填入你的认证指纹(MD5)即可获得apiKey了,结果显示如下:
         感谢您注册 Android 地图 API 密钥!

            您的密钥是:

       XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

4.       使用得到的apiKey:

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

  或者

  <view android:id="@+id/mv"
   class="com.google.android.maps.MapView"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_weight="1" 
   android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" 
   />

5.Android在sdk1.5的预装的add-on中提供了一个Map扩展库com.google.android.maps,利用它你就可以给你的android应用程序增加上强大的地图功能了。这个库的位置在Your-SDK_DIR\add-ons\google_apis-3\libs.
一定要在manifest.xml中设置全相应的权限,比如:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
要在manifest.xml中加上要用的maps库:

<manifest xmlns:android="http://schemas.android.com/apk/res/android
package="com.example.package.name"> 
  ... 
  <application android:name="MyApplication" > 
    <uses-library android:name="com.google.android.maps" /> 
    ... 
  </application> 
  ... 
</manifest>

需要说明的是这个库不是标准的android sdk的内容,你也可以自己从这里这里下载并放到你的sdk里面。

Maps库分析


Maps库提供了十几个类,具体可以参考这里http://code.google.com/intl/ja/android/add-ons/google-apis/reference/index.html,  包括Mapview,MapController,MapActivity等。

 

Mapview是用来显示地图的view, 它也是派生自android.view.ViewGroup。

地图可以以不同的形式来显示出来,如街景模式,卫星模式等,具体方法可以参考:

setSatellite(boolean), setTraffic(boolean), and setStreetView(boolean)

 

MapView只能被MapActivity来创建,这是因为mapview需要通过后台的线程来连接网络或者文件系统,而这些线程要由mapActivity来管理。


需要特别说明的一点是,android 1.5中,map的zoom采用了built-in机制,可以通过setBuiltInZoomControls(boolean)来设置是否在地图上显示zoom控件。


MapActivity是一个抽象类,任何想要显示MapView的activity都需要派生自MapActivity。并且在其派生类的onCreate()中,都要创建一个MapView实例,可以通过MapView constructor (then add it to a layout View with ViewGroup.addView(View)) 或者通过layout XML来创建。

 

实例分析


最后,按照惯例,还是用一个小程序来演示一下android中地图功能的开发。主要功能是实现了地图的缩放,添加了菜单,从而可以手动选择地图的显示模式等。

Step 1: 新建一个android project, 注意这里要选择的build target为"Google APIs"


Step 2: 修改menifest文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android
      package="com.map.prac" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <uses-library android:name="com.google.android.maps" /> 
        <activity android:name=".MapViewPrac2" 
                  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> 
    <uses-sdk android:minSdkVersion="3" /> 
</manifest>


Step 3: 修改layout文件,main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
    android:id="@+id/main" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <com.google.android.maps.MapView 
        android:id="@+id/map" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:enabled="true" 
        android:clickable="true" 
        android:apiKey="???????????????????????????????????" 
        /> 
</LinearLayout> 
这里需要将api key中的????????????改成你自己申请到的api key.

 


Step 4: 修改代码:

package com.map.prac; 
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 android.app.AlertDialog; 
import android.app.Dialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.MenuItem; 
public class MapViewPrac2 extends MapActivity { 
    private final String TAG = "MapPrac"; 
    private MapView mapView = null; 
    private MapController mc; 
    //Menu items 
    final private int menuMode = Menu.FIRST; 
    final private int menuExit = Menu.FIRST+1; 
    final private int menuCommandList = Menu.FIRST + 2; 
    private int chooseItem = 0; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        mapView = (MapView)findViewById(R.id.map); 
        mc = mapView.getController(); 
        mapView.setTraffic(true); // 
        mapView.setSatellite(false); 
        mapView.setStreetView(true); 
        //GeoPoint gp = new GeoPoint((int)(39.269259 * 1000000), (int)(115.255762 * 1000000));//yixian 
        GeoPoint gp = new GeoPoint((int)(39.95 * 1000000), (int)(116.37 * 1000000));//beijing 
        //mc.animateTo(gp); 
        //mc.setZoom(12); 
        mc.setCenter(gp); 
        //to display zoom control in MapView 
        mapView.setBuiltInZoomControls(true); 
    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        // TODO Auto-generated method stub 
        Log.i(TAG,"enter onKeyDown"); 
        return super.onKeyDown(keyCode, event); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        menu.add(0, menuMode, 0, "Map Mode"); 
        menu.add(0, menuCommandList, 1, "Command List"); 
        menu.add(0, menuExit, 2, "Exit"); 
        return super.onCreateOptionsMenu(menu); 
    } 
    @Override 
    public boolean onMenuItemSelected(int featureId, MenuItem item) { 
        // TODO Auto-generated method stub 
        switch(item.getItemId()) 
        { 
        case menuMode: 
            Dialog dMode = new AlertDialog.Builder(this) 
            //.setIcon(R.drawable.alert_dialog_icon) 
            .setTitle(R.string.alert_dialog_single_choice) 
            .setSingleChoiceItems(R.array.select_dialog_items2, chooseItem, new DialogInterface.OnClickListener() { 
                public void onClick(DialogInterface dialog, int whichButton) { 
                    Log.i(TAG, "choose button is "+ whichButton); 
                    chooseItem = whichButton; 
                    /* User clicked on a radio button do some stuff */ 
                } 
            }) 
            .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { 
                public void onClick(DialogInterface dialog, int whichButton) { 
                    /* User clicked Yes so do some stuff */ 
                    Log.i(TAG,"item = "+chooseItem); 
                    switch(chooseItem) 
                    { 
                    case 0: 
                        mapView.setSatellite(false); 
                        break; 
                    case 1: 
                        mapView.setSatellite(true); 
                        break; 
                    case 2: 
                        mapView.setTraffic(true); 
                        break; 
                    case 3: 
                        mapView.setStreetView(true); 
                        break; 
                    default: 
                        break; 
                    } 
                } 
            }) 
            .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { 
                public void onClick(DialogInterface dialog, int whichButton) { 
                    /* User clicked No so do some stuff */ 
                } 
            }) 
           .create(); 
            dMode.show(); 
            break; 
        case menuCommandList: 
            //create the dialog 
            Dialog d = new AlertDialog.Builder(this) 
            .setTitle(R.string.select_dialog) 
            .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() { 
                public void onClick(DialogInterface dialog, int which) { 
                    /* User clicked so do some stuff */ 
                    String[] items = getResources().getStringArray(R.array.select_dialog_items); 
                    /*new AlertDialog.Builder(this) 
                            .setMessage("You selected: " + which + " , " + items[which]) 
                            .show();*/ 
                    Log.i(TAG,"you choose is: " + items[which]); 
                } 
            }) 
            .create(); 
            //show the dialog 
            d.show(); 
            break; 
        case menuExit: 
            finish(); 
            break; 
        default: 
                break; 
        } 
        return super.onMenuItemSelected(featureId, item); 
    } 
    @Override 
    protected boolean isRouteDisplayed() { 
        // TODO Auto-generated method stub 
        return false; 
    } 
}

目录
相关文章
|
定位技术 Android开发
Android MapView 申请apiKey
1. 首先先要获取你的debug keystore位置: 打开Eclipse--->Windows--->Preferences--->Android--->Build 查看默认的debug keystore位置,我的是C:\Documents and Settings\MYNAME \.
753 0
|
17天前
|
消息中间件 网络协议 Java
Android 开发中实现数据传递:广播和Handler
Android 开发中实现数据传递:广播和Handler
16 1
|
7天前
|
存储 安全 Android开发
安卓应用开发:构建一个高效的用户登录系统
【5月更文挑战第3天】在移动应用开发中,用户登录系统的设计与实现是至关重要的一环。对于安卓平台而言,一个高效、安全且用户体验友好的登录系统能够显著提升应用的用户留存率和市场竞争力。本文将探讨在安卓平台上实现用户登录系统的最佳实践,包括对最新身份验证技术的应用、安全性考量以及性能优化策略。
|
10天前
|
前端开发 Android开发 iOS开发
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
【4月更文挑战第30天】Flutter 框架实现跨平台移动应用,通过一致的 UI 渲染(Skia 引擎)、热重载功能和响应式框架提高开发效率和用户体验。然而,Android 和 iOS 的系统差异、渲染机制及编译过程影响性能。性能对比显示,iOS 可能因硬件优化提供更流畅体验,而 Android 更具灵活性和广泛硬件支持。开发者可采用代码、资源优化和特定平台优化策略,利用性能分析工具提升应用性能。
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
|
11天前
|
监控 Java Android开发
安卓应用开发:打造高效用户界面的五大策略
【4月更文挑战第29天】 在安卓应用开发的世界中,构建一个既美观又高效的用户界面(UI)对于吸引和保留用户至关重要。本文将深入探讨五种策略,这些策略可以帮助开发者优化安卓应用的UI性能。我们将从布局优化讲起,逐步过渡到绘制优化、内存管理、异步处理以及最终的用户交互细节调整。通过这些实践技巧,你将能够为用户提供流畅而直观的体验,确保你的应用在竞争激烈的市场中脱颖而出。
|
2天前
|
Java API 开发工具
java与Android开发入门指南
java与Android开发入门指南
8 0
|
3天前
|
Android开发 Kotlin
Kotlin开发Android之基础问题记录
Kotlin开发Android之基础问题记录
15 1