2016年8月27日 星期六

Android: Google Maps

since: 2016/08/27
update: 2016/10/13

reference:

A.  安裝 Google Play services SDK
      1. Android Studio > Tools > Android > SDK Manager

      2. SDK Tools > Google Play services (確認勾選) > OK

-----------------------------------------------------------------------------------------------

B. 新增 Maps 專案
    1. Start a new Android Studio project

    2. 設定好專案相關資料後 > Next

    3. Minimum SDK 選擇 API 18: Android 4.3 (Jelly Bean)
        > Next

    4. 選擇 Google Maps Activity > Next

    5. Customize the Activity: 除了 Title 外, 使用預設即可 > Finish

-----------------------------------------------------------------------------------------------

C.  申請 Google Maps API Key (開發憑證)
    1. 先查看自動開啟的檔案: google_maps_api.xml

<resources>
    <!--
    TODO: Before you run your application, you need a Google Maps API key.

    To get one, follow this link, follow the directions and press "Create" at the end:
 https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=FE:0E:E6:48:42:5D:E4:9B:39:B7:DF:5B:62:79:01:88:8C:C6:17:27%3Bpipi.myhome.mapviewer

    You can also add your credentials to an existing key, using this line:
    FE:0E:E6:48:42:5D:E4:9B:39:B7:DF:5B:62:79:01:88:8C:C6:17:27;pipi.myhome.mapviewer

    Alternatively, follow the directions here:
    https://developers.google.com/maps/documentation/android/start#get-key

    Once you have your key (it starts with "AIza"), replace the "google_maps_key"
    string in this file.
    -->

    <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">YOUR_KEY_HERE</string>
</resources>


    2. 將申請 API Key 的網址 copy & paste 到瀏覽器上
         > 繼續

    3. > 前往 "憑證"

    4. > 建立

    5. > 確定
        > 記下產生的金鑰: AIzaSyASXfTVIM2s2qhj0n2GAu6gemqDoqqO6EY

    6. 結果

-----------------------------------------------------------------------------------------------

D. 專案設定

     1. 將剛剛的 金鑰 copy & paste 到 google_maps_api.xml  裡的 YOUR_KEY_HERE
....
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyASXfTVIM2s2qhj0n2GAu6gemqDoqqO6EY</string>
....


     2. Tools > Android > Sync Project with Gradle Files

-----------------------------------------------------------------------------------------------

E. 查看專案的
AndroidManifest.xml 檔案

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pipi.myhome.mapviewer">

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality.
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/.
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

                                          
        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


-----------------------------------------------------------------------------------------------

F. 查看專案的 build.gradle(Module: app) 檔案
....
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'com.google.android.gms:play-services:9.4.0'
}


-----------------------------------------------------------------------------------------------

G. 查看專案的 activity_maps.xml 檔案
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="pipi.myhome.mapviewer.MapsActivity" />


-----------------------------------------------------------------------------------------------

H.
查看專案的 MapsActivity.java
檔案
package pipi.myhome.mapviewer;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}


-----------------------------------------------------------------------------------------------

I. 執行:

-----------------------------------------------------------------------------------------------

J. 讓 API 金鑰可以在不同 app (或不同 "開發" 電腦上) 使用
   1. 建立憑證指紋:
       $ cd
       $ keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey  -storepass android -keypass android

   2. 查詢憑證指紋:
       $ cd
      
$ cd .android
       $ keytool -list -v -keystore debug.keystore

輸入金鑰儲存庫密碼:  android

金鑰儲存庫類型: JKS
金鑰儲存庫提供者: SUN

您的金鑰儲存庫包含 1 項目

別名名稱: androiddebugkey
建立日期: 2016/7/9
項目類型: PrivateKeyEntry
憑證鏈長度: 1
憑證 [1]:
擁有者: C=US, O=Android, CN=Android Debug
發出者: C=US, O=Android, CN=Android Debug
序號: 1
有效期自: Sat Jul 09 18:26:27 CST 2016 到: Mon Jul 02 18:26:27 CST 2046
憑證指紋:
     MD5:  84:AB:96:D7:79:CE:90:D4:39:79:60:1A:CE:13:6B:1A
     SHA1: FE:0E:E6:48:42:5D:E4:9B:39:B7:DF:5B:62:79:01:88:8C:C6:17:27
     SHA256: 89:77:ED:02:9B:37:12:58:86:75:38:73:27:F0:F6:65:84:6F:D1:2F:3E:D2:75:03:D7:FB:D0:9D:CC:C5:59:E2
     簽章演算法名稱: SHA1withRSA
     版本: 1


   4. 到 Google API 的憑證網址:
       https://console.developers.google.com/apis/credentials/

   5. 新增套件名稱指紋

-----------------------------------------------------------------------------------------------

K. Add permission check:


-----------------------------------------------------------------------------------------------

L. 產生給 app 上架使用的 Google Map API 金鑰



-----------------------------------------------------------------------------------------------

M. Google Play change to Android ACCESS_FINE_LOCATION permissions

Action required: If your app requires GPS hardware to operate properly, you will need to explicitly add the "android.hardware.location.gps
" uses-feature to your manifest.

What’s changing
We’re making a change on October 15th, 2016 that will affect apps targeting API version 21 (Android 5.0, Lollipop) or higher that use ACCESS_FINE_LOCATION but don't explicitly have the "android.hardware.location.gps" uses-feature. Going forward, these apps will be available to install on devices that don't have GPS hardware. In most cases this won't be an issue since Wi-Fi and Cell-ID based location provides high enough fidelity for the typical operation of these apps. However, any apps that require GPS hardware, such as GPS navigators, should explicitly add the "android.hardware.location.gps" uses-feature to their manifest.
If your app requires GPS to function properly and you do not include android.hardware.location.gps in your manifest declaration, your users may have a poor app experience.
Also, if you’re using the fused location provider and wish to receive the most accurate location samples from GPS (i.e. with PRIORITY_HIGH_ACCURACY), you must include the "android.hardware.location.gps" feature in your app’s manifest to ensure that Google Play only distributes your app to devices with GPS sensors.

-----------------------------------------------------------------------------------------------

N. Problem with GPS -- Intermittent operation

Nextbit Community - Problem with GPS -- Intermittent operation - Nextbit Community

Changing the type of data network. Allowing LTE or 3G connections brings up the problem,
forcing a 2G connection "fixes" the problem, i.e. GPS work ok.


沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。