android谷歌地圖開發(fā)_第1頁
android谷歌地圖開發(fā)_第2頁
android谷歌地圖開發(fā)_第3頁
android谷歌地圖開發(fā)_第4頁
android谷歌地圖開發(fā)_第5頁
已閱讀5頁,還剩15頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

3.1.2程序功能的概述量等信息。界面正中央的小點則指明了用戶當前所在的位置。MenuMenuAndroid如圖3-2所示,顯示了軟件的運行流程。開始顯示地圖圖層否用戶位置有無改變?開始顯示地圖圖層否用戶位置有無改變?是采集更新的位置信息定位用戶位置偵聽用戶點擊事件判斷命令類型顯示分類圖層退出圖層清除結束功能模塊實現的基本原理GPSAndroidandroid.locationGPSLocationManagerLocationListenerLocationLocationProvider類LocationManagerGPSAPI確定位置和方位。GPS衛(wèi)星數據的獲取可通過如下代碼實現:mLocationManager=(LocationManager)getSystemService(LOCATION_SERVICE);mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mLocationListener);publicLocationListenermLocationListener=newLocationListener(){publicvoidonlocationChanged(Locationlocation){intlat,lon;lat=location.getLatitude();lon=location.getLongitude();}publicvoidonProviderDisabled(Stringprovider){}publicvoidonProviderEnabled(Stringprovider){}publicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras){}}代碼說明如下:調用getSystemService(Context.LOCATION_SERVICE)LocationManagerrequestLocationUpdates(String,long,float,LocatonListener)方GPS1234LocationListenerGPSGPSLocationListenerLocation-ManageronLocationChanged(Locationlocationlocation(getLatitude(getLongitude(getAccuracy()等的GPSonProviderDisabled(viderEnabled()在用戶啟用具有定位功能的硬件時被調用;onStatusChang-ed()在提供定位功能的硬件的狀態(tài)改變時被調用,如從不可獲取位置信息狀態(tài)到可以獲取位置信息的狀態(tài),反之亦然。GPSAndroidManifest.xml戶許可,實現代碼如下:<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>Google獲取Google地圖服務首先要向Google申請一組經過驗證的“地圖密鑰”(MapAPIKeywwGoogleAndroidGoogleMapsAPIKeykeystore(MD5)碼keytool.exeMD5C:\ProgramFiles\Java\jre1.5.0_06\bin目錄下執(zhí)行命令:>keytool–list–keystore”C:\Documentsandsettings\adminstrator\.android\debug.keystore”,如圖3-3,可得到認證指紋(MD5)碼。3-3取得認證指紋(MD5)碼GoogleMapsAPIKey進入“AndroidMapsAPIKeySignup/intl/ja/android/maps-api-signup.htmlMycertificate’sMD5fingerprint:”字段輸入獲取到的認證指紋(MD5)單擊“GenerateAPI3-4MapsAPIKey。圖3-4MapAPIKey在/res/layout/main.xml文件內建立<com.google.android.maps.MapVi-ew>標簽,并設置MapsAPIKey,如圖3-5所示,圖3-5建立<com.google.android.maps.MapView>標簽GoogleidManifest.xml<user-libr-ary>標簽<uses-permission>標簽<uses-libraryandroid:name="com.google.android.maps"/><uses-permissionandroid:name="android.permission.INTERNET"/>完成上述準備工作后,就可以從Google地圖服務系統(tǒng)獲得地圖了。地圖疊層技術的基本原理鼠標點擊事件等。Google地圖上可以加入多個覆蓋層,所有覆蓋層均都在地圖圖層之上,每個覆蓋層均可以對用戶的點擊事件做出響應。要實現地圖上覆蓋對象,首先要實現一個ItemizedOverlay類,它可以管理一套覆蓋項目功能。以下是創(chuàng)建ItemizedOverlay類的基本代碼:publicclassMyItemizedOverlayextendsItemizedOverlay{privateArrayList<OverlayItem>mOverlays=newArrayList<OverlayItem>();publicMyItemizedOverlay(DrawabledefaultMarker){super(boundCenterBottom (defaultMarker));//TODOAuto-generatedconstructorstub}publicvoidaddOverlay(OverlayItemoverlay){mOverlays.add(overlay);populate();}@OverrideprotectedOverlayItemcreateItem(inti){returnmOverlays.get(i);}@Overridepublicintsize(){returnmOverlays.size();}}代碼說明如下:OverlayItemArrayList置所有要貼在地圖上的標記對象。DrawableboundCenterBottom()方法來處理。addOverlayOverlayItemArratListOverlayItempopulate()方法,它會OverlayItemOverlayItempopulate(creatItemArrayListsize(size()方法的返回時加入如下代碼:returnmOverlays.size();以上為實現ItemizedOverlay類的基本程序。實現了一個ItemizedOverlay類后,就為后續(xù)的地圖貼圖工作打下了基礎。軟件的詳細分析與設計地圖圖層的顯示3.2.2MapsAPIKeyGoogleGooglemapView=(MapView)findViewById(R.id.mv2);mapView.setSatellite(false);mapView.setTraffic(false);mapView.setBuiltInZoomControls(true);intmaxZoom=mapView.getMaxZoomLevel();intinitZoom=(int)(0.90*(double)maxZoom);mapControl=mapView.getController();mapControl.setZoom(initZoom);GeoPointnewPoint=newGeoPoint((int)(lat*1e6),(int)(lon*1e6));mapControl.animateTo(newPoint);說明如下:MapView模式或交通模式。本例通過mapView.setSatellite(false);mapView.setTr-affic(false);兩條語句將所顯示的圖層設置為地圖模式。MapController則是MapViewMapView的顯示中心和縮放級別等功能。程序中的縮放級別為最大級別的0.9倍。mapControl.animateTo(newPoint)為中心顯示。運行結果如圖4-1所示:圖4-1地圖圖層的顯示衛(wèi)星數據的獲取與顯示獲取衛(wèi)星數據3.2.1當用戶的位置改變時,函數onLocationChanged(Locationlocation)會調用centerOnLocation()函數,獲取經度、緯度等衛(wèi)星信息。centerOnlocation()函數代碼如下:privatevoidcenterOnLocation(){loc=locman.getLastKnownLocation(provider);if(loc!=null){lat=loc.getLatitude()-0.002;lon=loc.getLongitude()+0.003;GeoPointnewPoint=newGeoPoint((int)(lat*1e6),(int)(lon*1e6));getSatelliteData();}if(displayOverlay!=null){displayOverlay.putSatStuff(lat,lon,satAccuracy,bearing,altitude,speed,currentProvider,numberSats);}}publicvoidgetSatelliteData(){if(loc!=null){locBundle=loc.getExtras();if(locBundle!=null){numberSats=locBundle.getInt("satellites",-1);}satAccuracy=loc.getAccuracy();bearing=loc.getBearing();altitude=loc.getAltitude();speed=loc.getSpeed();}}說明如下:getLastKnowLocation()方法可以獲取當前的位置信息,在其返回的loc對象行修正以消除這一偏差。mapControl.animateTo(newPoint)地圖圖層。調用getSatelliteData(拔高度等信息。displayOverlay.java程序的putSatStuff(據,以便將其顯示在用戶的手機屏幕上。衛(wèi)星數據的顯示displayOverlay.javaCanvas(畫布)類來顯示當前位置信息。Canvas類主要實現屏幕的繪制過程,其中包含了很如前一小節(jié)所述,函數putSatStuff()用來接收得到的衛(wèi)星數據,而顯示這些數據則通過如下代碼實現:publicvoiddraw(Canvascanvas,MapViewmapview,booleanshadow){super.draw(canvas,mapview,shadow);if(showData){paint=newPaint();paint.setAntiAlias(true);paint.setARGB(80,255,255,255);canvas.drawRect(0,0,350,33,paint);paint.setTextSize(11);paint.setARGB(180,0,0,0);canvas.drawText("Lat="+lat+"Long="+lon+"Alt="+(int)altitude+"m",8,14,paint);canvas.drawText("Sat="+numberSats+"Accur="+(int)satAccuracy+"m"+"speed="+(int)speed+"m/sbearing="+(int)bearing+"deg",8,27,paint);}}代碼說明如下:paint=newPaint()Paintpaint.setAntiAlias()函數設置了Paintpaint.setARGB()函數設置了Paint對象的顏色;paint.setTextSize()函數設置字體大小。canvas.drawRect()函數使得繪制區(qū)域為一矩形區(qū)域,并規(guī)定其范圍(左上頂點與右下頂點的坐標運行結果如圖4-2所示,屏幕最上方顯示了當前得到的衛(wèi)星信息:圖4-2衛(wèi)星數據的顯示地圖疊層技術的實現SQLiteDatabasesAndroid操作系統(tǒng)采用標準SQLite數據庫,提供管理數據庫相關的API。SQLitePostgreSQLMySQLRDBMSSQLiteSQLiteOpenHelper時需要建立三個回調(CallBack)onCreate(SQLiteData-baseonUpgrade(SQLiteDatabase,int,intonOpen(SQLiteD-atabase)。SQLiteDataBaseInsert(Update刪除(Delete、檢索(QuerySQLSQLiteCursor(Query)SQLiteContentValues首先要定義一個接口MapSchema,包括數據庫的表名稱和字段名稱,publicinterfaceMapSchema{StringTABLE_NAME="MapInfo";StringID="_id";StringTITLE="Title";StringNAME="name";StringLONGITUDE="longitude";StringLATITUDE="latitude";StringDETAILINFO1="detailinfo1";StringDETAILINFO2="detailinfo2";}接著通過SQLiteOpenHelper類來建立數據庫MapInfoDB,并使用CallBack方法onCreate()函數來建立表MapInfo,publicstaticclassDBConnectionextendsSQLiteOpenHelper{privatestaticfinalStringDATABASE_NAME="MapInfoDB";privatestaticfinalintDATABASE_VERSION=1;privateDBConnection(Contextctx){super(ctx,DATABASE_NAME,null,DATABASE_VERSION);}publicvoidonCreate(SQLiteDatabasedb){Stringsql="CREATETABLE"+MapSchema.TABLE_NAME+"("+MapSchema.ID+"INTEGERprimarykeyautoincrement,"+MapSchema.TITLE+"textnotnull,"+MapSchema.NAME+"textnotnull,"+MapSchema.LONGITUDE+"textnotnull,"+MapSchema.LATITUDE+"textnotnull,"+MapSchema.DETAILINFO1+"textnotnull,"+MapSchema.DETAILINFO2+"textnotnull"+");";db.execSQL(sql);}publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){//TODOAuto-generatedmethodstub}}建立好數據庫和表后,可通過定義SQLiteDataBasedb,SQLiteDataBasedb,dbhelper=newDBConnection(this);SQLiteDatabasedb=helper.getWritableDatabase();插入一行數據到數據庫中,db.insert(MapSchema.TABLE_NAME,null,values);從數據庫中檢索一行數據,Cursorc=db.query(MapSchema.TABLE_NAME,FROM,null,null,null,null,null);將分類圖層覆蓋于地圖圖層上3.2.3MyItemizedOverlay.javaItemizedOverlay首先實例化一個List<Overlay>類對象mapOverlays作為地圖圖層,并從“/res/drawable”文件里取得圖標knifefork_small.PNG,mapOverlays=mapView.getOverlays();drawable=this.getResources().getDrawable(R.drawable.knifefork_small);其次,實例化一個MyItemizedOverlay類對象itemizedOverlay來放置標記,itemizedOverlay=newMyItemizedOverlay(drawable,this,title);OverlayItemoverlayitemitemizedOverlayOverlayItemoverlayitem=newOverlayItem(newGeoPoint(Latitude,Longitude),name,text);itemizedOverlay.addOverlay(overlayitem);最后,將貼圖覆蓋于地圖圖層上,mapOverlays.add(itemizedOverlay);運行結果如圖4-3所示,圖4-3分類圖層的實現處理用戶點擊事件并顯示地圖信息MyItemizedOverlay.javaonTap(擊事件的處理并顯示地圖信息。代碼如下:booleanprotected

溫馨提示

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

評論

0/150

提交評論