版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、<p> 畢業(yè)設計(論文)外文資料翻譯</p><p> 外文出處 Mark Murphy.Beginning </p><p> Android 2 Chapter 33 </p><p> Mapping with MapView and MapActivity</p><p> One
2、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 to San Francisco (only 2,905 miles!), along with
3、supplying street views and satellite imagery.</p><p> Most Android devices, not surprisingly, integrate Google Maps. For those that do, there is a mapping activity available to users directly from the main
4、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 maps, control the zoom level, and allow people t
5、o 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, integrating basic mapping features into your Android
6、 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> Integrating Google Maps into your own applic
7、ation 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 Google Maps, please review these terms closely to de
8、termine 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 conflict.</p><p> Also, keep your eyes pee
9、led for other mapping options, based on other sources of map data, such as OpenStreetMap (http://www.openstreetmap.org/).</p><p><b> Piling On</b></p><p> As of Android l.5, Google
10、 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 other subsystems that may be part of some devices
11、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 due to licensing issues. For example, at the time o
12、f 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 day-to-day development. However, bear in mind the
13、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 test your Google Maps integration, you will also nee
14、d 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 to create your own subclass of MapActivity. Like
15、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 dominated by a MapView.</p><p> In your l
16、ayout 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 spell out the names of widget classes, by includ
17、ing 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 whatever android:id attribute value you want, plus
18、 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, which in production will need to be a Google Maps AP
19、I 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 Maps/NooYawk sample application, here is the main
20、 layout:</p><p> <?xml version="l.0" encoding="utf-8"?></p><p> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"</p><
21、;p> android:layout_width="fill_parent"</p><p> android:layout_height="fill_parent"></p><p> <com.google.android.maps.MapView android:id="@+id/map"</p
22、><p> android:layout_width=" fill_parent"</p><p> android:layout_height="fill_parent"</p><p> android:apiKey="<YOUR_ API_KEY>"</p><p&g
23、t; android:clickable="true" /></p><p> </RelativeLayout></p><p> We'll cover that mysterious apiKey later in this chapter, in the "The Key to It All" section.
24、 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 latter for use with the MyLocationOverlay class, descr
25、ibed later in this chapter)</p><p> Inside your <application>, a <uses-library> element with</p><p> android:name ="com.google.android.maps", to indicate you are using on
26、e of the optional Android APIs</p><p> Here is the AndroidManifest.xml file for NooYawk:</p><p> <?xml version=”1.0” encoding=”utf-8”?></p><p> <manifest xmlns:android=”
27、http://schemas.android.com/apk/res/android”</p><p> package="com.commonsware.android.maps”></p><p> <uses-permission android:name=”android. permission.INTERNET” /></p><
28、;p> <uses-permission android:name=”android.permission.ACCESS_COARES_LOCATION” /></p><p> <application android:label="@string/app_name"</p><p> android:icon="@drawab
29、le/cw"></p><p> <uses-library android:name=”com.google.android.maps"/></p><p> <activity android:name=".NooYawk” android:label="@string/app_name"><
30、/p><p> <intent-filter></p><p> <action android:name="android.intent.action.MAIN" /></p><p> <category android:name="android.intent.category.LAUNCHER&q
31、uot; /></p><p> </intent-filter></p><p> </activity></p><p> </application></p><p> </manifest></p><p> That is pretty much
32、 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 would get a nice map of the world. Note, however, tha
33、t MapActivity is abstract. You need to implement isRouteDisplayed() to indicate if you are supplying some sort of driving directions.</p><p> In theory, users could pan around the map using the D-pad. Howev
34、er, 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 a few things, as described next.</p><
35、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 getMapController() method. Between the MapView and MapCon
36、troller, 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 likely want to use.</p><p><b> Z
37、oom</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 narrower in scope, such as a few city blocks.</p&g
38、t;<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 world view and 21 is the tightest zoom you can ge
39、t. 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 phone's display probably doesn't have 26
40、8,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 setBuiltInZoomControls (true);, and the user will be abl
41、e 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, you will need to control what the map is showing,
42、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 the MapController.</p><p> The setCe
43、nter() 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 as integers representing the actual latitude and l
44、ongitude 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 convert the GeoPoint into a map position. However, it
45、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 computer can display satellite imagery, so can Andr
46、oid maps.</p><p> MapView offers toggleSatellite(), which, as the name suggests, toggles on and off the satellite perspective on the area being viewed. You can have the user trigger these via an options men
47、u or, in the case of NooYawk, via key presses:</p><p><b> @Override</b></p><p> public boolean onKeyDown(int keyCode, KeyEvent event) {</p><p> if (keyCode == KeyEven
48、t.KEYCODE_S) {</p><p> map.setSatellite( ! map.isSatellite());</p><p> return(true);</p><p> }else if(keycode== KeyEvent.KEYCODE_Z) {</p><p> Map.dispalyZoomControl
49、s(true);</p><p> Return(true);</p><p><b> }</b></p><p> return(super.onKeyDown(keyCode, event));}</p><p> Layers upon Layers</p><p> If yo
50、u 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 location being searched. In map parlance (and, f
51、or 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 layer atop the map layer.</p><p> A
52、ndroid'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, NooYawk uses a layer to show where select building
53、s 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 subclass of Overlay. There is an ItemizedOverlay
54、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, just call getOverlays() on your MapView and add ()
55、 your Overlay instance to it, as we do here with a custom SitesOverlay:</p><p> marker.setBounds(0, 0, marker.getIntrinsicWidth(),marker.getIntrinsicHeight());</p><p> map.getOverlays().add(ne
56、w SitesOverlay(marker));</p><p> We will take a closer look at that marker in the next section.</p><p> Drawing the ItemizedOverlay</p><p> As the name suggests, ItemizedOverlay
57、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. Here are the minimum steps to make this work:<
58、;/p><p> 1. Override ItemizedOverlay<OverlayItem> as your own subclass (in this example, SitesOverlay).</p><p> 2. In the constructor, build your roster of OverlayItem instances, and call
59、 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><p> 4. Override createItem() to return Ove
60、rlayItem instances given an index.</p><p> 5. When you instantiate your ItemizedOverlay subclass, provide it with a Drawable that represents the default icon (e.g., a pushpin) to display for each item.&
61、lt;/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() to do a better job of handling the shadow for
62、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 can draw the shadow appropriately.</p>&l
63、t;p> For example, here is SitesOverlay:</p><p> private class SitesOverlay extends ItemizedOverlay<OverlayItem> {</p><p> private List<OverlayItem> items=new ArrayList<Overl
64、ayItem>();</p><p> private Drawable marker=null;</p><p> public SitesOverlay(Drawable marker){</p><p> super(marker);</p><p> this.marker=marker;</p><
65、p> items.add(new OverlayItem(getPoint(40.748963847316034,-73.96807193756104), "UN", "United Nations"));</p><p> items.add(new OverlayItem(getPoint(40.7686629997438
66、7,-73.98268461227417), "Lincoln Center", "Home of Jazz at Lincoln Center"));</p><p> items.add (new OverlayItem (getPoint(40.765136435316755,-73.97989511489868),</
67、p><p> "Carnegie Hall", "Where you go with practice, practice, practice"));</p><p> items.add(new OverlayItem(getPoint(40.70686417491799,-74.01572942733765),</p><
68、p> "The Downtown Club", "Original home of the Heisman Trophy"));</p><p> populate();}</p><p><b> @Override</b></p><p> protected OverlayItem
69、createItem(int i) {</p><p> return(items.get(i));}</p><p><b> @Override</b></p><p> public void draw(Canvas canvas, MapView mapView, boolean shadow)</p><p&
70、gt; super.draw(canvas, mapView, shadow);</p><p> boundCenterBottom(marker);}</p><p><b> @Override</b></p><p> protected boolean onTap(int i) {</p><p>
71、Toast.makeText(NooYawk.this,items.get(i).getSnippet(),</p><p> Toast.LENGTH_SHORT) .show();</p><p> return(true);}</p><p><b> @Override</b></p><p> publ
72、ic int size() {</p><p> return(items.size());</p><p><b> } }</b></p><p> Handling Screen Taps</p><p> An Overlay subclass can also implement onTap(), to
73、 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 information about the business at that pin's location.
74、 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 is up to you to do something worthwhile with thi
75、s event.</p><p> In the case of SitesOverlay, as shown in the preceding section, onTap() looks like this:</p><p><b> @Override</b></p><p> protected boolean onTap(int
76、 i) {</p><p> Toast.makeText(NooYawk.this,items.get(i).getSnippet(),</p><p> Toast.LENGTH_SHORT).show();</p><p> return(true),}</p><p> Here, we just toss up a shor
77、t Toast with the snippet from the OverlayItem, returning true to indicate we handled the tap.</p><p> My, Myself, and MyLocationOverlay</p><p> Android has a built-in overlay to handle two com
78、mon 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, based on the built-in compass sensor, where avail
79、able</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 features at appropriate times.</p><p>
80、 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 recommended that you enable these features in onRes
81、ume() 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 the overlay and add it to the list of overlays:<
82、/p><p> me=new MyLocationOverlay(this, map);</p><p> map.getOverlays().add(me);</p><p> Then we enable and disable the compass rose as appropriate:</p><p><b> @O
83、verride</b></p><p> public void onResume() {</p><p> super.onResume();</p><p> me.enableCompass();}</p><p><b> @Override</b></p><p>
84、 public void onPause(){</p><p> super.onPause();</p><p> me.disableCompass();}</p><p> The Key to It All</p><p> If you actually download the source code for the bo
85、ok, 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><p> That's because the API key in t
86、he 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 instructions for generating API keys for developmen
87、t 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 the narrow case of getting NooYawk running in yo
88、ur 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. Reread those terms of service and make really sure yo
89、u 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 key signup page, paste in that MD5 signature and
90、 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> The trickiest part is finding the MD5 signature o
91、f 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 Android applications are signed using a digital signat
92、ure 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 certificate for use in your production applications. T
93、his 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><p> To get your MD5 digest of your de
94、bug certificate, if you are on Mac OS X or Linux, use the following command:</p><p> keytool -list -alias androiddebugkey –keystore ~/.android/debug.keystore -storepass</p><p> android -keypas
95、s 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 account:</p><p> On Windows XP, use C:
96、\Documents and Settings\<user>\.android\debug. keystore.</p><p> On Windows Vista/Windows 7, use C:\Users\<user>\.android\debug.keystore (where <user> is your account name).</p><
97、;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顯示地圖</p><p> 谷歌地圖是谷歌服務中最受歡迎的后臺搜索服務之一,它可以讓你尋
98、找到一切,比如,從紐約到舊金山 (僅 2,905 英里 ?。┓较虻淖罱谋人_餅客廳,還提供街道視圖和衛(wèi)星圖像。</p><p> 大多數(shù)的安卓設備,都整合了谷歌地圖,這并不奇怪。而這樣做的目的是,在主要安卓的發(fā)射器中有一個能直接對用戶有用的顯示地圖的活動。但作為一個開發(fā)者,更多和你有關聯(lián)的是MapView類和MapActivity類 ,這讓地圖融入你自己的應用程序中。你不僅可以顯示地圖,控制縮放級別,讓人們平移
99、,而且你也可以在安卓的基于位置的服務的(包括第32章)配合下,顯示設備在哪以及往哪里去。</p><p> 幸運的是,把基本顯示功能納入你的安卓項目是相當容易的。如果你想要獲得想象的功能,你也有權利去增加你想象的功能。</p><p><b> 條款無情</b></p><p> 把谷歌地圖納入你自己的應用程序上一般需要一個相當長的法律術
100、語集。其中包括一些可能使你不愉快的條款。 如果你正在考慮使用谷歌地圖,請嚴密地審查這些條款來確定你的使用目的是否將無法運行、是否與其它各個條款相抵觸。如果有任何潛在沖突的地方,強烈建議您尋求專業(yè)的法律條款。 此外,為了其他顯示選項,根據(jù)其他地圖數(shù)據(jù)的來源,請睜大你的眼睛去選擇。如OpenStreetMap的(http://www.openstreetmap.org/)。</p><p><b
101、> 添加項問題</b></p><p> 如同安卓l.5版本,谷歌地圖不是安卓SDK嚴格的一部分。相反,它是谷歌應用程序界面的加載項,是普通SDK的延伸部分。安卓附加系統(tǒng)為其他子系統(tǒng)提供了掛鉤。這可能是設備一部分而不是設備的全部。</p><p> 注: 谷歌地圖不是安卓開源項目的一部分,由于缺乏一些獲得許可的問題有一些設備中并沒有谷歌地圖。例如,在寫這篇文章的時候
102、,人們所用的愛可視5的安卓系統(tǒng)并沒有谷歌地圖。</p><p> 大體上,谷歌地圖是加載項中的一個事實,這并不影響你日常的開發(fā)。然而,牢記以下:</p><p> 你將需要創(chuàng)建一個合適的項目指標,以確保谷歌地圖的應用程序界面將是可用的。</p><p> 為了測試你完成了的谷歌地圖,你還需要安卓運行的虛擬環(huán)境(AVD) ,支持谷歌地圖API。</p>
103、;<p><b> 基本要素 </b></p><p> 最簡單的方式得到一張地圖,為您的應用程序創(chuàng)建自己的類MapActivity 的子類。像 MapActivity ListActivity ,在列表視圖中占主導地位的活動背后,它進行了一些活動的包裝,處理一些細微之處設立一個由MapView主導的活動。</p><p> 在你為Map
104、Activity子類設計布局時,你需要添加一個自定義的視圖,在寫這篇文章時用的是,com.google.android.maps.MapView 。這是普通平湊的方式拼出桌面小插件類的名稱,包括完整的軟件包名稱與類名。這是必要的,因為MapView類是不在com.google.android.widget包中。你可以給你想要的任何MapView類部件的android :id屬性賦值,再加上處理所有的布局細節(jié),與你的其它部件正確的呈現(xiàn)出來
105、。</p><p> 然而,你需要有這兩個項目:</p><p> android:apiKey ,在創(chuàng)建中,將需要一個Google地圖API密鑰。</p><p> android:clickable="true",如果你想讓用戶能夠點擊,并能夠平移地圖,設置clickable為“真”。</p><p> 例如,
106、從地圖/ NooYawk的示例應用程序,這里是主要的布局:</p><p> <?xml version="l.0" encoding="utf-8f'?></p><p> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android
107、"</p><p> android:layout_width="fill_parent"</p><p> android:layout height="fill_parent"></p><p> <com.google.android.maps.MapView android:id="
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 眾賞文庫僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- android畢業(yè)設計外文資料翻譯--使用mapview和mapactivity顯示地圖
- 畢業(yè)設計外文資料翻譯.doc
- 畢業(yè)設計(論文)外文資料翻譯
- 畢業(yè)設計外文資料翻譯.doc
- 畢業(yè)設計外文資料翻譯.doc
- 畢業(yè)設計外文資料翻譯.doc
- android游戲畢業(yè)設計開題報告和外文翻譯
- 畢業(yè)設計(論文)外文資料翻譯.doc
- 畢業(yè)設計(論文)外文資料翻譯.doc
- 畢業(yè)設計(論文)外文資料翻譯.doc
- 畢業(yè)設計(論文)外文資料翻譯.doc
- 畢業(yè)設計(論文)外文資料翻譯.doc
- 畢業(yè)設計(論文)外文資料翻譯.doc
- 畢業(yè)設計(論文)外文資料翻譯.doc
- 畢業(yè)設計(論文)外文資料翻譯.doc
- 畢業(yè)設計(論文)外文資料翻譯.doc
- 畢業(yè)設計(論文)外文資料翻譯.doc
- 畢業(yè)設計(論文)外文資料翻譯.doc
- 畢業(yè)設計(論文)外文資料翻譯.doc
- 機械專業(yè)畢業(yè)設計--外文資料翻譯
評論
0/150
提交評論