2016-06-19

【Android Studio】Google 地圖 app 速成篇

參考資料 ----
新增地圖到 Android 應用程式的快速入門指南

Android Studio 版本:2.1.2 已經內建開發 地圖 app 的樣本


選擇 Google Maps Activity


專案建立後,會看到有個檔案 google_maps_api.xml,內有申請 Google Maps API 金鑰 的連結。複製該連結,開啟瀏覽器,將連結貼上,前往申請金鑰。









將金鑰貼入 google_maps_api.xmlgoogle_maps_key


開啟瀏覽器,啟用 Google 地圖,找一個我們想要地圖顯示的地點,以 高雄捷運美麗島站 為例。
找到捷運美麗島站後,在文字點滑鼠右鍵,會出現功能表,點選 "這是哪裡?" 在螢幕下方中央處會顯示美麗島站的座標:第一個數字是 "緯度",第二個數字是 "經度",把這兩個數字抄下來。


Android Studio 自動生成的檔案還有 activity_maps.xml,裡面只有一個 fragment
  1.  
  2. <fragment
  3. android:id="@+id/map"
  4. android:name="com.google.android.gms.maps.SupportMapFragment"
  5. xmlns:android="http://schemas.android.com/apk/res/android"
  6. xmlns:map="http://schemas.android.com/apk/res-auto"
  7. xmlns:tools="http://schemas.android.com/tools"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.example.mymap.MapsActivity"/>
  11.  


MapsActivity.java
  1.  
  2. package com.example.mymap;
  3.  
  4. import android.support.v4.app.FragmentActivity;
  5. import android.os.Bundle;
  6.  
  7. import com.google.android.gms.maps.CameraUpdateFactory;
  8. import com.google.android.gms.maps.GoogleMap;
  9. import com.google.android.gms.maps.OnMapReadyCallback;
  10. import com.google.android.gms.maps.SupportMapFragment;
  11. import com.google.android.gms.maps.model.LatLng;
  12. import com.google.android.gms.maps.model.MarkerOptions;
  13.  
  14. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback
  15. {
  16. private final static String TAG = "MapsActivity";
  17. private GoogleMap mMap;
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState)
  21. {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_maps);
  24. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  25. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  26. .findFragmentById(R.id.map);
  27. mapFragment.getMapAsync(this);
  28. }
  29.  
  30.  
  31. /**
  32. * Manipulates the map once available.
  33. * This callback is triggered when the map is ready to be used.
  34. * This is where we can add markers or lines, add listeners or move the camera. In this case,
  35. * we just add a marker near Sydney, Australia.
  36. * If Google Play services is not installed on the device, the user will be prompted to install
  37. * it inside the SupportMapFragment. This method will only be triggered once the user has
  38. * installed Google Play services and returned to the app.
  39. */
  40. @Override
  41. public void onMapReady(GoogleMap googleMap)
  42. {
  43. mMap = googleMap;
  44.  
  45. // Add a marker in Sydney and move the camera
  46. // LatLng sydney = new LatLng(-34, 151); 原本的座標值是雪梨某處
  47. // 替換上美麗島站的座標
  48. LatLng sydney = new LatLng(22.631392, 120.301803);
  49. mMap.addMarker(new MarkerOptions().position(sydney).title("捷運美麗島站"));
  50.  
  51. mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
  52. mMap.setMyLocationEnabled(true); // 右上角的定位功能;這行會出現紅色底線,不過仍可正常編譯執行
  53. mMap.getUiSettings().setZoomControlsEnabled(true); // 右下角的放大縮小功能
  54. mMap.getUiSettings().setCompassEnabled(true); // 左上角的指南針,要兩指旋轉才會出現
  55. mMap.getUiSettings().setMapToolbarEnabled(true); // 右下角的導覽及開啟 Google Map功能
  56.  
  57. Log.d(TAG, "最高放大層級:"+mMap.getMaxZoomLevel());
  58. Log.d(TAG, "最低放大層級:"+mMap.getMinZoomLevel());
  59.  
  60. mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
  61. mMap.animateCamera(CameraUpdateFactory.zoomTo(16)); // 放大地圖到 16 倍大
  62. }
  63. }
  64.  




相關筆記 ----
取得自己的 Android 裝置的定位
在模擬器取得定位值的操作