android google map api 入門 一
1 . google map api 簡介
Google Maps Android API 向您的應用新增基於 Google Maps 資料的地圖。 該 API 會自動處理對 Google 地圖伺服器的訪問、資料下載、地圖顯示以及對地圖手勢的響應。您還可以利用 API 呼叫向基礎地圖新增標記、多邊形和疊層,以及更改特定地圖區域的使用者檢視。這些物件可為地圖位置提供附加資訊,實現使用者與地圖的互動
2.獲取google map api 祕鑰
2.1 註冊獲取api key
登陸網站 https://developers.google.com/maps/android/,單機右上角 get a key
新建一個project,continue
下面的是第二次建立key才會有的介面
第一次建立的時候介面
name: 隨便取一個名字
package name:你專案的包名,也就是manifest中的名字
SHA: cd ./.android 裡面應該有一個debug.keystore
在終端執行
keytool -list -v -keystore debug.keystore 密碼 android 裡面 將 SHA的內容複製填入到4的位置continue
如圖便是你這個包下的key了
2.2 開啟api功能
google map api 中的key和高德,百度的不同,獲得了key,但是這個key我們什麼功能也沒有給他開啟,其實這個key暫時是不能用的,所以,我們需要開啟他們的功能
選擇DashBoard,單擊 ENABLEAPI
按照需求選擇相應的要開啟的功能
單擊上面的ENABLE
只有這樣,key才生效
3. 建立一個基本的地圖
我們如果選擇如下模式,android studio會自動幫我們構建一些應用
具體程式碼如下:
build.gradle加入
compile 'com.android.support:appcompat-v7:25.0.0' compile 'com.google.android.gms:play-services-maps:9.6.1'
Manifest:(注意,裡面的key為上面你所獲得的key,這個包名com.example.tate.firstmap 是申請key的時候填寫的包名
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.tate.firstmap" > <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" > <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>
MapsActivity:
類中方法介紹:此類繼承了FragmentActivity 實現了OnMapReadyCallback介面
通過 SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map); 獲取了地圖物件,執行
mapFragment.getMapAsync(this);然後會執行onMapRead方法,其中加了一個marker,移動了攝像頭的位置,這張地圖就出來了
package com.example.tate.firstmap;
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); } @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)); } }Activity_maps.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/map" tools:context="com.example.tate.firstmap.MapsActivity" android:name="com.google.android.gms.maps.SupportMapFragment"/>
執行後效果(google map需要手機支援google map服務,需要連上代理):