日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

如何在Android手机上进行Google Map的开发。

發布時間:2025/5/22 Android 69 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在Android手机上进行Google Map的开发。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.題記

提起谷歌Map相信大家都不會陌生,那進入我們今天的話題,如何在Android手機上進行Google Map的開發。

2.Map應用程序的開發

2.1 準備工作

2.1.1 申請Android Map API KEY

?? ? ? ? 步驟一: 找到你的debug.keystore文件,在Eclipse 首選項中可以看到該文件。如下圖:

?


???????????? 步驟二:取得debug.keystore的MD5值

?????????????在命令行下進入debug.keystore文件所在的路徑,執行命令:keytool -list -keystore debug.keystore,會提示輸入密碼,輸入默認密碼“android”,即可取得MD5值。

????????????? 步驟三:申請Android Map的API key。

????????????? 在瀏覽器重輸入網址:http://code.google.com/intl/zh-CN/android/maps-api-signup.html,登錄Google賬號,輸入步驟2中得到的MD5值,即可申請到API Key。記下API Key。

2.1.2 創建基于Google APIs的AVD

?????? 在Eclipse中打開AVD 界面,創建AVD,選擇Target為Google APIs的項,如下圖:


若在Target處無Google APIs選項,請自行添加maps.jar文件。

2.1.3?創建基于Google APIs的工程(略),即選擇Build Target為Google APIs。

2.2 Google Map API的使用

??其類均在com.google.android.maps包中,一下是該包中幾個重要的類:

MapActivity用于顯示Google Map的Activity類,該類為抽象類,開發時請繼承該類,并實現onCreate()方法。在該類中必須創建一個MapView實例。

MapView?用戶顯示地圖的View組件.

MapController?用于控制地圖的移動、縮放等

Overlay?這是一個可顯示于地圖上的可繪制的對象

GeoPoint?一個包含經緯度位置的對象

2.3 實例

2.3.1 創建工程,注意選擇Build Target為“Google APIs”

2.3.2 修改AndroidManifest.xml文件,增加訪問網絡的權限

<uses-permission android:name="android.permission.INTERNET" />

2.3.3 創建Map View,代碼如下:

