2025-02-09

本教程將探討如何使用谷歌地圖API來顯示出信息點(POI),並使用手機提供的定位服務,來顯示當前位置可搜索到的POI位置。這種情況下,我們應使用商場作為POI。

 

我把教程分為瞭兩部分。 
第一部分(在本節中)將介紹包括:
1.MapView對象的使用 
2.使用MD5密文獲取一個谷歌地圖API密鑰 
3.實現一個地點監聽器,以獲得你的當前位置

第二部分將包括:
1.在應用程序中使用外部庫類 
2.創建顯示出你的當前位置和周邊的信息點

word-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px”>現在讓我們開始吧!

>步驟1:創建一個新的Android項目

啟動Eclipse IDE並創建一個新的Android項目。 
File > Create > New Android Project

使用以下設置創建新的項目:

項目名稱:MallFinder 
生成目標:Google APIs Platform–2.1  API Level 7 
應用程序名:Mall Finder 
包名稱:com.shawnbe.mallfinder 
創建Activity:MallFinderActivity 
最低SDK版本:7

Step1 
設置完成後,點擊完成。

>步驟2:在谷歌上註冊獲取API密鑰

因為MapView對象使用谷歌地圖,你需要在谷歌註冊一個API密鑰,並同意服務條款,然後才可以使用他們。註冊過程相當簡單。想要瞭解更多關於獲得地圖API密鑰信息,請查看此鏈接。

要對你的應用程序進行簽名就必須要用到的MD5密文生成的API秘鑰,聽起來很令人困惑,但它並不難,因為你會發現它像當我們之前的幾個步驟 。而不是上面的鏈接所述,使用命令行的方式得到你的MD5密文,我們將使用keytool的一個Eclipse插件 。想要瞭解更多有關keytool的插件信息,請查看此鏈接。

>步驟3:安裝keytool插件

在Eclipse IDE中,導航到 Help > Install New Software。

step3

當新的窗口彈出,在窗口的頂部點擊“Add”按鈕。

在“Name”欄輸入“keytool”(不含引號), 
在“Location”欄輸入“http://www.keytool.sourceforge.net/update”

step3screen2

點擊“OK”。

經過短暫的加載時間,窗口將出現一個標有“keytool”的復選框。選中該復選框並單擊“下一步”。同意協議的條款,然後單擊“下一步”完成。

註意:在安裝過程中,可能會提示您確定是否信任安全證書。如果你確定,就選中復選框並單擊“確定”繼續安裝。 
step3screen3

安裝完成後,將會提示重新啟動Eclipse。 

>步驟4:獲取MD5密文

Eclipse重新啟動之後,你應該看到一個新的菜單項“keytool”,它旁邊有一個小鑰匙圖標。 
step4 

我們現在要打開調試的keystore。 
註意:由於操作系統的位置可能會有所不同。各種操作系統的默認位置:

  • >Windows Vista : C:\Users\<user>\.android\debug.keystore
  • >Windows XP : C:\Documents and Settings\<user>\.android\debug.keystore
  • >OS X and Linux : ~/.android/debug.keystore 

    點擊keytool菜單項>打開密鑰庫。

    點擊位於“文件名”文本框右側的“瀏覽”按鈕,並找到keystore的位置(上述為默認位置),並選擇debug.keystore文件。

    單擊“打開”,選擇類型“android”(不帶引號),這是默認的調試密碼,然後單擊“Load”。 
    step4screen2

現在應該在屏幕底部的面板可見一個新的keytool的標簽(如果你沒有看到它,導航到“Window > Open Perspective > Java Browsing”)。 
step4screen3

點擊debug.keystore路徑左側的小箭頭將顯示androiddebugkey。

雙擊androiddubugkey並復制MD5密文。

打開Web瀏覽器,進入以下網址。   http://code.google.com/android/maps-api-signup.html

