最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

Android Map开发基础知识学习笔记

日期:08-03    来源:中国设计秀    作者:cnwebshow.com

 b7U中国设计秀
b7U中国设计秀
运行:keytool -list -keystore ~/.android/debug.keystoreb7U中国设计秀
b7U中国设计秀
用得到的md5码到http://code.google.com/intl/zh-CN/android/maps-api-signup.html注册API密钥。参考:b7U中国设计秀
b7U中国设计秀
http://www.cnblogs.com/feisky/archive/2010/01/17/1650083.html b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
注册完成后会得到如下的网页:b7U中国设计秀
b7U中国设计秀
您的密钥是:b7U中国设计秀
b7U中国设计秀
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXb7U中国设计秀
b7U中国设计秀
此密钥适用于所有使用以下指纹所对应证书进行验证的应用程序:b7U中国设计秀
b7U中国设计秀
XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XXb7U中国设计秀
b7U中国设计秀
下面是一个 xml 格式的示例,帮助您了解地图功能:b7U中国设计秀
b7U中国设计秀
<com.google.android.maps.MapViewb7U中国设计秀
  android:layout_width="fill_parent"b7U中国设计秀
  android:layout_height="fill_parent"                   android:apiKey="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"b7U中国设计秀
  />b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
模拟器设置b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
建立一个"Google Inc.:Google APIs:3"的模拟器。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
手机终端需要支持Google Add-onsb7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
作为与Google合作的手机终端的制造商,如果想在自己的Android终端中集成Google的一些应用,例如map,market,picasa,sync等,需要和Google签约,这样Google会有专人帮你将这一系列服务整合到你的平台中来。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
作为没有和Google合作的手机终端的制造商,我们可以使用破解的方法将Add-ons中的运行库整合到自己的framework中来(build到手机终端的system.img中)。操作如下:b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
1. 在Android中,额外支持Google Add-ons(Google Maps),需要将如下档案(从标准的SDK中pull出来的,也可以从GPhone中pull出来)打包进System.img。Google Add-ons只需要1--2,Goolge Maps(Google的地图程序) 需要1--5:b7U中国设计秀
b7U中国设计秀
1)system/framework      com.google.android.maps.jarb7U中国设计秀
b7U中国设计秀
2)system/etc/permissions  com.google.android.maps.xmlb7U中国设计秀
b7U中国设计秀
3)system/framework      com.google.android.gtalkservice.jarb7U中国设计秀
b7U中国设计秀
4)system/etc/permissions  com.google.android.gtalkservice.xmlb7U中国设计秀
b7U中国设计秀
5)system/app          Maps.apk (Google Maps v3.0)b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
2.源码:Android Cupcake Release(经测试,在donut平台同样适用。)b7U中国设计秀
b7U中国设计秀
1)编译源码(为了生成out/target目录中Android最原始的东西)b7U中国设计秀
b7U中国设计秀
2)将以上文件加入out/target/PRoduct/generic相应目录,在源码根目录使用make snod打包,获得新的system.imgb7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
权限和Maps库设置b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
在manifest.xml中设置全相应的权限,比如:   b7U中国设计秀
b7U中国设计秀
                                b7U中国设计秀
b7U中国设计秀
<uses-permission android:name="android.permission.access_COARSE_LOCATION"/>b7U中国设计秀
b7U中国设计秀
<uses-permission android:name="android.permission.INTERNET" />b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
在manifest.xml中加上要用的maps库:b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
<manifest xmlns:android="http://schemas.android.com/apk/res/android"b7U中国设计秀
b7U中国设计秀
package="com.example.package.name">b7U中国设计秀
b7U中国设计秀
  ...b7U中国设计秀
b7U中国设计秀
  <application android:name="MyApplication" >b7U中国设计秀
b7U中国设计秀
    <uses-library android:name="com.google.android.maps" />b7U中国设计秀
b7U中国设计秀
    ...b7U中国设计秀
b7U中国设计秀
  </application>b7U中国设计秀
b7U中国设计秀
  ...b7U中国设计秀
b7U中国设计秀
</manifest>b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
Maps库分析b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
Maps库提供了十几个类,具体可以参考这里http://code.google.com/intl/ja/android/add-ons/google-apis/reference/index.html,包括Mapview,MapController,MapActivity等。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
(1)MapControllerb7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
控制地图移动,伸缩,以某个GPS坐标为中心,控制MapView中的view组件,管理Overlay,提供View的基本功能。使用多种地图模式(地图模式(某些城市可实时对交通状况进行更新),卫星模式,街景模式)来查看Google Map。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
常用方法:animateTo(GeoPoint point)  setCenter(GeoPoint point)  setZoom(int zoomLevel) 等。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
(2)MapViewb7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
Mapview是用来显示地图的view, 它派生自android.view.ViewGroup。当MapView获得焦点,可以控制地图的移动和缩放。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
地图可以以不同的形式来显示出来,如街景模式,卫星模式等,通过setSatellite(boolean)  setTraffic(boolean), setStreetView(boolean) 方法。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
MapView只能被MapActivity来创建,这是因为mapview需要通过后台的线程来连接网络或者文件系统,而这些线程要由mapActivity来管理。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
需要特别说明的一点是,android 1.5中,map的zoom采用了built-in机制,可以通过setBuiltInZoomControls(boolean)来设置是否在地图上显示zoom控件。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
常用方法:getController()  getOverlays()  setSatellite(boolean)  setTraffic(boolean),   setStreetView(boolean)  setBuiltInZoomControls(boolean)等。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
(3)MapActivityb7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
管理Activity的生命周期,为mapview建立及取消对map service的连接。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
MapActivity是一个抽象类,任何想要显示MapView的activity都需要派生自MapActivity。并且在其派生类的onCreate()中,都要创建一个MapView实例,可以通过MapViewconstructor (然后添加到View中ViewGroup.addView(View)) 或者通过layout XML来创建。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
(4)Overlayb7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
Overlay是覆盖到MapView的最上层,可以扩展其ondraw接口,自定义在MapView中显示一些自己的东西。MapView通过MapView.getOverlays()对Overlay进行管理。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
除了Overlay这个基类,Google还扩展了2个比较有用的Overlayb7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
1)MylocationOverlay—集成了Android.location中接收当前坐标的接口,集成SersorManager中CompassSensor的接口b7U中国设计秀
b7U中国设计秀
我们只需要enableMyLocation(),enableCompass就可以让我们的程序拥有实时的MyLocation以及Compass 功能(Activity.onResume()中)。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
2)ItemlizedOverlay—管理一个OverlayItem链表,用图片等资源在地图上作风格相同的标记。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
(5)Projection:MapView中GPS坐标与设备坐标的转换(GeoPoint和Point)。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
一个简单的小例子b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
用一个小程序来演示一下android中地图功能的开发。主要功能是实现了地图的缩放,添加了菜单,从而可以手动选择地图的显示模式等。b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
Step1: 新建一个android project, 注意这里要选择的buildtarget为"GoogleAPIs"b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
Step 2: 修改menifest文件:b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
<?xmlversion="1.0"encoding="utf-8"?>b7U中国设计秀
b7U中国设计秀
<manifest xmlns:android="http://schemas.android.com/apk/res/android"b7U中国设计秀
     package="com.map.prac"b7U中国设计秀
     android:versionCode="1"b7U中国设计秀
     android:versionName="1.0">b7U中国设计秀
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>b7U中国设计秀
    <uses-permission android:name="android.permission.INTERNET" />b7U中国设计秀
b7U中国设计秀
   <application android:icon="@drawable/icon"android:label="@string/app_name">b7U中国设计秀
   <uses-library android:name="com.google.android.maps" />b7U中国设计秀
       <activity android:name=".MapViewPrac2"b7U中国设计秀
             android:label="@string/app_name">b7U中国设计秀
         <intent-filter>b7U中国设计秀
          <action android:name="android.intent.action.MAIN"/>b7U中国设计秀
        <category android:name="android.intent.category.LAUNCHER" />b7U中国设计秀
         </intent-filter>b7U中国设计秀
       </activity>b7U中国设计秀
   </application>b7U中国设计秀
    <uses-sdk android:minSdkVersion="3" />b7U中国设计秀
</manifest>b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
Step 3: 修改layout文件,main.xmlb7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
<?xmlversion="1.0"encoding="utf-8"?>b7U中国设计秀
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"b7U中国设计秀
    android:id="@+id/main"b7U中国设计秀
    android:layout_width="fill_parent"b7U中国设计秀
    android:layout_height="fill_parent">b7U中国设计秀
   <com.google.android.maps.MapViewb7U中国设计秀
       android:id="@+id/map"b7U中国设计秀
       android:layout_width="fill_parent" b7U中国设计秀
       android:layout_height="fill_parent"b7U中国设计秀
       android:enabled="true"b7U中国设计秀
       android:clickable="true"b7U中国设计秀
       android:apiKey="???????????????????????????????????"b7U中国设计秀
       />    b7U中国设计秀
</LinearLayout>b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
这里需要将api key中的????????????改成你自己申请到的api key. b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
b7U中国设计秀
Step4: 修改代码:b7U中国设计秀
b7U中国设计秀
package feisky.navigation;b7U中国设计秀
b7U中国设计秀
import com.google.android.maps.GeoPoint;b7U中国设计秀
import com.google.android.maps.MapActivity;b7U中国设计秀
import com.google.android.maps.MapController;b7U中国设计秀
import com.google.android.maps.MapView;b7U中国设计秀
import android.app.AlertDialog;b7U中国设计秀
import android.app.Dialog;b7U中国设计秀
import android.content.DialogInterface;b7U中国设计秀
import android.os.Bundle;b7U中国设计秀
import android.view.KeyEvent;b7U中国设计秀
import android.view.Menu;b7U中国设计秀
import android.view.MenuItem;b7U中国设计秀
b7U中国设计秀
public class MainActivity extends MapActivity {b7U中国设计秀
b7U中国设计秀
// 地图显示控制相关变量定义b7U中国设计秀
private MapView map=null;b7U中国设计秀
private MapController mapCon;b7U中国设计秀
b7U中国设计秀
// 菜单项b7U中国设计秀
final private int menuMode=Menu.FIRST;b7U中国设计秀
final private int menuExit=Menu.FIRST+1;b7U中国设计秀
private int chooseItem=0;b7U中国设计秀
b7U中国设计秀
    /** Called when the activity is first created. */b7U中国设计秀
    @Overrideb7U中国设计秀
    public void onCreate(Bundle savedInstanceState) {b7U中国设计秀
        super.onCreate(savedInstanceState);b7U中国设计秀
        setContentView(R.layout.main);b7U中国设计秀
        b7U中国设计秀
        // 获取MapViewb7U中国设计秀
        map=(MapView)findViewById(R.id.map);b7U中国设计秀
        // 设置显示模式b7U中国设计秀
        map.setTraffic(true);b7U中国设计秀
        map.setSatellite(false);b7U中国设计秀
        map.setStreetView(true);b7U中国设计秀
        // 设置可以缩放b7U中国设计秀
        map.setBuiltInZoomControls(true);b7U中国设计秀
        b7U中国设计秀
        // 设置初始地图的中心位置b7U中国设计秀
        GeoPoint geoBeijing=new GeoPoint((int)(39.95*1000000), (int)(116.37*1000000));b7U中国设计秀
        mapCon=map.getController();b7U中国设计秀
        mapCon.setCenter(geoBeijing);b7U中国设计秀
    }b7U中国设计秀
    b7U中国设计秀
    @Overrideb7U中国设计秀
public boolean onCreateOptionsMenu(Menu menu) {b7U中国设计秀
// 建立菜单b7U中国设计秀
     menu.add(0,menuMode,0,"地图模式");b7U中国设计秀
menu.add(0, menuExit, 1, "退出");b7U中国设计秀
return super.onCreateOptionsMenu(menu);b7U中国设计秀
}b7U中国设计秀
b7U中国设计秀
@Overrideb7U中国设计秀
public boolean onKeyDown(int keyCode, KeyEvent event) {b7U中国设计秀
return super.onKeyDown(keyCode, event);b7U中国设计秀
}b7U中国设计秀
b7U中国设计秀
@Overrideb7U中国设计秀
public boolean onMenuItemSelected(int featureId, MenuItem item) {b7U中国设计秀
switch (item.getItemId()) {b7U中国设计秀
case menuExit:b7U中国设计秀
finish();b7U中国设计秀
break;b7U中国设计秀
case menuMode:b7U中国设计秀
Dialog dMode=new AlertDialog.Builder(this)b7U中国设计秀
.setTitle("地图模式设置")b7U中国设计秀
.setSingleChoiceItems(R.array.MapMode, chooseItem, new DialogInterface.OnClickListener()b7U中国设计秀
{b7U中国设计秀
b7U中国设计秀
@Overrideb7U中国设计秀
public void onClick(DialogInterface dialog, int which) {b7U中国设计秀
chooseItem=which;b7U中国设计秀
}b7U中国设计秀
})b7U中国设计秀
.setPositiveButton("确定", new DialogInterface.OnClickListener()b7U中国设计秀
{b7U中国设计秀
b7U中国设计秀
@Overrideb7U中国设计秀
public void onClick(DialogInterface dialog, int which) {b7U中国设计秀
switch (which) {b7U中国设计秀
case 0:b7U中国设计秀
map.setSatellite(true);b7U中国设计秀
//map.setTraffic(false);b7U中国设计秀
//map.setStreetView(false);b7U中国设计秀
break;b7U中国设计秀
case 1:b7U中国设计秀
//map.setSatellite(false);b7U中国设计秀
map.setTraffic(true);b7U中国设计秀
//map.setStreetView(false);b7U中国设计秀
break;b7U中国设计秀
case 2:b7U中国设计秀
//map.setSatellite(false);b7U中国设计秀
//map.setTraffic(false);b7U中国设计秀
map.setStreetView(true);b7U中国设计秀
break;b7U中国设计秀
default:b7U中国设计秀
break;b7U中国设计秀
}b7U中国设计秀
}b7U中国设计秀
})b7U中国设计秀
.setNegativeButton("取消", new DialogInterface.OnClickListener() {b7U中国设计秀
b7U中国设计秀
@Overrideb7U中国设计秀
public void onClick(DialogInterface dialog, int which) {b7U中国设计秀
b7U中国设计秀
}b7U中国设计秀
})b7U中国设计秀
.create();b7U中国设计秀
dMode.show();b7U中国设计秀
break;b7U中国设计秀
default:b7U中国设计秀
break;b7U中国设计秀
}b7U中国设计秀
return super.onMenuItemSelected(featureId, item);

本文引用地址:/bc/article_46137.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明