阅读:3249回复:4
Android Google Map API 开发基础知识
<p>开发基于谷歌地图的应用和普通的android应用差不多都要使用它提供给我们的类库,所不同的是google
map的类库不是android平台的基本类库,是google api的一部分,所以建立项目时,SDK要选择Google APIs;</p><p> </p><p>还有一点,开发基于地图的应用时候需要使用google map的APIkey,必须先申请key,然后才能开发基于地图的应用。</p><p> </p><p>下边分步骤记录下,整个过程:</p><p> </p><p><strong> 一、申请google Maps API key(用于开发和debug)</strong></p><p> </p><p>为了能顺利的申请Android Map API Key,必须要准备google的账号和系统的证明书。一般Google发布Key都需要Google账号,Google账号是通用的,Gmail的账号就可以。当一个程序发布时必须要证明书,证明书其实就是MD5.我们这里并不是发布,而只是为了开发测试,可以使用Debug版的证明书,下面我们就来学习如何申请Debug版的Key:</p><p> </p><p>1.找到你的debug.keystore文件</p><p> </p><p>在Eclipse工具下,选择<span style="color: rgb(255, 0, 0);">windows-->Preference-->Android-->Build,</span>其中Default debug keystore的值便是debug.keystore的路径了。</p><p> </p><p>2.取得debug.keystore的MD5值</p><p> </p><p>首先cmd命令行进入debug.keystore文件所在的路径,执行命令:<span style="color: rgb(255, 0, 0);">keytool -list -keystore debug.keystore</span>,这时可能会提示你输入密码,这里默认的密码是“android",这样即可取得MD5值。</p><p> </p><p>3.申请Android Map 的API Key.</p><p> </p><p>打开浏览器,输入网址:<span style="text-decoration: underline;"><span style="color: rgb(2, 104, 205);" color="#0268cd"><a href="http://code.google.com/android/maps-api-signup.html">http://code.google.com/android/maps-api-signup.html</a></span></span><span color="#0268cd">,填入你的认证指纹(MD5)即可获得apiKey了,结果显示如下:<br/></span></p><p> </p><p><span color="#0268cd">感谢您注册 Android 地图 API 密钥!<br/>您的密钥是:<br/>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br/>我IE打开的时候是乱码,不知道是不是自己电脑有问题。后来改用了chrome就正常显示了。</span></p><p> </p><p>到此,我们就完成了API Key的申请了,记录下Key值,在后续开发中使用。(放在layout中加入的MapView中)</p> |
|
|
1楼#
发布于:2012-09-12 09:49
<p><strong>二.Google Map API的使用</strong></p>
<p>Android中定义了一个名为com.google.android.map的包,其中包含了一系列用于在google map上显示、控制和叠层信息的功能类,以下是该包中最重要的几个类:</p> <p><strong>1.MapActivity:</strong>这个类是用于显示Google Map的Activity类,它需要连接底层网络。MapActivity是一个抽象类,任何想要显示MapView的activity都需要派生自MapActivity,并且在其派生类的onCreate()中,都要创建一个MapView实例。</p> <p><strong>2.MapView:</strong>MapView是用于显示地图的View组件。它派生自android.view.ViewGroup。它必须和MapActivity配合使用,而且只能被MapActivity创建,这是因为MapView需要通过后台的线程来连接网络或者文件系统,而这些线程需要有MapActivity来管理。</p> <p><strong>3.MapController:</strong>MapController用于控制地图的移动、缩放等。</p> <p><strong>4.OverLay:</strong>这是一个可显示于地图之上的可绘制的对象。</p> <p><strong>5.GeoPoint:</strong>这是一个包含经纬度位置的对象。</p> |
|
|
2楼#
发布于:2012-09-12 09:49
<p><strong>三.实例开发</strong></p>
<p>1.创建工程,注意SDK旋转为"Goolge APIs”</p> <p>2.修改AndroidManifest.xml文件</p> <p>由于使用Google Map API,所以必须添加<uses-library android:name="com.google.android.maps" /></p> <p>由于需要从网络获取地图数据,所以需要访问网络的权限<uses-permission android:name="android.permission.INTERNET"/></p> <p>可能还需要添加其他权限。</p> <p>例如:</p> <div class="cnblogs_code"><img id="code_img_closed_0288e3e5-6938-4b0c-8cbd-0329ea78eba4" class="code_img_closed" src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"/><img style="display: none;" id="code_img_opened_0288e3e5-6938-4b0c-8cbd-0329ea78eba4" class="code_img_opened" src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif"/><span class="cnblogs_code_collapse">AndroidManifest.xml</span> <div id="cnblogs_code_open_0288e3e5-6938-4b0c-8cbd-0329ea78eba4" class="cnblogs_code_hide"><pre><div><span style="color: rgb(0, 0, 0);"><?</span><span style="color: rgb(0, 0, 0);">xml version</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">1.0</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> encoding</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">utf-8</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">?></span><span style="color: rgb(0, 0, 0);"><br/></span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">manifest xmlns:android</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">http://schemas.android.com/apk/res/android</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br/> package</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">com.yarin.android.Examples_09_03</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br/> android:versionCode</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">1</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br/> android:versionName</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">1.0</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br/> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">application android:icon</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">@drawable/icon</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> android:label</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">@string/app_name</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br/> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">uses</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">library android:name</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">com.google.android.maps</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"><br/> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">activity android:name</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">.Activity01</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br/> android:label</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">@string/app_name</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br/> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">intent</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">filter</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br/> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">action android:name</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">android.intent.action.MAIN</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"><br/> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">category android:name</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">android.intent.category.LAUNCHER</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"><br/> </span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">intent</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">filter</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br/> </span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">activity</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br/> </span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">application</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br/> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">uses</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">permission android:name</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">android.permission.INTERNET</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"><br/> </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">uses</span><span style="color: rgb(0, 0, 0);">-</span><span style="color: rgb(0, 0, 0);">sdk android:minSdkVersion</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">5</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"><br/></span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">manifest</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> </span></div></pre></div></div> <p>3.创建MapView</p> <p>要显示地图,需要创建一个MapView,在Xml文件中的布局如下。其中的android:apiKey的值就是我们第一步申请的Key了。</p> <div class="cnblogs_code"><img id="code_img_closed_3550069a-a772-41db-bbb8-593c9417e65a" class="code_img_closed" src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"/><img style="display: none;" id="code_img_opened_3550069a-a772-41db-bbb8-593c9417e65a" class="code_img_opened" src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif"/><span class="cnblogs_code_collapse">main.xml</span> <div id="cnblogs_code_open_3550069a-a772-41db-bbb8-593c9417e65a" class="cnblogs_code_hide"><pre><div><span style="color: rgb(0, 0, 0);"><?</span><span style="color: rgb(0, 0, 0);">xml version</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">1.0</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> encoding</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">utf-8</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">?></span><span style="color: rgb(0, 0, 0);"> <br/></span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">RelativeLayout xmlns:android</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">http://schemas.android.com/apk/res/android</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> <br/> android:layout_width</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">fill_parent</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> <br/> android:layout_height</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">fill_parent</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> <br/> </span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> <br/></span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">com.google.android.maps.MapView<br/> android:id</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">@+id/MapView01</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br/> android:layout_width</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">fill_parent</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br/> android:layout_height</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">fill_parent</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"><br/> android:apiKey</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">0AubmfALdupLSlQkE67OTXgcQgWtyhXcO7uhsIQ</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/></span><span style="color: rgb(0, 0, 0);"><br/></span><span style="color: rgb(0, 0, 0);"></</span><span style="color: rgb(0, 0, 0);">RelativeLayout</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> </span></div></pre></div></div> <p>当然,可以在程序中通过如下代码来创建MapView:</p> <p>MapView map = new MapView( this, "(android maps api key)");</p> <p>4.实现MapActivity</p> <p>MapView需要由MapActivity来管理,所以程序部分应该继承自MapActivity类,必须实现isRouteDisplay方法。</p> <p>MapView提供了3中模式的地图,分别可以通过以下方式设置采用什么模式来显示地图。</p> <p><span> </span> mMapView.setTraffic(true); //设置为交通模式<span> </span></p> <p><span> </span> <span> </span>mMapView.setSatellite(true); //设置为卫星模式//<span> </span></p> <p><span> </span>mMapView.setStreetView(false); //设置为街景模式</p> <p>通过setBuiltZoomControls方法设置地图是否支持缩放。</p> |
|
|
3楼#
发布于:2012-09-12 09:50
<p>5.MapController的使用</p>
<p>如果需要设置地图显示的地点以及放大倍数等,就需要使用MapController来控制地图。可以通过如下代码获得MapController对象:</p> <p>mMapController = mMapView.getController(); </p> <p>要定位地点,需要构造一个GeoPoint来表示地点的经纬度,然后使用animateTo方法将地图定位到指定的GeoPoint上,代码如下:</p> <div class="cnblogs_code"><pre><div><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设置起点为成都</span><span style="color: rgb(0, 128, 0);"><br/></span><span style="color: rgb(0, 0, 0);">mGeoPoint </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> GeoPoint((</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">) (</span><span style="color: rgb(0, 0, 0);">30.659259</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1000000</span><span style="color: rgb(0, 0, 0);">), (</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">) (</span><span style="color: rgb(0, 0, 0);">104.065762</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1000000</span><span style="color: rgb(0, 0, 0);">));<br/></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">定位到成都</span><span style="color: rgb(0, 128, 0);"><br/></span><span style="color: rgb(0, 0, 0);">mMapController.animateTo(mGeoPoint); </span></div></pre></div> <p>6.Ovelay的使用</p> <p>如果需要在地图上标注一些图标文字等信息,就需要使用Overlay。这里我们首先要将地图上的经度和纬度转换成屏幕上的实际坐标,才能将信息绘制上去。Map API中提供了Projection.toPixels(GeoPoint in,GeoPoint out)方法,可以将经度和纬度转换成屏幕上的坐标。</p> <p>首先需要实现OverLay中的draw方法才能在地图上绘制信息,代码如下:</p> <div class="cnblogs_code"> <div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif"/></a></span></div><pre><div><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> MyLocationOverlay </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Overlay<br/>{<br/> @Override<br/> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> draw(Canvas canvas, MapView mapView, </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> shadow, </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> when)<br/> {<br/> ........<br/> }<br/>}</span></div></pre> <div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif"/></a></span></div></div> <p>综合上面的代码如下</p> <div class="cnblogs_code"><img style="display: none;" id="code_img_closed_aaf5680f-5043-4830-a078-34709a85dd77" class="code_img_closed" src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" jquery170732355308663331="2"/><img id="code_img_opened_aaf5680f-5043-4830-a078-34709a85dd77" class="code_img_opened" src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif"/><span class="cnblogs_code_collapse">Activity01.java</span> <div style="display: block;" id="cnblogs_code_open_aaf5680f-5043-4830-a078-34709a85dd77" class="cnblogs_code_hide" jquery170732355308663331="1"> <div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码" href="javascript:void(0);"><img alt="复制代码" src="http://common.cnblogs.com/images/copycode.gif"/></a></span></div><pre><div><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> java.util.List;<br/></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> android.graphics.Bitmap;<br/></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> android.graphics.BitmapFactory;<br/></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> android.graphics.Canvas;<br/></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> android.graphics.Paint;<br/></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> android.graphics.Point;<br/></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> android.os.Bundle;<br/></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> com.google.android.maps.GeoPoint;<br/></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> com.google.android.maps.MapActivity;<br/></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> com.google.android.maps.MapController;<br/></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> com.google.android.maps.MapView;<br/></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);"> com.google.android.maps.Overlay;<br/><br/></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> Activity01 </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> MapActivity<br/>{<br/> </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> MapView mMapView;<br/> </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> MapController mMapController; <br/> </span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);"> GeoPoint mGeoPoint;<br/> </span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);"> Called when the activity is first created. </span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);"><br/> @Override<br/> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> onCreate(Bundle savedInstanceState)<br/> {<br/> </span><span style="color: rgb(0, 0, 255);">super</span><span style="color: rgb(0, 0, 0);">.onCreate(savedInstanceState);<br/> setContentView(R.layout.main);<br/> mMapView </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (MapView) findViewById(R.id.MapView01);<br/> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设置为交通模式<br/></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> mMapView.setTraffic(true);<br/> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设置为卫星模式<br/></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> mMapView.setSatellite(true); <br/> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设置为街景模式</span><span style="color: rgb(0, 128, 0);"><br/></span><span style="color: rgb(0, 0, 0);"> mMapView.setStreetView(</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">);<br/> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">取得MapController对象(控制MapView)</span><span style="color: rgb(0, 128, 0);"><br/></span><span style="color: rgb(0, 0, 0);"> mMapController </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> mMapView.getController(); <br/> mMapView.setEnabled(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);<br/> mMapView.setClickable(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);<br/> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设置地图支持缩放</span><span style="color: rgb(0, 128, 0);"><br/></span><span style="color: rgb(0, 0, 0);"> mMapView.setBuiltInZoomControls(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">); <br/> <br/> <br/> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设置起点为成都</span><span style="color: rgb(0, 128, 0);"><br/></span><span style="color: rgb(0, 0, 0);"> mGeoPoint </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> GeoPoint((</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">) (</span><span style="color: rgb(0, 0, 0);">30.659259</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1000000</span><span style="color: rgb(0, 0, 0);">), (</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">) (</span><span style="color: rgb(0, 0, 0);">104.065762</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">1000000</span><span style="color: rgb(0, 0, 0);">));<br/> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">定位到成都</span><span style="color: rgb(0, 128, 0);"><br/></span><span style="color: rgb(0, 0, 0);"> mMapController.animateTo(mGeoPoint); <br/> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设置倍数(1-21)</span><span style="color: rgb(0, 128, 0);"><br/></span><span style="color: rgb(0, 0, 0);"> mMapController.setZoom(</span><span style="color: rgb(0, 0, 0);">15</span><span style="color: rgb(0, 0, 0);">); <br/> <br/> <br/> <br/> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">添加Overlay,用于显示标注信息</span><span style="color: rgb(0, 128, 0);"><br/></span><span style="color: rgb(0, 0, 0);"> MyLocationOverlay myLocationOverlay </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> MyLocationOverlay();<br/> List</span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">Overlay</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"> list </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> mMapView.getOverlays();<br/> list.add(myLocationOverlay);<br/> }<br/> </span><span style="color: rgb(0, 0, 255);">protected</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> isRouteDisplayed()<br/> {<br/> </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br/> }<br/> </span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> MyLocationOverlay </span><span style="color: rgb(0, 0, 255);">extends</span><span style="color: rgb(0, 0, 0);"> Overlay<br/> {<br/> @Override<br/> </span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> draw(Canvas canvas, MapView mapView, </span><span style="color: rgb(0, 0, 255);">boolean</span><span style="color: rgb(0, 0, 0);"> shadow, </span><span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> when)<br/> {<br/> </span><span style="color: rgb(0, 0, 255);">super</span><span style="color: rgb(0, 0, 0);">.draw(canvas, mapView, shadow);<br/> Paint paint </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Paint();<br/> Point myScreenCoords </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> Point();<br/> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 将经纬度转换成实际屏幕坐标</span><span style="color: rgb(0, 128, 0);"><br/></span><span style="color: rgb(0, 0, 0);"> mapView.getProjection().toPixels(mGeoPoint, myScreenCoords);<br/> paint.setStrokeWidth(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);<br/> paint.setARGB(</span><span style="color: rgb(0, 0, 0);">255</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">255</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br/> paint.setStyle(Paint.Style.STROKE);<br/> Bitmap bmp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> BitmapFactory.decodeResource(getResources(), R.drawable.home);<br/> canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);<br/> canvas.drawText(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">天府广场</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, myScreenCoords.x, myScreenCoords.y, paint);<br/> </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br/> }<br/> }<br/>}</span></div></pre></div></div> |
|
|
4楼#
发布于:2012-12-11 17:00
不错,有用
|
|