【android】【google map api v2】google 地圖 api v2


    今天看了《Google Android游戲開發》,看到第五章的時候,發現有實現google地圖的示例,於是想copy下看看。才發現,書上的示例是對於google map api version1版本,其api已於去年年度升級至version2了。

    先貼上官方的網站,大家可以細看:https://developers.google.com/maps/documentation/android/?hl=zh-CN

    v2與v1變化還蠻大的,現在大致發現有三大區別:

1、API Key的獲取方式不同

    1) v1是拿MD5編碼去登陸網站http://code.google.com/android/maps-api-signup.html 申請由Google簽署的key

    2) v2則是拿SHA-1 fingerprint登錄網站https://code.google.com/apis/console/申請API Key(具體怎么拿,會再后面給出)

2、顯示組件不同

  • Maps are now encapsulated in the MapFragment class, an extension of Android's Fragment class. Now you can add a map as a piece of a larger Activity. With a MapFragment object, you can show a map by itself on smaller screens, such as mobile phones, or as a part of a more complex UI on larger-screen devices, such as tablets.
  • Because maps are encapsulated in the MapFragment class, you can implement them by extending the Android standard Activity class, rather than extending the MapActivity used in version 1. 

    1)v1中是采用MapView+MapActivity來實現地圖的展示

    2)v2是采用MapFragment來實現

3、2D+3D視圖

    v2中增加了2D的不同方位展示,以及3D效果展示,更加地服務於大眾了,尤其是對於像我這樣東南西北分不清楚的人。

 

    現在貼上使用google map api v2的步驟:

1、安裝Google Play services SDK:

    如果是使用Eclipse開發工具,則可以點擊窗口—Android SDK Manage – Extras – Google Play service,install packages...安裝。如下圖:

pwii42pi.rga

2、獲取API Key

1)先獲取SHA-1 fingerprint:

注意,這里的數字證書是有兩種的,一種是debug,還有release。前者只能用於測試;后者才可以用於實際產品。

  • Debug certificate: The Android SDK tools generate this certificate automatically when you do a "debug" build from the command line, or when you build and run a project from Eclipse without exporting it as a released application. The certificate is only for use with an application that you're testing; you can't publish an app that's signed with a debug certificate. The debug certificate is described in more detail in the section Signing in Debug Mode in the Android Developer Documentation. You can generate an API key from this certificate, but only use the key for testing, never for production.
  • Release certificate: The Android SDK tools generate this certificate when you do a "release" build with either ant program or Eclipse. You can also generate this certificate using the keytool program. This certificate can be used with an app you release to the world. Once you have the correct certificate for your needs, you can display its SHA-1 fingerprint using the keytool program.

    現在就是用debug.keystore來獲取數字證書。在命令行中輸入命令:keytool -list -v -keystore "C:\Users\your_user_name\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android

    得到SHA1。如下圖:

2c4crgmu.fpq

     至於release版本的數字證書:

    1)使用keytool生成keystore.基本命令如下:keytool –genkey –alias demo.keystore –keyalg RSA –validity 40000 –keystore demo.keystore

             說明:-genkey:產生密鑰

                    -alias demo.keystore: 別名demo.keystore

                    -keyalg RSA: 使用RSA算法對簽名加密

                    -validity 40000: 有效期限40000天

                    -keystore demo.keystore

2letwt5w.wnt

    2)獲取SHA1的方式同debug是類似的,區別在於keystore密碼不是android,而是你在生成keystore時設置的密碼。

    3)畫外音:生成release版本的APK,是另外的一個議題了。

 

2)登錄網站獲取API Key

去這個網址:https://code.google.com/apis/console/ 用Gmail的賬戶登錄,如果是第一次的話,需要創建項目,默認情況會創建一個叫做API Project的項目。

點擊左邊的Services,會在中間看到很多的APIs和Services,找到Google Maps Android API v2,然后把它設置成on,需要接受一些服務條款。

在左邊的導航條中選擇API Access。在出來的頁面中選擇Create New Android Key...就可以生成key了:

20vnjrtn.hng

如上截圖給出的API Key就是我們所需要的。

注意:在申請API Key是會要求寫上包名,這里請填上你需要測試的工程的包名(例如,我的包名是com.amanda.activity)。

 

3、創建工程

這里就只貼上一些代碼了。如果需要查看具體是什么意思,請參考鏈接:https://developers.google.com/maps/documentation/android/start?hl=zh-CN

 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.amanda.activity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.amanda.activity.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAT6jZbQOgdX8JR6zvylp1o_SX7gxN--yM"/>
        
    </application>
    
    <permission 
        android:name="com.amanda.activity.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    
    <uses-permission android:name="com.amanda.activity.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.INTERNET"/> 
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
	<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 
	<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>  

	<uses-feature 
	    android:glEsVersion="0x00020000"
	    android:required="true"/>
</manifest>
 

布局文件activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/map" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  class="com.google.android.gms.maps.MapFragment"/>
 

主程序MainActivity.java:

package com.amanda.activity;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
}

4、導入Google Play services類庫:

1)在Eclipse里面選擇:File > Import > Android > Existing Android Code Into Workspace然后點擊Next.

  之后Browse..., 找到路徑下的<android-sdk-folder>/extras/google/google_play_services/libproject/google-play-services_lib, 然后選擇Finish。

2)添加對這個庫的引用:

    在自己的項目上右鍵,選Properties,左邊選Android,然后在下面的Library里面Add剛才的google-play-services_lib。

syw0k2v2.o1s

 

基本就是以上內容了。

運行時,出現了一些問題,現在也一並貼上。

問題一:沒有導入google-play-services_lib類

0hlvwza5.zqm

出現以上問題,請見上述步驟4.

 

問題二:提示沒有google play服務

郁悶的是,自己的手機是精簡版的,手機上沒有google play。在運行自己的工程時,提示“您的手機中沒有google play服務...”。

這個解決辦法是找了以下鏈接解決的,大家也可以看看:

http://bbs.lewaos.com/thread-65641-1-1.html

 

因為還沒有花多少時間研究該API,只是讓它跑起來了。后期再加上其他功能的。例如,3D顯示,經緯度定位等等。

 

 

參考文獻:

【1】Google Map API官網介紹:https://developers.google.com/maps/documentation/android/?hl=zh-CN

【2】http://www.cnblogs.com/mengdd/archive/2013/01/01/2841390.html

【3】http://bbs.lewaos.com/thread-65641-1-1.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM