TShopping

 找回密碼
 註冊
搜索
查看: 1927|回復: 0

[教學] [ch11_2_地圖種類與UI設定]

[複製鏈接]
發表於 2013-7-9 11:58:49 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
1.jpg


1.設定Manifest檔案


  1. <!-- 允許存取Google Maps伺服器 -->
  2. <permission
  3. android:name="com.derek.ch11_2_mapui.permission.MAPS_RECEIVE"
  4. android:protectionLevel="signature" />
  5. <uses-permission android:name="com.derek.ch11_2_mapui.MAPS_RECEIVE" />
  6. <!-- 允許下載地圖資訊 -->
  7. <uses-permission android:name="android.permission.INTERNET" />
  8. <!-- 允許存取Google所提供web型式的服務 -->
  9. <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
  10. <!-- 允許圖資暫存到外部儲存體 -->
  11. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  12. <!-- 允許WiFi或行動網路來定位 -->
  13. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  14. <!-- 允許GPS來定位 -->
  15. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  16. <!-- Maps API v2使用到OpenGL ES第2版功能 -->
  17. <uses-feature
  18. android:glEsVersion="0x00020000"
  19. android:required="true" />

  20. <application…
  21. <!-- android:value屬性要輸入申請的API金鑰 -->
  22. <meta-data
  23. android:name="com.google.android.maps.v2.API_KEY"
  24. android:value="AIzaSyAKSNNGbCW-uPT0RRfPDG-d8-N_3J4zLI8" />

  25. </application>
複製代碼



2.建立帶有Google地圖的layout檔案,必須搭配FragmentActivity ,可以支援舊版的Android系統
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".BasicMap3F" >
  10. <fragment
  11. android:id="@+id/fragment1"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. android:layout_alignParentTop="true"
  15. class="com.google.android.gms.maps.SupportMapFragment" />

  16. <ScrollView
  17. android:id="@+id/scrollView1"
  18. android:layout_width="wrap_content"
  19. android:layout_height="175dp"
  20. android:layout_alignBottom="@+id/fragment1"
  21. android:layout_alignLeft="@+id/fragment1" >

  22. <LinearLayout
  23. android:layout_width="match_parent"
  24. android:layout_height="match_parent"
  25. android:background="#FFFFFF"
  26. android:orientation="vertical" >

  27. <Spinner
  28. android:id="@+id/spinner1"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content" />

  31. <CheckBox
  32. android:id="@+id/checkBox1"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:onClick="cb1_click"
  36. android:checked="true"
  37. android:text="交通資訊" />

  38. <CheckBox
  39. android:id="@+id/checkBox2"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:onClick="cb2_click"
  43. android:checked="true"
  44. android:text="位置按鈕" />

  45. <CheckBox
  46. android:id="@+id/checkBox3"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content"
  49. android:onClick="cb3_click"
  50. android:checked="true"
  51. android:text="位址圖層" />

  52. <CheckBox
  53. android:id="@+id/checkBox4"
  54. android:layout_width="wrap_content"
  55. android:layout_height="wrap_content"
  56. android:onClick="cb4_click"
  57. android:checked="true"
  58. android:text="縮放按鈕" />
  59. <CheckBox
  60. android:id="@+id/checkBox5"
  61. android:layout_width="wrap_content"
  62. android:layout_height="wrap_content"
  63. android:onClick="cb5_click"
  64. android:checked="true"
  65. android:text="指北針" />
  66. <CheckBox
  67. android:id="@+id/checkBox6"
  68. android:layout_width="wrap_content"
  69. android:layout_height="wrap_content"
  70. android:onClick="cb6_click"
  71. android:checked="true"
  72. android:text="捲動手勢" />
  73. <CheckBox
  74. android:id="@+id/checkBox7"
  75. android:layout_width="wrap_content"
  76. android:layout_height="wrap_content"
  77. android:onClick="cb7_click"
  78. android:checked="true"
  79. android:text="縮放手勢" />
  80. <CheckBox
  81. android:id="@+id/checkBox8"
  82. android:layout_width="wrap_content"
  83. android:layout_height="wrap_content"
  84. android:onClick="cb8_click"
  85. android:checked="true"
  86. android:text="傾斜手勢" />

  87. <CheckBox
  88. android:id="@+id/checkBox9"
  89. android:layout_width="wrap_content"
  90. android:layout_height="wrap_content"
  91. android:onClick="cb9_click"
  92. android:checked="true"
  93. android:text="旋轉手勢" />
  94. </LinearLayout>
  95. </ScrollView>

  96. </RelativeLayout>
複製代碼



3.建立Activity檔案
  1. package com.derek.ch11_2_mapui;

  2. import com.google.android.gms.maps.GoogleMap;
  3. import com.google.android.gms.maps.SupportMapFragment;
  4. import com.google.android.gms.maps.UiSettings;
  5. import android.os.Bundle;
  6. import android.app.Activity;
  7. import android.support.v4.app.FragmentActivity;
  8. import android.view.Menu;
  9. import android.view.View;
  10. import android.widget.AdapterView;
  11. import android.widget.AdapterView.OnItemSelectedListener;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.CheckBox;
  14. import android.widget.Spinner;

  15. public class Ch11_2_MPUI extends FragmentActivity {
  16. GoogleMap map;
  17. UiSettings uiSettings;
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. if(map ==null)
  22. {
  23. map=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment1)).getMap();
  24. }else{
  25. map.setTrafficEnabled(true);
  26. map.setMyLocationEnabled(true);
  27. uiSettings=map.getUiSettings();
  28. }
  29. setMyMapType();
  30. }
  31. public void setMyMapType(){
  32. Spinner sp= (Spinner)findViewById(R.id.spinner1);
  33. ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.mapType, android.R.layout.simple_spinner_item);
  34. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  35. sp.setAdapter(adapter);
  36. sp.setOnItemSelectedListener(new OnItemSelectedListener(){

  37. @Override
  38. public void onItemSelected(AdapterView<?> parent, View view,
  39. int position, long id) {

  40. String mapType=parent.getItemAtPosition(position).toString();
  41. if(mapType.equals("一般圖"))
  42. map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
  43. else if(mapType.equals("混合圖"))
  44. map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
  45. else if(mapType.equals("衛星圖"))
  46. map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
  47. else if(mapType.equals("地形圖"))
  48. map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
  49. }
  50. @Override
  51. public void onNothingSelected(AdapterView<?> arg0) {

  52. }});
  53. }
  54. public void cb1_click(View v){
  55. // 顯示/隱藏交通流量
  56. map.setTrafficEnabled(((CheckBox)v).isChecked());
  57. }
  58. public void cb2_click(View v){
  59. // 顯示/隱藏自己位置按鈕
  60. uiSettings.setMyLocationButtonEnabled(((CheckBox)v).isChecked());
  61. }
  62. public void cb3_click(View v){
  63. // 顯示/隱藏自己位置圖層,如果未開?則自己位置按鈕也無法顯示
  64. map.setMyLocationEnabled(((CheckBox)v).isChecked());
  65. }
  66. public void cb4_click(View v){
  67. // 顯示/隱藏縮放按鈕
  68. uiSettings.setZoomControlsEnabled(((CheckBox)v).isChecked());
  69. }
  70. public void cb5_click(View v){
  71. // 顯示/隱藏指北針
  72. uiSettings.setCompassEnabled(((CheckBox)v).isChecked());
  73. }
  74. public void cb6_click(View v){
  75. // 開啟/關閉地圖捲動手勢
  76. uiSettings.setScrollGesturesEnabled(((CheckBox) v).isChecked());
  77. }
  78. public void cb7_click(View v){
  79. // 開啟/關閉地圖縮放手勢
  80. uiSettings.setZoomGesturesEnabled(((CheckBox) v).isChecked());
  81. }
  82. public void cb8_click(View v){
  83. // 開啟/關閉地圖傾斜手勢
  84. uiSettings.setTiltGesturesEnabled(((CheckBox) v).isChecked());
  85. }
  86. public void cb9_click(View v){
  87. // 開啟/關閉地圖旋轉手勢
  88. uiSettings.setRotateGesturesEnabled(((CheckBox) v).isChecked());
  89. }
  90. @Override
  91. protected void onResume() {
  92. super.onResume();
  93. if(map ==null)
  94. {
  95. map=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment1)).getMap();
  96. }else{
  97. map.setTrafficEnabled(true);
  98. map.setMyLocationEnabled(true);
  99. uiSettings=map.getUiSettings();
  100. }
  101. }
  102. }
複製代碼


 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-19 03:30 , Processed in 0.228584 second(s), 26 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表