Xml代碼??
  • <com.google.android.maps.MapView??
  • ????android:id="@+id/MapView01"??
  • ????android:layout_width="fill_parent"??
  • ????android:layout_height="fill_parent"??
  • ????android:apiKey="0u7spJisVnJZmy3X6nX1M01SirYWYgNm-EQZbhQ"/>??
  • ??? 其中APIKEY即為之前得到的APIkey。

    2.3.4 實現MapActivity,代碼和講解如下:

    Java代碼??
  • package?com.sulang.android.map;??
  • ??
  • import?java.util.List;??
  • import?android.graphics.Bitmap;??
  • import?android.graphics.BitmapFactory;??
  • import?android.graphics.Canvas;??
  • import?android.graphics.Paint;??
  • import?android.graphics.Point;??
  • import?android.os.Bundle;??
  • ??
  • import?com.google.android.maps.GeoPoint;??
  • import?com.google.android.maps.MapActivity;??
  • import?com.google.android.maps.MapController;??
  • import?com.google.android.maps.MapView;??
  • import?com.google.android.maps.Overlay;??
  • ??
  • ??
  • public?class?Activity01?extends?MapActivity??
  • {??
  • ????private?MapView??mMapView;??
  • ????private?MapController?mMapController;???
  • ????private?GeoPoint?mGeoPoint;??
  • ????/**?Called?when?the?activity?is?first?created.?*/??
  • ????@Override??
  • ????public?void?onCreate(Bundle?savedInstanceState)??
  • ????{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.main);??
  • ????????mMapView?=?(MapView)?findViewById(R.id.MapView01);??
  • ????????//設置為交通模式??
  • ????????//mMapView.setTraffic(true);??
  • ????????//設置為衛星模式??
  • ????????//mMapView.setSatellite(true);???
  • ????????//設置為街景模式??
  • ????????mMapView.setStreetView(false);??
  • ????????//取得MapController對象(控制MapView)??
  • ????????mMapController?=?mMapView.getController();???
  • ????????mMapView.setEnabled(true);??
  • ????????mMapView.setClickable(true);??
  • ????????//設置地圖支持縮放??
  • ????????mMapView.setBuiltInZoomControls(true);???
  • ????????//設置起點為成都??
  • ????????mGeoPoint?=?new?GeoPoint((int)?(30.659259?*?1000000),?(int)?(104.065762?*?1000000));??
  • ????????//定位到成都??
  • ????????mMapController.animateTo(mGeoPoint);???
  • ????????//設置倍數(1-21)??
  • ????????mMapController.setZoom(12);???
  • ????????//添加Overlay,用于顯示標注信息??
  • ????????MyLocationOverlay?myLocationOverlay?=?new?MyLocationOverlay();??
  • ????????List<Overlay>?list?=?mMapView.getOverlays();??
  • ????????list.add(myLocationOverlay);??
  • ????}??
  • ????protected?boolean?isRouteDisplayed()??
  • ????{??
  • ????????return?false;??
  • ????}??
  • ????class?MyLocationOverlay?extends?Overlay??
  • ????{??
  • ????????@Override??
  • ????????public?boolean?draw(Canvas?canvas,?MapView?mapView,?boolean?shadow,?long?when)??
  • ????????{??
  • ????????????super.draw(canvas,?mapView,?shadow);??
  • ????????????Paint?paint?=?new?Paint();??
  • ????????????Point?myScreenCoords?=?new?Point();??
  • ????????????//?將經緯度轉換成實際屏幕坐標??
  • ????????????mapView.getProjection().toPixels(mGeoPoint,?myScreenCoords);??
  • ????????????paint.setStrokeWidth(1);??
  • ????????????paint.setARGB(255,?255,?0,?0);??
  • ????????????paint.setStyle(Paint.Style.STROKE);??
  • ????????????Bitmap?bmp?=?BitmapFactory.decodeResource(getResources(),?R.drawable.home);??
  • ????????????canvas.drawBitmap(bmp,?myScreenCoords.x,?myScreenCoords.y,?paint);??
  • ????????????canvas.drawText("**廣場",?myScreenCoords.x,?myScreenCoords.y,?paint);??
  • ????????????return?true;??
  • ????????}??
  • ????}??
  • }??
  • 2.3.5 啟動模擬器,看效果圖:


    ?

    至此關于Google Map的開發已完成,下面是GPS的開發。


    3.GPS應用開發

    3.1相關API說明

    ?????關于地理定位系統的API全部位于android.location包內,其中包括以下幾個重要的功能類:

    ????? LocationManager:本類提供訪問定位服務的功能,也提供了獲取最佳定位提供者的功能。

    ????? LocationProvider:該類是定位提供者的抽象類。定位提供者具備周期性報告設備地理位置的功能

    ????? LocationListener:提供定位信息發生改變時的回調功能。必須事先在定位管理器中注冊監聽器對象。

    ????? Criteria:該類是的應用能夠通過在LocationProvider中設置的屬性來選擇合適的定位提供者.

    ????? Geocider:用于處理地理編碼和反向地理編碼的類。

    ??????要使用地理定位,首先需要取得LocationManager的實例:

    Java代碼??
  • locationManager?=?(LocationManager)?getSystemService(context);??
  • ????? 取得LocationManager對象之后,還需要注冊一個周期性的更新視圖:

    Java代碼??
  • locationManager.requestLocationUpdates(provider,?3000,?0,locationListener);??
  • ????? 其中第一個參數是設置服務提供者,第二個參數是周期。最后一個參數是用來監聽定位信息的改變的。

    3.2 具體實例

    3.2.1 在AndroidManifest.xml文件中添加權限,代碼如下:

    ?

    Xml代碼??
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <manifest?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????package="com.sulang.android.map"?android:versionCode="1"??
  • ????android:versionName="1.0">??
  • ????<uses-sdk?android:minSdkVersion="3"?/>??
  • ??
  • ????<application?android:icon="@drawable/icon"?android:label="@string/app_name">??
  • ????????<uses-library?android:name="com.google.android.maps"?/>??
  • ????????<activity?android:name=".Activity01"?android:label="@string/app_name">??
  • ????????????<intent-filter>??
  • ????????????????<action?android:name="android.intent.action.MAIN"?/>??
  • ????????????????<category?android:name="android.intent.category.LAUNCHER"?/>??
  • ????????????</intent-filter>??
  • ????????</activity>??
  • ????</application>??
  • ????<uses-permission?android:name="android.permission.INTERNET"?/>??
  • ????<uses-permission?android:name="android.permission.ACCESS_COARSE_LOCATION"?/>??
  • ????<uses-permission?android:name="android.permission.ACCESS_FINE_LOCATION"?/>??
  • </manifest>??
  • 3.2.2 給模擬器設置個默認的坐標值

    ?啟動Eclipse ,選擇Window ->Show View?打開?Emulator Control 界面即可進行設置。

    3.2.3 實現MapActivity

    ?具體代碼和講解如下:

    Java代碼??
  • package?com.sulang.android.map;??
  • ??
  • import?java.io.IOException;??
  • import?java.util.List;??
  • import?java.util.Locale;??
  • ??
  • import?android.content.Context;??
  • import?android.graphics.Bitmap;??
  • import?android.graphics.BitmapFactory;??
  • import?android.graphics.Canvas;??
  • import?android.graphics.Paint;??
  • import?android.graphics.Point;??
  • import?android.location.Address;??
  • import?android.location.Criteria;??
  • import?android.location.Geocoder;??
  • import?android.location.Location;??
  • import?android.location.LocationListener;??
  • import?android.location.LocationManager;??
  • import?android.os.Bundle;??
  • import?android.view.Menu;??
  • ??
  • import?com.google.android.maps.GeoPoint;??
  • import?com.google.android.maps.MapActivity;??
  • import?com.google.android.maps.MapController;??
  • import?com.google.android.maps.MapView;??
  • import?com.google.android.maps.Overlay;??
  • ??
  • /*?
  • ?*@author?七里香的悔恨,2011-3-16?
  • ?*MyMapActivity.java?
  • ?*Blog:[url]http://bigboy.iteye.com/[/url]?
  • ?*/??
  • public?class?MyMapActivity?extends?MapActivity?{??
  • ????public?MapController?mapController;??
  • ????public?MyLocationOverlay?myPosition;??
  • ????public?MapView?myMapView;??
  • ????private?static?final?int?ZOOM_IN?=?Menu.FIRST;??
  • ????private?static?final?int?ZOOM_OUT?=?Menu.FIRST?+?1;??
  • ??
  • ????@Override??
  • ????protected?boolean?isRouteDisplayed()?{??
  • ????????return?false;??
  • ????}??
  • ??
  • ????class?MyLocationOverlay?extends?Overlay?{??
  • ????????Location?mLocation;??
  • ??
  • ????????//?在更新坐標時,設置該坐標,一邊畫圖??
  • ????????public?void?setLocation(Location?location)?{??
  • ????????????mLocation?=?location;??
  • ????????}??
  • ??
  • ????????@Override??
  • ????????public?boolean?draw(Canvas?canvas,?MapView?mapView,?boolean?shadow,??
  • ????????????????long?when)?{??
  • ????????????super.draw(canvas,?mapView,?shadow);??
  • ????????????Paint?paint?=?new?Paint();??
  • ????????????Point?myScreenCoords?=?new?Point();??
  • ????????????//?將經緯度轉換成實際屏幕坐標??
  • ????????????GeoPoint?tmpGeoPoint?=?new?GeoPoint(??
  • ????????????????????(int)?(mLocation.getLatitude()?*?1E6),?(int)?(mLocation??
  • ????????????????????????????.getLongitude()?*?1E6));??
  • ????????????mapView.getProjection().toPixels(tmpGeoPoint,?myScreenCoords);??
  • ????????????paint.setStrokeWidth(1);??
  • ????????????paint.setARGB(255,?255,?0,?0);??
  • ????????????paint.setStyle(Paint.Style.STROKE);??
  • ????????????Bitmap?bmp?=?BitmapFactory.decodeResource(getResources(),??
  • ????????????????????R.drawable.home);??
  • ????????????canvas.drawBitmap(bmp,?myScreenCoords.x,?myScreenCoords.y,?paint);??
  • ????????????canvas.drawText("Here?am?I",?myScreenCoords.x,?myScreenCoords.y,??
  • ????????????????????paint);??
  • ????????????return?true;??
  • ??
  • ????????}??
  • ??
  • ????}??
  • ??
  • ????@Override??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.main);??
  • ????????//?取得LocationManager實例??
  • ????????LocationManager?locationManager;??
  • ????????String?context?=?Context.LOCATION_SERVICE;??
  • ????????locationManager?=?(LocationManager)?getSystemService(context);??
  • ????????myMapView?=?(MapView)?findViewById(R.id.MapView01);??
  • ????????//?取得MapController實例,控制地圖??
  • ????????mapController?=?myMapView.getController();??
  • ????????myMapView.setEnabled(true);??
  • ????????myMapView.setClickable(true);??
  • ????????//?設置顯示模式??
  • ????????myMapView.setSatellite(true);??
  • ????????myMapView.setStreetView(true);??
  • ????????//?設置縮放控制??
  • ????????myMapView.setBuiltInZoomControls(true);???
  • ????????myMapView.displayZoomControls(true);??
  • ????????//?設置使用MyLocationOverlay來繪圖??
  • ????????mapController.setZoom(17);??
  • ????????myPosition?=?new?MyLocationOverlay();??
  • ????????List<Overlay>?overlays?=?myMapView.getOverlays();??
  • ????????overlays.add(myPosition);??
  • ????????//?設置Criteria(服務商)的信息??
  • ????????Criteria?criteria?=?new?Criteria();??
  • ????????//?經度要求??
  • ????????criteria.setAccuracy(Criteria.ACCURACY_FINE);??
  • ????????criteria.setAltitudeRequired(false);??
  • ????????criteria.setBearingRequired(false);??
  • ????????criteria.setCostAllowed(false);??
  • ????????criteria.setPowerRequirement(Criteria.POWER_LOW);??
  • ????????//?取得效果最好的criteria??
  • ????????String?provider?=?locationManager.getBestProvider(criteria,?true);??
  • ????????//?得到坐標相關的信息??
  • ????????Location?location?=?locationManager.getLastKnownLocation(provider);??
  • ????????//?更新坐標??
  • ????????updateWithNewLocation(location);??
  • ????????//?注冊一個周期性的更新,3000ms更新一次??
  • ????????//?locationListener用來監聽定位信息的改變??
  • ????????locationManager.requestLocationUpdates(provider,?3000,?0,??
  • ????????????????locationListener);??
  • ??
  • ????}??
  • ??
  • ????private?void?updateWithNewLocation(Location?location)?{??
  • ????????String?latLongString;??
  • ??
  • ????????String?addressString?=?"沒有找到地址\n";??
  • ??
  • ????????if?(location?!=?null)?{??
  • ????????????//?為繪制標志的類設置坐標??
  • ????????????myPosition.setLocation(location);??
  • ????????????//?取得經度和緯度??
  • ????????????Double?geoLat?=?location.getLatitude()?*?1E6;??
  • ????????????Double?geoLng?=?location.getLongitude()?*?1E6;??
  • ????????????//?將其轉換為int型??
  • ????????????GeoPoint?point?=?new?GeoPoint(geoLat.intValue(),?geoLng.intValue());??
  • ????????????//?定位到指定坐標??
  • ????????????mapController.animateTo(point);??
  • ????????????double?lat?=?location.getLatitude();??
  • ????????????double?lng?=?location.getLongitude();??
  • ????????????latLongString?=?"經度:"?+?lat?+?"\n緯度:"?+?lng;??
  • ??
  • ????????????double?latitude?=?location.getLatitude();??
  • ????????????double?longitude?=?location.getLongitude();??
  • ????????????//?更具地理環境來確定編碼??
  • ????????????Geocoder?gc?=?new?Geocoder(this,?Locale.getDefault());??
  • ????????????try?{??
  • ????????????????//?取得地址相關的一些信息\經度、緯度??
  • ????????????????List<Address>?addresses?=?gc.getFromLocation(latitude,??
  • ????????????????????????longitude,?1);??
  • ????????????????StringBuilder?sb?=?new?StringBuilder();??
  • ????????????????if?(addresses.size()?>?0)?{??
  • ????????????????????Address?address?=?addresses.get(0);??
  • ????????????????????for?(int?i?=?0;?i?<?address.getMaxAddressLineIndex();?i++)??
  • ????????????????????????sb.append(address.getAddressLine(i)).append("\n");??
  • ??
  • ????????????????????sb.append(address.getLocality()).append("\n");??
  • ????????????????????sb.append(address.getPostalCode()).append("\n");??
  • ????????????????????sb.append(address.getCountryName());??
  • ????????????????????addressString?=?sb.toString();??
  • ????????????????}??
  • ????????????}?catch?(IOException?e)?{??
  • ????????????}??
  • ????????}?else?{??
  • ????????????latLongString?=?"沒有找到坐標.\n";??
  • ????????}??
  • ????????//?顯示??
  • ????????//?myLocationText.setText("你當前的坐標如下:\n"+latLongString+"\n"+addressString);??
  • ????}??
  • ??
  • ????private?final?LocationListener?locationListener?=?new?LocationListener()?{??
  • ????????//?當坐標改變時觸發此函數??
  • ????????public?void?onLocationChanged(Location?location)?{??
  • ????????????updateWithNewLocation(location);??
  • ????????}??
  • ??
  • ????????//?Provider被disable時觸發此函數,比如GPS被關閉??
  • ????????public?void?onProviderDisabled(String?provider)?{??
  • ????????????updateWithNewLocation(null);??
  • ????????}??
  • ??
  • ????????//?Provider被enable時觸發此函數,比如GPS被打開??
  • ????????public?void?onProviderEnabled(String?provider)?{??
  • ????????}??
  • ??
  • ????????//?Provider的轉態在可用、暫時不可用和無服務三個狀態直接切換時觸發此函數??
  • ????????public?void?onStatusChanged(String?provider,?int?status,?Bundle?extras)?{??
  • ????????}??
  • ????};??
  • }??
  • ??
    ?

    至此 GPS 應用開發完畢。

    源代碼

    • maps.rar?(47.1 KB)
    • 下載次數: 78
    • 查看圖片附件

    轉載于:https://www.cnblogs.com/tfy1332/p/3653417.html

    《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

    總結

    以上是生活随笔為你收集整理的如何在Android手机上进行Google Map的开发。的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。