STEP1:
首先需建立Google Map API 至官方網站申請
圖如下:
按下繼續後,需輸入套件名稱與SHA-1憑證指紋
STEP2:
SHA1如何取得?
有許多解決方法,不過根據網路查詢某討論文章認為以下方法最快:
- Open Android Studio
- Open Your Project
- Click on Gradle (From Right Side Panel, you will see Gradle Bar)
- Click on Refresh (Click on Refresh from Gradle Bar, you will see List Gradle scripts of your Project)
- Click on Your Project (Your Project Name form List (root))
- Click on Tasks
- Click on android
- Double Click on signingReport (You will get SHA1 and MD5 in Run Bar)
(觀察螢光筆處)
輸入完套件名稱與SHA-1憑證指紋後,按下建立,即完成API申請。
PS.執行主程式時,要把執行改為APP(綠色執行按鈕左側的下拉式選單),不然會一直跑SHA1
STEP3:
接著要編輯APP主程式MainActivity前要匯入google Services如何匯入google Services?
官方說明網站:Setting Up Google Play Services
需輸入螢光筆處那一行,以依靠該套件
實際上在裝按那裡匯入?如圖下:
因為我是API19 buildToolsVersion "22.0.1" 所以適用
compile 'com.google.android.gms:play-services:6.5.87'
STEP4:
Manifest.xml加入權限
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="你申請的API" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
----另外研究-----
如果去翻閱官方網站就能發現,竟然有兩種寫法了!
(1) android:name="com.google.android.maps.v2.API_KEY"
(2) android:name="com.google.android.geo.API_KEY"
都可以,v2有相容,不過最新文件是第2個
---------------------
以下提供檔案完整內容
manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="你的套件名稱!!!" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="你申請的API" />
</application>
</manifest>
MainActivity:
package com.example.user.exgooglemap;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleMap map =
((MapFragment) getFragmentManager().findFragmentById(R.id.map_fragment)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
map.getUiSettings().setZoomControlsEnabled(true);
LatLng nkut = new LatLng(23.97918, 120.69686);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(nkut,15.0f));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
layout_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<fragment
android:id="@+id/map_fragment"
class="com.google.android.gms.maps.MapFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
PS.
如果layout出現紅色底線
class="com.google.android.gms.maps.MapFragment"
錯誤表示沒有匯入google service!
如果MainActivity GoogleMap 出現紅色底線
錯誤表示沒有匯入google service!
-------------
執行主程式看效果!
模擬器一定要使用有google APIs的
完成。
下載
下載此範例可能遇到的問題
1.此範例為API19 需安裝API19
2.可能會要求安裝什麼,同意即可
3.同意安裝後會詢問是否update選擇update
4.build.gradle 跑許久,重開該檔案即可
5.每個人SHA1不同,一定要去設定KEY和SHA1
6.出現此錯誤正常,每人SDK放置位置不同,OK即可
留言列表