閱讀並同意條款,在文本框中輸入MD5密文,並點擊“Generate API Key”。請註意保存好你的API密鑰,因為使用MapView的時候需要它。 

>步驟5:在佈局中添加MapView

在這一步,我們將添加一個MapView到佈局文件。

打開位於MallFinder > res > layout> main.xml中的main.xml文件,並修改該文件如下所示,包括一個FrameLayout和MapView


01
<?xml version="1.0" encoding="utf-8"?>
02
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03
    android:layout_width="fill_parent"
04
    android:layout_height="fill_parent"
05
    android:orientation="vertical" >
06
    <FrameLayout
07
    android:layout_width="fill_parent"
08
    android:layout_height="fill_parent">
09
        <com.google.android.maps.MapView
10
        android:id="@+id/mapView"
11
        android:layout_width="fill_parent"
12
        android:layout_height="fill_parent"
13
        android:clickable="true"
14
        android:apiKey="PUT YOUR API KEY HERE"/>
15
    </FrameLayout>
16
</LinearLayout>
上述佈局在整個可用的屏幕空間創建一個MapView。在當前狀態下運行的應用程序,將導致在強制關閉。我們還需要完成多個步驟。
步驟6:設置權限,並導入所需的庫
由於我們的應用程序將會從Google Maps下載數據,同時需要從手機的GPS或其他定位服務訪問信息,所以我們需要在AndroidManifest文件中的聲明必要的權限來使用這些服務。
 
要做到這一點,打開位於MallFinder > AndroidManifest.xml中的AndroidManifest.xml文件。
 
在關閉應用程序標記(</application>)後面,和在閉幕清單標記(</manifest>)之前添加以下幾行。
1
<uses-feature android:name="android.hardware.location.gps"/>
2
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
3
<uses-permission android:name="android.permission.INTERNET"/>
在這個例子中,我不需要FINE_LOCATION,但我已經包含在此,我想你可能要調整你的代碼來測試各種提供商,如果你想獲得更準確的位置,你將需要FINE_LOCATION權限。通常任何生產中的應用程序都不應該聲明不需要的權限。如你要求更多的權限,這是一個非常糟糕的做法,使用戶警惕你的應用程序。如果你不打算使用更準確的供應商,你可以不聲明android.permission.ACCESS_COARSE_LOCATION。
 
為瞭在我們的MapView中使用谷歌地圖,我們需要在Manifest文件中聲明該庫,這樣我們要在關閉活動標記和關閉應用程序標記代碼之間加入下面的標簽:
 
1
<uses-library android:required="true" android:name="com.google.android.maps" />
我們可以把標題欄去掉,因為我覺得這是不必要的。它使用瞭太多我們有限的屏幕空間。在應用程序刪除標題欄添加下面代碼:
1
android:theme="@android:style/Theme.NoTitleBar"
現在我們完整的Manifest文件看起來像這樣:
01
<?xml version="1.0" encoding="utf-8"?>
02
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
03
    package="com.shawnbe.mallfinder"
04
    android:versionCode="1"
05
    android:versionName="1.0" >
06
    <uses-sdk android:minSdkVersion="7" />
07
    <application
08
        android:icon="@drawable/ic_launcher"
09
        android:label="@string/app_name"
10
        android:theme="@android:style/Theme.NoTitleBar">
11
        <activity
12
            android:label="@string/app_name"
13
            android:name=".MallFinderActivity" >
14
            <intent-filter >
15
                <action android:name="android.intent.action.MAIN" />
16
 
17
                <category android:name="android.intent.category.LAUNCHER" />
18
            </intent-filter>
19
        </activity>
20
        <uses-library android:required="true" android:name="com.google.android.maps" />
21
    </application>
22
    <uses-feature android:name="android.hardware.location.gps"/>
23
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
24
    <uses-permission android:name="android.permission.INTERNET"/>
25
</manifest>
步驟7:設置MapView
在我們運行MapView之前,我們還需要做一些MallFinderActivity類的調整。
打開的主要活動類(MallFinderActivity.java),這是位於:MallFinder > src > com.shawnbe.mallfinder > MallFinderActivity.java
 
在我們的應用程序中,我們需要將這個類繼承“MapActivity”,而不是“Activity”。
 
修改行:
1
public class MallFinderActivity extends Activity {
改成:
1
public class MallFinderActivity extends MapActivity {
繼承MapActivity類時,我們需要實現isRouteDisplayed()方法,所以我們需要添加下面的方法 :
1
@Override
2
protected boolean isRouteDisplayed() {
3
    // TODO Auto-generated method stub
4
    return false;
5
}
這時,我們可以啟動應用程序並顯示MapView,但我們的沒有設置選項,如縮放級別和可見圖層。我們開始在onCreate方法中聲明以下變量:
1
private MapController mapController;
2
private MapView mapView;
在onCreate方法中添加以下幾行代碼:
1
mapView = (MapView)findViewById(R.id.mapView);
2
mapView.setBuiltInZoomControls(true);
3
mapView.setSatellite(false);
4
mapView.setStreetView(true);
5
mapController = mapView.getController();
6
mapController.setZoom(13);
上面的代碼隻顯示街層並隱藏的衛星層。我個人喜好選擇它,因為我覺得在這種情況下更容易讀。正如你喜歡用佈爾值false和true來隨意調整一樣。此外,縮放級別設置為13,你也可以調整到1作為整個世界視圖來看和20最大變焦。
 
最後,我們可以繼續啟動應用程序,以確保一切運行像預期一樣。
有幾種方法來運行應用程序,首先:
 
在Package Explorer window中右鍵單擊項目> Run as > Android Application
 
或者

 

>從菜單上,單擊Run > Run 
step7

>然後選擇你的手機或模擬器。你應該可以看到一個地圖覆蓋整個屏幕,除瞭在屏幕上方的通知欄,而且可以滾動並使用縮放。 
step7screen2

>>步驟8:獲取你當前的位置

>要獲得你的當前位置,我們使用 locationManager類,它允許應用程序獲取設備的位置。此過程可能需要一些時間來定位當前位置,因此,建議使用你lastKnownLocation,直到你可以得到一個更準確的當前的位置。

>再聲明以下兩個變量在步驟7中

private LocationManager locationManager;
2
private GeoPoint currentLocation;
此外,將以下方法添加到MallFinderActivity.java類:
01
public void getLastLocation(){
02
    String provider = getBestProvider();
03
    currentLocation = locationManager.getLastKnownLocation(provider);
04
    if(currentLocation != null){
05
        setCurrentLocation(currentLocation);
06
    }
07
    else
08
    {
09
        Toast.makeText(this, "Location not yet acquired", Toast.LENGTH_LONG).show();
10
    }
11
}
12
 
13
public void animateToCurrentLocation(){
14
    if(currentPoint!=null){
15
        mapController.animateTo(currentPoint);
16
    }
17
}
18
 
19
public String getBestProvider(){
20
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
21
    Criteria criteria = new Criteria();
22
    criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
23
    criteria.setAccuracy(Criteria.NO_REQUIREMENT);
24
    String bestProvider = locationManager.getBestProvider(criteria, true);
25
    return bestProvider;
26
}
27
 
28
public void setCurrentLocation(Location location){
29
    int currLatitude = (int) (location.getLatitude()*1E6);
30
    int currLongitude = (int) (location.getLongitude()*1E6);
31
    currentLocation = new GeoPoint(currLatitude,currLongitude);
32
 
33
    currentLocation = new Location("");
34
    currentLocation.setLatitude(currentPoint.getLatitudeE6() / 1e6);
35
    currentLocation.setLongitude(currentPoint.getLongitudeE6() / 1e6);
36
}
在onCreate方法中添加以下兩行:
1
getLastLocation();
2
animateToCurrentLocation();
onCreate方法現在看起來:
01
public void onCreate(Bundle savedInstanceState) {
02
    super.onCreate(savedInstanceState);
03
    setContentView(R.layout.main);
04
    mapView = (MapView)findViewById(R.id.mapView);
05
    mapView.setBuiltInZoomControls(true);
06
    mapView.setSatellite(false);
07
    mapView.setStreetView(true);
08
    mapController = mapView.getController();
09
    mapController.setZoom(13);
10
    getLastLocation();
11
    animateToCurrentLocation();
12
}
getLastLocation方法創建的locationManager類的實例,這是基於最好的供應商使用定位服務的要求。在上面的代碼中,我們沒有指定任何條件的供應商,但是你可以根據你的精度和功耗要求的標準來調整使用criteria.setAccuracy()和criteria.setPowerRequirement()方法。上述步驟6中,我提到,你可能需要調整你的標準,以獲得更精確的的位置。如果你想做到這一點,你設置的精度標準ACCURACY_FINE的代碼是:
1
criteria.setAccuracy(Criteria.ACCURACY_FINE);
我沒有選擇最精確的供應商(GPS)的原因是,因為我在室內測試此代碼,並可能永遠不會得到我位置。如果你能用GPS定位到當前位置,你隨時可以改變準確性在外面測試它。
 
由於你的手機可能會使用不同的供應商指定的標準,這通常是GPS或網絡(也可以是別的)。如果設置為currentLocation,最後知道的位置至少要等到我們得到瞭更準確和更新的位置。我們滾動圍繞這些坐標為中心的地圖才能發現當前位置的經度和緯度。
 
現在我們得到最後一個已知的位置,我們使用位置監聽器來檢測我們的位置變化來更新應用程序。要做到這一點,我們需要我們的類(MallFinderActivity.java)實現locationListener。首先,我們修改行:
1
public class MallFinderActivity extends MapActivity {
改成:
1
public class MallFinderActivity extends MapActivity implements LocationListener{
實現地點監聽器,需要我們重寫一組方法,所以我們要添加以下標準方法如下(我指的這些標準方法,實際上並不做任何事情):
01
@Override
02
public void onLocationChanged(Location arg0) {
03
    // TODO Auto-generated method stub
04
}
05
 
06
@Override
07
public void onProviderDisabled(String arg0) {
08
    // TODO Auto-generated method stub
09
}
10
 
11
@Override
12
public void onProviderEnabled(String arg0) {
13
    // TODO Auto-generated method stub
14
}
15
 
16
@Override
17
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
18
    // TODO Auto-generated method stub
19
}
我們將使用onLocationChanged的方法檢測我們的位置更新,然後使用setcurrentLocation方法設置為我們當前的位置。
 
修改onlocationChanged的方法如下:
1
@Override
2
public void onLocationChanged(Location newLocation) {
3
    // TODO Auto-generated method stub
4
    setCurrentLocation(newLocation);
5
}
添加以下幾行代碼。這些方法會在每當應用程序啟動或恢復的時候,請求更新;當應用程序被暫停或關閉的同時,也將停止檢查更新。
01
@Override
02
protected void onResume() {
03
    super.onResume();
04
    locationManager.requestLocationUpdates(getBestProvider(), 1000, 1, this);
05
}
06
 
07
@Override
08
protected void onPause() {
09
    super.onPause();
10
    locationManager.removeUpdates(this);
11
}
如果此時運行的應用程序的MapView應圍繞你最後知道的位置,當你的手機能夠定位,它將會圍繞你的當前位置為中心更新。由於在步驟7設置定位的精確性,得到你的位置時間長度會有所不同。
在這一節中,我們已經學會如何使用的MapView,實現locationListener,並取得你的當前位置。在下一節中,我們將擴展包括使用外部的庫來處理的MapView上信息點的覆蓋和使用氣球彈出窗口顯示一些信息。

摘自 迷途ф書童的博客
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *