android畢業(yè)設(shè)計(jì)外文資料翻譯--使用mapview和mapactivity顯示地圖_第1頁(yè)
已閱讀1頁(yè),還剩16頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、<p><b>  中文4128字</b></p><p>  畢業(yè)設(shè)計(jì)(論文)外文資料翻譯</p><p>  外文出處 Mark Murphy.Beginning </p><p>  Android 2 Chapter 33 </p><p>  Mapping with M

2、apView and MapActivity</p><p>  One of Google's most popular services-after search of course-is Google Maps, which lets you find everything from the nearest pizza parlor to directions from New York City

3、to San Francisco (only 2,905 miles!), along with supplying street views and satellite imagery.</p><p>  Most Android devices, not surprisingly, integrate Google Maps. For those that do, there is a mapping ac

4、tivity available to users directly from the main Android launcher. More relevant to you, as a developer, are MapView and MapActivity, which allow you to integrate maps into your own applications. Not only can you display

5、 maps, control the zoom level, and allow people to pan around, but you can tie in Android's location-based services (covered in Chapter 32) to show where the device is and where</p><p>  Fortunately, int

6、egrating basic mapping features into your Android project is fairly easy. And there is also a fair bit of power available to you, if you want to get fancy.</p><p>  Terms, Not of Endearment</p><p&

7、gt;  Integrating Google Maps into your own application requires agreeing to a fairly lengthy set of legal terms. These terms include clauses that you may find unpalatable.</p><p>  If you are considering Goo

8、gle Maps, please review these terms closely to determine if your intended use will not run afoul of any clauses. You are strongly recommended to seek professional legal counsel if there are any potential areas of conflic

9、t.</p><p>  Also, keep your eyes peeled for other mapping options, based on other sources of map data, such as OpenStreetMap (http://www.openstreetmap.org/).</p><p><b>  Piling On</b>

10、;</p><p>  As of Android l.5, Google Maps is not strictly part of the Android SDK. Instead, it is part of the Google APIs add-on, an extension of the stock SDK. The Android add-on system provides hooks for o

11、ther subsystems that may be part of some devices but not others.</p><p>  NOTE: Google Maps is not part of the Android open source project, and undoubtedly there will be some devices that lack Google Maps du

12、e to licensing issues. For example, at the time of this writing, the Archos 5 Android tablet does not have Google Maps.</p><p>  By and large, the fact that Google Maps is in an add-on does not affect your d

13、ay-to-day development. However, bear in mind the following:</p><p>  You will need to create your project with a suitable target to ensure the Google Maps APIs will be available.</p><p>  To tes

14、t your Google Maps integration, you will also need an AVD that supports the Google Maps API.</p><p>  The Bare Bones</p><p>  Far and away the simplest way to get a map into your application is

15、to create your own subclass of MapActivity. Like ListActivity, which wraps up some of the smarts behind having an activity dominated by a ListView, MapActivity handles some of the nuances of setting up an activity domina

16、ted by a MapView.</p><p>  In your layout for the MapActivity subclass, you need to add an element named, at the time of this writing, com.google.android.maps.MapView. This is the "longhand" way to

17、 spell out the names of widget classes, by including the full package name along with the class name. This is necessary because MapView is not in the com.google.android.widget namespace. You can give the MapView widget w

18、hatever android:id attribute value you want, plus handle all the layout details to have it render properly alo</p><p>  However, you do need to have these two items:</p><p>  android:apiKey, whi

19、ch in production will need to be a Google Maps API key</p><p>  android:clickable="true", if you want users to be able to click and pan through your map</p><p>  For example, from the

20、Maps/NooYawk sample application, here is the main layout:</p><p>  <?xml version="l.0" encoding="utf-8"?></p><p>  <RelativeLayout xmlns:android="http://schemas

21、.android.com/apk/res/android"</p><p>  android:layout_width="fill_parent"</p><p>  android:layout_height="fill_parent"></p><p>  <com.google.android.m

22、aps.MapView android:id="@+id/map"</p><p>  android:layout_width=" fill_parent"</p><p>  android:layout_height="fill_parent"</p><p>  android:apiKey=&qu

23、ot;<YOUR_ API_KEY>"</p><p>  android:clickable="true" /></p><p>  </RelativeLayout></p><p>  We'll cover that mysterious apiKey later in this chapt

24、er, in the "The Key to It All" section. In addition, you will need a couple of extra things in your AndroidManifest.xml file:</p><p>  The INTERNET and ACCESS_COARSE_LOCATION permissions (the latte

25、r for use with the MyLocationOverlay class, described later in this chapter)</p><p>  Inside your <application>, a <uses-library> element with</p><p>  android:name ="com.google

26、.android.maps", to indicate you are using one of the optional Android APIs</p><p>  Here is the AndroidManifest.xml file for NooYawk:</p><p>  <?xml version=”1.0” encoding=”utf-8”?>&l

27、t;/p><p>  <manifest xmlns:android=”http://schemas.android.com/apk/res/android”</p><p>  package="com.commonsware.android.maps”></p><p>  <uses-permission android:name=”a

28、ndroid. permission.INTERNET” /></p><p>  <uses-permission android:name=”android.permission.ACCESS_COARES_LOCATION” /></p><p>  <application android:label="@string/app_name"

29、</p><p>  android:icon="@drawable/cw"></p><p>  <uses-library android:name=”com.google.android.maps"/></p><p>  <activity android:name=".NooYawk” a

30、ndroid:label="@string/app_name"></p><p>  <intent-filter></p><p>  <action android:name="android.intent.action.MAIN" /></p><p>  <category andr

31、oid:name="android.intent.category.LAUNCHER" /></p><p>  </intent-filter></p><p>  </activity></p><p>  </application></p><p>  </manif

32、est></p><p>  That is pretty much all you need for starters, plus to subclass your activity from MapActivity. If you were to do nothing else, and built that project and tossed it in the emulator, you woul

33、d get a nice map of the world. Note, however, that MapActivity is abstract. You need to implement isRouteDisplayed() to indicate if you are supplying some sort of driving directions.</p><p>  In theory, user

34、s could pan around the map using the D-pad. However, that's not terribly useful when they have the whole world in their hands.</p><p>  Since a map of the world is not much good by itself, we need to add

35、 a few things, as described next.</p><p>  Exercising Your Control</p><p>  You can find your MapView widget by findViewById(), just as with any other widget. The widget itself offers a getMapCo

36、ntroller() method. Between the MapView and MapController, you have a fair bit of capability to determine what the map shows and how it behaves. The following sections cover zoom and center, the features you will most lik

37、ely want to use.</p><p><b>  Zoom</b></p><p>  The map of the world you start with is rather broad. Usually, people looking at a map on a phone will be expecting something a bit narr

38、ower in scope, such as a few city blocks.</p><p>  You can control the zoom level directly via the setZoom() method on the MapController. This takes an integer representing the level of zoom, where 1 is the

39、world view and 21 is the tightest zoom you can get. Each level is a doubling of the effective resolution: 1 has the equator measuring 256 pixels wide, while 21 has the equator measuring 268,435,456 pixels wide. Since the

40、 phone's display probably doesn't have 268,435,456 pixels in either dimension, the user sees a small map focused on one tiny</p><p>  If you wish to allow users to change the zoom level, call setBuil

41、tInZoomControls (true);, and the user will be able to zoom in and out of the map via zoom controls found at the bottom center of the map.</p><p><b>  Center</b></p><p>  Typically, y

42、ou will need to control what the map is showing, beyond the zoom level, such as the user's current location or a location saved with some data in your activity. To change the map's position, call setCenter() on t

43、he MapController.</p><p>  The setCenter() method takes a GeoPoint as a parameter. A GeoPoint represents a location, via latitude and longitude. The catch is that the GeoPoint stores latitude and longitude a

44、s integers representing the actual latitude and longitude multiplied by 1E6. This saves a bit of memory versus storing a float or double, and it greatly speeds up some internal calculations Android needs to do to conve

45、rt the GeoPoint into a map position. However, it does mean you must remember to multiply the real-wo</p><p>  Rugged Terrain</p><p>  Just as the Google Maps service you use on your full-size co

46、mputer can display satellite imagery, so can Android maps.</p><p>  MapView offers toggleSatellite(), which, as the name suggests, toggles on and off the satellite perspective on the area being viewed. You c

47、an have the user trigger these via an options menu or, in the case of NooYawk, via key presses:</p><p><b>  @Override</b></p><p>  public boolean onKeyDown(int keyCode, KeyEvent even

48、t) {</p><p>  if (keyCode == KeyEvent.KEYCODE_S) {</p><p>  map.setSatellite( ! map.isSatellite());</p><p>  return(true);</p><p>  }else if(keycode== KeyEvent.KEYCODE_

49、Z) {</p><p>  Map.dispalyZoomControls(true);</p><p>  Return(true);</p><p><b>  }</b></p><p>  return(super.onKeyDown(keyCode, event));}</p><p>

50、;  Layers upon Layers</p><p>  If you have ever used the full-size edition of Google Maps, you are probably used to seeing things overlaid atop the map itself, such as pushpins indicating businesses near the

51、 location being searched. In map parlance (and, for that matter, in many serious graphic editors), the pushpins are on a layer separate from than the map itself, and what you are seeing is the composition of the pushpin

52、layer atop the map layer.</p><p>  Android's mapping allows you to create layers as well, so you can mark up the maps as you need to based on user input and your application's purpose. For example, N

53、ooYawk uses a layer to show where select buildings are located in the island of Manhattan.</p><p>  Overlay Classes</p><p>  Any overlay you want to add to your map needs to be implemented as a

54、subclass of Overlay. There is an ItemizedOverlay subclass available if you are looking to add pushpins or the like; ItemizedOverlay simplifies this process.</p><p>  To attach an overlay class to your map, j

55、ust call getOverlays() on your MapView and add () your Overlay instance to it, as we do here with a custom SitesOverlay:</p><p>  marker.setBounds(0, 0, marker.getIntrinsicWidth(),marker.getIntrinsicHeight()

56、);</p><p>  map.getOverlays().add(new SitesOverlay(marker));</p><p>  We will take a closer look at that marker in the next section.</p><p>  Drawing the ItemizedOverlay</p>

57、<p>  As the name suggests, ItemizedOverlay allows you to supply a list of points of interest to be displayed on the map-specifically, instances of OverlayItem. The overlay handles much of the drawing logic for you.

58、 Here are the minimum steps to make this work:</p><p>  1. Override ItemizedOverlay<OverlayItem> as your own subclass (in this example, SitesOverlay).</p><p>  2. In the constructor, bui

59、ld your roster of OverlayItem instances, and call populate() when they are ready for use by the overlay.</p><p>  3. Implement size() to return the number of items to be handled by the overlay.</p>&

60、lt;p>  4. Override createItem() to return OverlayItem instances given an index.</p><p>  5. When you instantiate your ItemizedOverlay subclass, provide it with a Drawable that represents the default ico

61、n (e.g., a pushpin) to display for each item.</p><p>  The marker from the NooYawk constructor is the Drawable used for step 5. It shows a pushpin.</p><p>  You may also wish to override draw

62、() to do a better job of handling the shadow for your markers. While the map will handle casting a shadow for you, it appears you need to provide a bit of assistance for it to know where the bottom of your icon is, so it

63、 can draw the shadow appropriately.</p><p>  For example, here is SitesOverlay:</p><p>  private class SitesOverlay extends ItemizedOverlay<OverlayItem> {</p><p>  private Lis

64、t<OverlayItem> items=new ArrayList<OverlayItem>();</p><p>  private Drawable marker=null;</p><p>  public SitesOverlay(Drawable marker){</p><p>  super(marker);</p>

65、;<p>  this.marker=marker;</p><p>  items.add(new OverlayItem(getPoint(40.748963847316034,-73.96807193756104), "UN", "United Nations"));</p><p>  it

66、ems.add(new OverlayItem(getPoint(40.76866299974387,-73.98268461227417), "Lincoln Center", "Home of Jazz at Lincoln Center"));</p><p>  items.add (new OverlayItem (getP

67、oint(40.765136435316755,-73.97989511489868),</p><p>  "Carnegie Hall", "Where you go with practice, practice, practice"));</p><p>  items.add(new OverlayItem(getPoint(40.70

68、686417491799,-74.01572942733765),</p><p>  "The Downtown Club", "Original home of the Heisman Trophy"));</p><p>  populate();}</p><p><b>  @Override</b&

69、gt;</p><p>  protected OverlayItem createItem(int i) {</p><p>  return(items.get(i));}</p><p><b>  @Override</b></p><p>  public void draw(Canvas canvas, M

70、apView mapView, boolean shadow)</p><p>  super.draw(canvas, mapView, shadow);</p><p>  boundCenterBottom(marker);}</p><p><b>  @Override</b></p><p>  protec

71、ted boolean onTap(int i) {</p><p>  Toast.makeText(NooYawk.this,items.get(i).getSnippet(),</p><p>  Toast.LENGTH_SHORT) .show();</p><p>  return(true);}</p><p><b>

72、;  @Override</b></p><p>  public int size() {</p><p>  return(items.size());</p><p><b>  } }</b></p><p>  Handling Screen Taps</p><p>  A

73、n Overlay subclass can also implement onTap(), to be notified when the user taps the map, so the overlay can adjust what it draws. For example, in full-size Google Maps, clicking a pushpin pops up a bubble with informati

74、on about the business at that pin's location. With onTap(), you can do much the same in Android.</p><p>  The onTap() method for ItemizedOverlay receives the index of the OverlayItem that was clicked. It

75、 is up to you to do something worthwhile with this event.</p><p>  In the case of SitesOverlay, as shown in the preceding section, onTap() looks like this:</p><p><b>  @Override</b>&

76、lt;/p><p>  protected boolean onTap(int i) {</p><p>  Toast.makeText(NooYawk.this,items.get(i).getSnippet(),</p><p>  Toast.LENGTH_SHORT).show();</p><p>  return(true),}&l

77、t;/p><p>  Here, we just toss up a short Toast with the snippet from the OverlayItem, returning true to indicate we handled the tap.</p><p>  My, Myself, and MyLocationOverlay</p><p> 

78、 Android has a built-in overlay to handle two common scenarios:</p><p>  Showing where you are on the map, based on GPS or other location-providing logic</p><p>  Showing where you are pointed,

79、based on the built-in compass sensor, where available</p><p>  All you need to do is create a MyLocationOverlay instance, add it to your MapView's list of overlays, and enable and disable the desired fea

