先貼下Google官方的地圖demo地址:https://github.com/googlemaps/android-samples
那么接下來第一步,申請Google的API key。
使用谷歌賬號登錄谷歌地圖開發者平台(登錄網站:https://cloud.google.com/maps-platform/?hl=zh-CN),點擊控制台,如圖
點擊控制進入控制台進行如下操作,生成谷歌地圖的appkey:
如果你沒有項目時,需要先點擊“創建”,新建你的項目后才能使用創建憑據。
點擊API秘鑰,生成秘鑰,在生成的秘鑰界面上我們點擊秘鑰限制,設置成android ,然后根據他提示的指令在cmd中獲取SHA-1值填入,然后點擊保存即可。(可不要真的輸入"mystore.keystore"喔,根據你自己的需求輸入"debug.keystore",或"release.keystore")
注意:Google說可能最長可能要5分鍾才生效噠。
還有一點是要保證你的API已開啟,未啟動用時,請開啟,如圖示:
然后接下來在項目的modle的build.gradle中添加依賴:
implementation 'com.google.android.gms:play-services-maps:12.0.1' // 地圖操作工具類,添加標記等 implementation 'com.google.maps.android:android-maps-utils:0.5+'
在清單文件寫上你的API KEY:
<meta-data android:name="com.google.android.geo.API_KEY" android:value="google給你的 API KEY" />
now,開始地圖展示。
分為兩種加載方式,一種是在activity中動態或者是靜態加載我們的谷歌地圖的fragment,進行顯示:
布局:
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" class="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" />
代碼中:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { double lat = 40.73; double lng = -73.99; LatLng appointLoc = new LatLng(lat, lng); // 移動地圖到指定經度的位置 googleMap.moveCamera(CameraUpdateFactory.newLatLng(appointLoc)); //添加標記到指定經緯度 googleMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title("Marker") .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher))); } }
另一種即直接使用mapView顯示谷歌地圖。
布局:
<com.google.android.gms.maps.MapView android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
代碼中:
MapView mMap = (MapView) mView.findViewById(R.id.mapview);
mMap.onCreate(savedInstanceState); mMap.onResume(); try { MapsInitializer.initialize(this); } catch (Exception e) { e.printStackTrace(); }
int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); if (ConnectionResult.SUCCESS != errorCode) {
GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show(); } else { mMap.getMapAsync(this); }
@Override
public void onMapReady(GoogleMap googleMap) { double lat = 40.73; double lng = -73.99; LatLng appointLoc = new LatLng(lat, lng); // 移動地圖到指定經度的位置 googleMap.moveCamera(CameraUpdateFactory.newLatLng(appointLoc)); //添加標記到指定經緯度 googleMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title("Marker") .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher))); }
OK,運行項目,這里就應該顯示一張定位在紐約的地圖了。
如果只顯示了一行提示說設備不支持話,那么說明你需要在手機安裝Google play服務,手機上的應用市場一般是不能直接安裝的,可以先安裝一個谷歌下載器,然后通過這個apk安裝;
如果顯示一片空白(應該只顯示了Google地圖的logo),那么檢查你的網絡是否翻牆成功,確認API開啟,確認API key無誤。