Android 的 Mapview & Location 使用教程

fmms 12年前
     <p>本教程将探讨如何使用谷歌地图API来显示出信息点(POI),并使用手机提供的定位服务,来显示当前位置可搜索到的POI位置。这种情况下,我们应使用商场作为POI。</p>    <p> </p>    <p>我把教程分为了两部分。 <br /> 第一部分(在本节中)将介绍包括:<br /> 1.MapView对象的使用 <br /> 2.使用MD5密文获取一个谷歌地图API密钥 <br /> 3.实现一个地点监听器,以获得你的当前位置 </p>    <p>第二部分将包括:<br /> 1.在应用程序中使用外部库类 <br /> 2.创建显示出你的当前位置和周边的信息点 </p>    <p>现在让我们开始吧!</p>    <h4>步骤1:创建一个新的Android项目</h4>    <p>启动Eclipse IDE并创建一个新的Android项目。 <br /> File > Create > New Android Project</p>    <p>使用以下设置创建新的项目:</p>    <p><strong>项目名称</strong>:MallFinder <br /> <strong>生成目标</strong>:Google APIs Platform–2.1  API Level 7 <br /> <strong>应用程序名</strong>:Mall Finder <br /> <strong>包名称</strong>:com.shawnbe.mallfinder <br /> <strong>创建Activity</strong>:MallFinderActivity <br /> <strong>最低SDK版本</strong>:7</p>    <p><a href="https://simg.open-open.com/show/194d0d88fae63e50f5153cf1abe7a7ca.png" rel="nofollow"><img title="Step1" alt="Android 的 Mapview & Location 使用教程" src="https://simg.open-open.com/show/2ea66b5139e3f2f00d3a8b8956448293.png" width="591" height="530" /></a> <br /> 设置完成后,点击完成。 </p>    <h4><br /> 步骤2:在谷歌上注册获取API密钥</h4>    <p>因为MapView对象使用谷歌地图,你需要在谷歌注册一个API密钥,并同意服务条款,然后才可以使用他们。注册过程相当简单。想要了解更多关于获得地图API密钥信息,<a href="/misc/goto?guid=4959517655590625748" rel="nofollow">请查看此链接</a>。</p>    <p>要对你的应用程序进行签名就必须要用到的MD5密文生成的API秘钥,听起来很令人困惑,但它并不难,因为你会发现它像当我们之前的几个步骤 。而不是上面的链接所述,使用命令行的方式得到你的MD5密文,我们将使用keytool的一个Eclipse插件 。想要了解更多有关keytool的插件信息,<a href="/misc/goto?guid=4959517655683594092" rel="nofollow">请查看此链接</a>。</p>    <h4><br /> 步骤3:安装keytool插件</h4>    <p>在Eclipse IDE中,导航到 Help > Install New Software。</p>    <p><a href="https://simg.open-open.com/show/05a5f5734360f5f73c37c3bfe0b131d2.png" rel="nofollow"><img title="step3" alt="Android 的 Mapview & Location 使用教程" src="https://simg.open-open.com/show/a1f4a851f6ce26454f85319758ca6691.png" width="409" height="327" /></a></p>    <p>当新的窗口弹出,在窗口的顶部点击“Add”按钮。</p>    <p>在“Name”栏输入“keytool”(不含引号), <br /> 在“Location”栏输入“http://www.keytool.sourceforge.net/update”</p>    <p><a href="https://simg.open-open.com/show/35dab58efec0fa798dfdb7b3fabfe9ec.png" rel="nofollow"><img title="step3screen2" alt="Android 的 Mapview & Location 使用教程" src="https://simg.open-open.com/show/35a51154fa1107fa9b66478ef424ce9e.png" width="510" height="223" /></a></p>    <p>点击“OK”。</p>    <p>经过短暂的加载时间,窗口将出现一个标有“keytool”的复选框。选中该复选框并单击“下一步”。同意协议的条款,然后单击“下一步”完成。</p>    <p><strong>注意:在安装过程中,可能会提示您确定是否信任安全证书。如果你确定,就选中复选框并单击“确定”继续安装。 <br /> <a href="https://simg.open-open.com/show/0305fb68a9de99b95346119175530165.png" rel="nofollow"><img title="step3screen3" alt="Android 的 Mapview & Location 使用教程" src="https://simg.open-open.com/show/78f141457c7816954522836b979bb67a.png" width="589" height="718" /></a></strong></p>    <p>安装完成后,将会提示重新启动Eclipse。 <br /> </p>    <h4><br /> 步骤4:获取MD5密文</h4>    <p>Eclipse重新启动之后,你应该看到一个新的菜单项“keytool”,它旁边有一个小钥匙图标。 <br /> <a href="https://simg.open-open.com/show/c16b00b178a220e813172ad87a70e894.png" rel="nofollow"><img title="step4" alt="Android 的 Mapview & Location 使用教程" src="https://simg.open-open.com/show/e755ef6c56d0daf7c1979587ca45e457.png" width="423" height="96" /></a> <br /> </p>    <p>我们现在要打开调试的keystore。 <br /> <strong>注意:</strong>由于操作系统的位置可能会有所不同。各种操作系统的默认位置:</p>    <ul>     <li><strong>Windows Vista :</strong> C:\Users\      <user>       \.android\debug.keystore      </user></li>     <li><strong>Windows XP :</strong> C:\Documents and Settings\      <user>       \.android\debug.keystore      </user></li>     <li><strong>OS X and Linux :</strong> ~/.android/debug.keystore <br /> <p>点击keytool菜单项>打开密钥库。</p> <p>点击位于“文件名”文本框右侧的“浏览”按钮,并找到keystore的位置(上述为默认位置),并选择debug.keystore文件。</p> <p>单击“打开”,选择类型“android”(不带引号),这是默认的调试密码,然后单击“Load”。 <br /> <a href="https://simg.open-open.com/show/f9645dec9fc02987245d9ae36ceee48b.png" rel="nofollow"><img title="step4screen2" alt="Android 的 Mapview & Location 使用教程" src="https://simg.open-open.com/show/d9d9989becf2782a5126b344d7ae37d4.png" width="532" height="219" /></a></p> </li>    </ul>    <p>现在应该在屏幕底部的面板可见一个新的keytool的标签(如果你没有看到它,导航到“Window > Open Perspective > Java Browsing”)。 <br /> <a href="https://simg.open-open.com/show/e0a1befd259fbac528c392f2b0272156.png" rel="nofollow"><img title="step4screen3" alt="Android 的 Mapview & Location 使用教程" src="https://simg.open-open.com/show/c66c3131bf9a220ac9bfde006162de75.png" width="561" height="109" /></a></p>    <p>点击debug.keystore路径左侧的小箭头将显示androiddebugkey。</p>    <p>双击androiddubugkey并复制MD5密文。</p>    <p>打开Web浏览器,进入以下网址。   <a href="/misc/goto?guid=4959517655833163462" rel="nofollow">http://code.google.com/android/maps-api-signup.html</a></p>    <p>阅读并同意条款,在文本框中输入MD5密文,并点击“Generate API Key”。请注意保存好你的API密钥,因为使用MapView的时候需要它。 <br /> </p>    <h4>步骤5:在布局中添加MapView</h4>    <p>在这一步,我们将添加一个MapView到布局文件。</p> 打开位于MallFinder > res > layout> main.xml中的main.xml文件,并修改该文件如下所示,包括一个FrameLayout和MapView:    <div class="wlWriterEditableSmartContent">     <pre class="brush: xml; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 350px;"><?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" >     <FrameLayout     android:layout_width="fill_parent"      android:layout_height="fill_parent">         <com.google.android.maps.MapView         android:id="@+id/mapView"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:clickable="true"          android:apiKey="PUT YOUR API KEY HERE"/>     </FrameLayout> </LinearLayout></pre>    </div>    <p>上述布局在整个可用的屏幕空间创建一个MapView。在当前状态下运行的应用程序,将导致在强制关闭。我们还需要完成多个步骤。 <br /> </p>    <h4>步骤6:设置权限,并导入所需的库</h4>    <p>由于我们的应用程序将会从Google Maps下载数据,同时需要从手机的GPS或其他定位服务访问信息,所以我们需要在AndroidManifest文件中的声明必要的权限来使用这些服务。</p>    <p>要做到这一点,打开位于MallFinder > AndroidManifest.xml中的AndroidManifest.xml文件。</p>    <p>在关闭应用程序标记(</application>)后面,和在闭幕清单标记(</manifest>)之前添加以下几行。 <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: xml; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 76px;"><uses-feature android:name="android.hardware.location.gps"/>  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET"/></pre>    </div>    <p></p>    <p>在这个例子中,我不需要FINE_LOCATION,但我已经包含在此,我想你可能要调整你的代码来测试各种提供商,如果你想获得更准确的位置,你将需要FINE_LOCATION权限。通常任何生产中的应用程序都不应该声明不需要的权限。如你要求更多的权限,这是一个非常糟糕的做法,使用户警惕你的应用程序。如果你不打算使用更准确的供应商,你可以不声明android.permission.ACCESS_COARSE_LOCATION。</p>    <p>为了在我们的MapView中使用谷歌地图,我们需要在Manifest文件中声明该库,这样我们要在关闭活动标记和关闭应用程序标记代码之间加入下面的标签:</p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: xml; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 44px;"><uses-library android:required="true" android:name="com.google.android.maps" /></pre>    </div> 我们可以把标题栏去掉,因为我觉得这是不必要的。它使用了太多我们有限的屏幕空间。在应用程序删除标题栏添加下面代码:    <div class="wlWriterEditableSmartContent">     <pre class="brush: xml; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 44px;">android:theme="@android:style/Theme.NoTitleBar"</pre>    </div>    <p>现在我们完整的Manifest文件看起来像这样: <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: xml; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;"><?xml version="1.0" encoding="utf-8"?>  <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.shawnbe.mallfinder"     android:versionCode="1"     android:versionName="1.0" >     <uses-sdk android:minSdkVersion="7" />      <application         android:icon="@drawable/ic_launcher"         android:label="@string/app_name"         android:theme="@android:style/Theme.NoTitleBar">         <activity             android:label="@string/app_name"             android:name=".MallFinderActivity" >              <intent-filter >                 <action android:name="android.intent.action.MAIN" />                   <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>          </activity>         <uses-library android:required="true" android:name="com.google.android.maps" />     </application>     <uses-feature android:name="android.hardware.location.gps"/>      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>     <uses-permission android:name="android.permission.INTERNET"/> </manifest></pre>    </div>    <p></p>    <h4>步骤7:设置MapView</h4>    <p>在我们运行MapView之前,我们还需要做一些MallFinderActivity类的调整。 <br /> 打开的主要活动类(MallFinderActivity.java),这是位于:MallFinder > src > com.shawnbe.mallfinder > MallFinderActivity.java</p>    <p>在我们的应用程序中,我们需要将这个类继承“MapActivity”,而不是“Activity”。</p>    <p>修改行: <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;">public class MallFinderActivity extends Activity {</pre>    </div>    <p></p>    <p>改成: <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;">public class MallFinderActivity extends MapActivity {</pre>    </div>    <p></p>    <p>继承MapActivity类时,我们需要实现<code>isRouteDisplayed()</code>方法,所以我们需要添加下面的方法 : <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;">@Override protected boolean isRouteDisplayed() {     // TODO Auto-generated method stub     return false; }</pre>    </div>    <p></p>    <p>这时,我们可以启动应用程序并显示MapView,但我们的没有设置选项,如缩放级别和可见图层。我们开始在onCreate方法中声明以下变量: <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;">private MapController mapController; private MapView mapView;</pre>    </div>    <p></p>    <p>在<code>onCreate</code>方法中添加以下几行代码: <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;">mapView = (MapView)findViewById(R.id.mapView); mapView.setBuiltInZoomControls(true); mapView.setSatellite(false); mapView.setStreetView(true); mapController = mapView.getController(); mapController.setZoom(13);</pre>    </div>    <p></p>    <p>上面的代码只显示街层并隐藏的卫星层。我个人喜好选择它,因为我觉得在这种情况下更容易读。正如你喜欢用布尔值false和true来随意调整一样。此外,缩放级别设置为13,你也可以调整到1作为整个世界视图来看和20最大变焦。</p>    <p>最后,我们可以继续启动应用程序,以确保一切运行像预期一样。 <br /> 有几种方法来运行应用程序,首先:</p>    <p>在Package Explorer window中右键单击项目 > Run as > Android Application</p>    <p>或者</p>    <p>从菜单上,单击Run > Run <br /> <a href="https://simg.open-open.com/show/1a4b29aacfa2924f4411655395c6cf06.png" rel="nofollow"><img title="step7" border="0" alt="step7" src="https://simg.open-open.com/show/1330fb23da117d24809ef58009d2bc77.png" width="517" height="272" /></a></p>    <p>然后选择你的手机或模拟器。你应该可以看到一个地图覆盖整个屏幕,除了在屏幕上方的通知栏,而且可以滚动并使用缩放。 <br /> <a href="https://simg.open-open.com/show/15f8939171a0da07efaa311c1afbc6c2.png" rel="nofollow"><img title="step7screen2" border="0" alt="step7screen2" src="https://simg.open-open.com/show/e762cae1b455f55b85c8c6647099a315.png" width="484" height="804" /></a></p>    <h4>步骤8:获取你当前的位置</h4>    <p>要获得你的当前位置,我们<code>使用</code> locationManager类,它允许应用程序获取设备的位置。此过程可能需要一些时间来定位当前位置,因此,建议使用你<code>的</code>lastKnownLocation,直到你可以得到一个更准确的当前的位置。</p>    <p>再声明以下两个变量在步骤7中: <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;">private LocationManager locationManager; private GeoPoint currentLocation;</pre>    </div>    <p></p>    <p>此外,将以下方法添加到<code>MallFinderActivity.java</code>类: <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;">public void getLastLocation(){     String provider = getBestProvider();     currentLocation = locationManager.getLastKnownLocation(provider);     if(currentLocation != null){         setCurrentLocation(currentLocation);     }     else     {         Toast.makeText(this, "Location not yet acquired", Toast.LENGTH_LONG).show();     } }   public void animateToCurrentLocation(){     if(currentPoint!=null){         mapController.animateTo(currentPoint);     } }   public String getBestProvider(){     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);     Criteria criteria = new Criteria();     criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);     criteria.setAccuracy(Criteria.NO_REQUIREMENT);     String bestProvider = locationManager.getBestProvider(criteria, true);     return bestProvider; }   public void setCurrentLocation(Location location){     int currLatitude = (int) (location.getLatitude()*1E6);     int currLongitude = (int) (location.getLongitude()*1E6);     currentLocation = new GeoPoint(currLatitude,currLongitude);       currentLocation = new Location("");     currentLocation.setLatitude(currentPoint.getLatitudeE6() / 1e6);     currentLocation.setLongitude(currentPoint.getLongitudeE6() / 1e6); }</pre>    </div>    <p></p>    <p>在onCreate方法中添加以下两行: <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;">getLastLocation(); animateToCurrentLocation();</pre>    </div>    <p></p>    <p>onCreate方法现在看起来: <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;">public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);     mapView = (MapView)findViewById(R.id.mapView);     mapView.setBuiltInZoomControls(true);     mapView.setSatellite(false);     mapView.setStreetView(true);     mapController = mapView.getController();     mapController.setZoom(13);     getLastLocation();     animateToCurrentLocation(); }</pre>    </div>    <p></p>    <p>getLastLocation方法创建的locationManager类的实例,这是基于最好的供应商使用定位服务的要求。在上面的代码中,我们没有指定任何条件的供应商,但是你可以根据你的精度和功耗要求的标准来调整使用criteria.setAccuracy()和criteria.setPowerRequirement()方法。上述步骤6中,我提到,你可能需要调整你的标准,以获得更精确的的位置。如果你想做到这一点,你设置的精度标准ACCURACY_FINE的代码是: <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;">criteria.setAccuracy(Criteria.ACCURACY_FINE);</pre>    </div>    <p></p>    <p>我没有选择最精确的供应商(GPS)的原因是,因为我在室内测试此代码,并可能永远不会得到我位置。如果你能用GPS定位到当前位置,你随时可以改变准确性在外面测试它。</p>    <p>由于你的手机可能会使用不同的供应商指定的标准,这通常是GPS或网络(也可以是别的)。如果设置为currentLocation,最后知道的位置至少要等到我们得到了更准确和更新的位置。我们滚动围绕这些坐标为中心的地图才能发现当前位置的经度和纬度。</p>    <p>现在我们得到最后一个已知的位置,我们使用位置监听器来检测我们的位置变化来更新应用程序。要做到这一点,我们需要我们的类(MallFinderActivity.java)实现locationListener。首先,我们修改行: <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;">public class MallFinderActivity extends MapActivity {</pre>    </div>    <p></p>    <p>改成: <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;">public class MallFinderActivity extends MapActivity implements LocationListener{</pre>    </div>    <p></p>    <p>实现地点监听器,需要我们重写一组方法,所以我们要添加以下标准方法如下(我指的这些标准方法,实际上并不做任何事情): <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;">@Override public void onLocationChanged(Location arg0) {     // TODO Auto-generated method stub }   @Override public void onProviderDisabled(String arg0) {     // TODO Auto-generated method stub }   @Override public void onProviderEnabled(String arg0) {     // TODO Auto-generated method stub }   @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) {     // TODO Auto-generated method stub }</pre>    </div>    <p></p>    <p>我们将使用<code>onLocationChanged</code>的方法检测我们的位置更新,然后使用 setcurrentLocation方法设置为我们当前的位置。</p>    <p>修改onlocationChanged的方法如下: <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;">@Override public void onLocationChanged(Location newLocation) {     // TODO Auto-generated method stub     setCurrentLocation(newLocation); }</pre>    </div>    <p></p>    <p>添加以下几行代码。这些方法会在每当应用程序启动或恢复的时候,请求更新;当应用程序被暂停或关闭的同时,也将停止检查更新。 <br /> </p>    <div class="wlWriterEditableSmartContent">     <pre class="brush: java; gutter: true; first-line: 1; tab-size: 4; toolbar: true; width: 853px; height: 388px;">@Override protected void onResume() {     super.onResume();     locationManager.requestLocationUpdates(getBestProvider(), 1000, 1, this); }   @Override protected void onPause() {     super.onPause();     locationManager.removeUpdates(this); }</pre>    </div>    <p></p>    <p>如果此时运行的应用程序的MapView应围绕你最后知道的位置,当你的手机能够定位,它将会围绕你的当前位置为中心更新。由于在步骤7设置定位的精确性,得到你的位置时间长度会有所不同。 <br /> 在这一节中,我们已经学会如何使用的MapView,实现locationListener,并取得你的当前位置。在下一节中,我们将扩展包括使用外部的库来处理的MapView上信息点的覆盖和使用气球弹出窗口显示一些信息。</p>