80、tures at appropriate times.</p><p>  The "at appropriate times" notion is for maximizing battery life. There is no sense in updating locations or directions when the activity is paused, so it is re

81、commended that you enable these features in onResume() and disable them in onPause().</p><p>  For example, NooYawk will display a compass rose using MyLocationOverlay. To do this, we first need to create th

82、e overlay and add it to the list of overlays:</p><p>  me=new MyLocationOverlay(this, map);</p><p>  map.getOverlays().add(me);</p><p>  Then we enable and disable the compass rose

83、as appropriate:</p><p><b>  @Override</b></p><p>  public void onResume() {</p><p>  super.onResume();</p><p>  me.enableCompass();}</p><p><

84、;b>  @Override</b></p><p>  public void onPause(){</p><p>  super.onPause();</p><p>  me.disableCompass();}</p><p>  The Key to It All</p><p>  If

85、 you actually download the source code for the book, compile the NooYawk project, install it in your emulator, and run it, you will probably see a screen with a grid and a couple of pushpins, but no actual maps.</p>

86、;<p>  That's because the API key in the source code is invalid for your development machine. Instead, you will need to generate your own API key(s) for use with your application.</p><p>  Full in

87、structions for generating API keys for development and production use can be found on the Android web site (http://code.google.com/android/add-ons/google -apis/mapkey.html). In the interest of brevity, let's focus on

88、 the narrow case of getting NooYawk running in your emulator. Doing this requires the following steps:</p><p>  1. Visit the API key signup page and review the terms of service.</p><p>  2. Rere

89、ad those terms of service and make really sure you want to agree to them.</p><p>  3. Find the MD5 digest of the certificate used for signing your debug-mode applications.</p><p>  4. On the API

90、 key signup page, paste in that MD5 signature and submit the form.</p><p>  5. On the resulting page, copy the API key and paste it as the value of apiKey in your MapView-using layout.</p><p>  

91、The trickiest part is finding the MD5 signature of the certificate used for signing your debug-mode applications. Actually, much of the complexity is merely in making sense of the concept.</p><p>  All Andro

92、id applications are signed using a digital signature generated from a certificate. You are automatically given a debug certificate when you set up the SDK, and there is a separate process for creating a self-signed certi

93、ficate for use in your production applications. This signature process involves the use of the Java keytool and jar signer utilities. For the purposes of getting your API key, you only need to worry about keytool.</p&

94、gt;<p>  To get your MD5 digest of your debug certificate, if you are on Mac OS X or Linux, use the following command:</p><p>  keytool -list -alias androiddebugkey –keystore ~/.android/debug.keystore

95、 -storepass</p><p>  android -keypass android</p><p>  On other development platforms, you will need to replace the value of the –keystore switch with the location for your platform and user acc

96、ount:</p><p>  On Windows XP, use C:\Documents and Settings\<user>\.android\debug. keystore.</p><p>  On Windows Vista/Windows 7, use C:\Users\<user>\.android\debug.keystore (where &

97、lt;user> is your account name).</p><p>  The second line of the output contains your MD5 digest, as a series of pairs of hex digits separated by colons.</p><p>  使用MapView和MapActivity顯示地圖<

98、/p><p>  谷歌地圖是谷歌服務(wù)中最受歡迎的后臺(tái)搜索服務(wù)之一,它可以讓你尋找到一切,比如,從紐約到舊金山 (僅 2,905 英里 !)方向的最近的比薩餅客廳,還提供街道視圖和衛(wèi)星圖像。</p><p>  大多數(shù)的安卓設(shè)備,都整合了谷歌地圖,這并不奇怪。而這樣做的目的是,在主要安卓的發(fā)射器中有一個(gè)能直接對(duì)用戶有用的顯示地圖的活動(dòng)。但作為一個(gè)開(kāi)發(fā)者,更多和你有關(guān)聯(lián)的是MapView類和Map

99、Activity類 ,這讓地圖融入你自己的應(yīng)用程序中。你不僅可以顯示地圖,控制縮放級(jí)別,讓人們平移,而且你也可以在安卓的基于位置的服務(wù)的(包括第32章)配合下,顯示設(shè)備在哪以及往哪里去。</p><p>  幸運(yùn)的是,把基本顯示功能納入你的安卓項(xiàng)目是相當(dāng)容易的。如果你想要獲得想象的功能,你也有權(quán)利去增加你想象的功能。</p><p><b>  條款無(wú)情</b><

100、;/p><p>  把谷歌地圖納入你自己的應(yīng)用程序上一般需要一個(gè)相當(dāng)長(zhǎng)的法律術(shù)語(yǔ)集。其中包括一些可能使你不愉快的條款。 如果你正在考慮使用谷歌地圖,請(qǐng)嚴(yán)密地審查這些條款來(lái)確定你的使用目的是否將無(wú)法運(yùn)行、是否與其它各個(gè)條款相抵觸。如果有任何潛在沖突的地方,強(qiáng)烈建議您尋求專業(yè)的法律條款。 此外,為了其他顯示選項(xiàng),根據(jù)其他地圖數(shù)據(jù)的來(lái)源,請(qǐng)睜大你的眼睛去選擇。如OpenStreetMap的(http://w

101、ww.openstreetmap.org/)。</p><p><b>  添加項(xiàng)問(wèn)題</b></p><p>  如同安卓l.5版本,谷歌地圖不是安卓SDK嚴(yán)格的一部分。相反,它是谷歌應(yīng)用程序界面的加載項(xiàng),是普通SDK的延伸部分。安卓附加系統(tǒng)為其他子系統(tǒng)提供了掛鉤。這可能是設(shè)備一部分而不是設(shè)備的全部。</p><p>  注: 谷歌地圖不是安

102、卓開(kāi)源項(xiàng)目的一部分,由于缺乏一些獲得許可的問(wèn)題有一些設(shè)備中并沒(méi)有谷歌地圖。例如,在寫(xiě)這篇文章的時(shí)候,人們所用的愛(ài)可視5的安卓系統(tǒng)并沒(méi)有谷歌地圖。</p><p>  大體上,谷歌地圖是加載項(xiàng)中的一個(gè)事實(shí),這并不影響你日常的開(kāi)發(fā)。然而,牢記以下:</p><p>  你將需要?jiǎng)?chuàng)建一個(gè)合適的項(xiàng)目指標(biāo),以確保谷歌地圖的應(yīng)用程序界面將是可用的。</p><p>  為了測(cè)試

103、你完成了的谷歌地圖,你還需要安卓運(yùn)行的虛擬環(huán)境(AVD) ,支持谷歌地圖API。</p><p><b>  基本要素 </b></p><p>  最簡(jiǎn)單的方式得到一張地圖,為您的應(yīng)用程序創(chuàng)建自己的類MapActivity 的子類。像 MapActivity ListActivity ,在列表視圖中占主導(dǎo)地位的活動(dòng)背后,它進(jìn)行了一些活動(dòng)的包裝,處理一些細(xì)微

104、之處設(shè)立一個(gè)由MapView主導(dǎo)的活動(dòng)。</p><p>  在你為MapActivity子類設(shè)計(jì)布局時(shí),你需要添加一個(gè)自定義的視圖,在寫(xiě)這篇文章時(shí)用的是,com.google.android.maps.MapView 。這是普通平湊的方式拼出桌面小插件類的名稱,包括完整的軟件包名稱與類名。這是必要的,因?yàn)镸apView類是不在com.google.android.widget包中。你可以給你想要的任何MapVi

105、ew類部件的android :id屬性賦值,再加上處理所有的布局細(xì)節(jié),與你的其它部件正確的呈現(xiàn)出來(lái)。</p><p>  然而,你需要有這兩個(gè)項(xiàng)目:</p><p>  android:apiKey ,在創(chuàng)建中,將需要一個(gè)Google地圖API密鑰。</p><p>  android:clickable="true",如果你想讓用戶能夠點(diǎn)擊,并

106、能夠平移地圖,設(shè)置clickable為“真”。</p><p>  例如,從地圖/ NooYawk的示例應(yīng)用程序,這里是主要的布局:</p><p>  <?xml version="l.0" encoding="utf-8f'?></p><p>  <RelativeLayout xmlns:android

107、="http://schemas.android.com/apk/res/android"</p><p>  android:layout_width="fill_parent"</p><p>  android:layout height="fill_parent"></p><p>  <

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 眾賞文庫(kù)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論