TShopping

 找回密碼
 註冊
搜索
TShopping 精選文章 Android 手機開發 查看內容

[ch11_2_地圖種類與UI設定]

2013-7-10 19:42| 發佈者: woff| 查看: 1528| 評論: 0|原作者: woff

摘要: 1.設定Manifest檔案 !-- 允許存取Google Maps伺服器 -- permission android:name=com.derek.ch11_2_mapui.permission.MAPS_RECEIVE android:protectionLevel=signature / uses-permission android:name=com.derek.c ...
 

1.jpg



1.設定Manifest檔案




  1. android:name="com.derek.ch11_2_mapui.permission.MAPS_RECEIVE"
  2. android:protectionLevel="signature" />












  3. android:glEsVersion="0x00020000"
  4. android:required="true" />


  5. android:name="com.google.android.maps.v2.API_KEY"
  6. android:value="AIzaSyAKSNNGbCW-uPT0RRfPDG-d8-N_3J4zLI8" />

復制代碼



2.建立帶有Google地圖的layout檔案,必須搭配FragmentActivity ,可以支援舊版的Android系統

  1. xmlns:tools="http://schemas.android.com/tools"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:paddingBottom="@dimen/activity_vertical_margin"
  5. android:paddingLeft="@dimen/activity_horizontal_margin"
  6. android:paddingRight="@dimen/activity_horizontal_margin"
  7. android:paddingTop="@dimen/activity_vertical_margin"
  8. tools:context=".BasicMap3F" >
  9. android:id="@+id/fragment1"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:layout_alignParentTop="true"
  13. class="com.google.android.gms.maps.SupportMapFragment" />

  14. android:id="@+id/scrollView1"
  15. android:layout_width="wrap_content"
  16. android:layout_height="175dp"
  17. android:layout_alignBottom="@+id/fragment1"
  18. android:layout_alignLeft="@+id/fragment1" >

  19. android:layout_width="match_parent"
  20. android:layout_height="match_parent"
  21. android:background="#FFFFFF"
  22. android:orientation="vertical" >

  23. android:id="@+id/spinner1"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content" />

  26. android:id="@+id/checkBox1"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:onClick="cb1_click"
  30. android:checked="true"
  31. android:text="交通資訊" />

  32. android:id="@+id/checkBox2"
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:onClick="cb2_click"
  36. android:checked="true"
  37. android:text="位置按鈕" />

  38. android:id="@+id/checkBox3"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:onClick="cb3_click"
  42. android:checked="true"
  43. android:text="位址圖層" />

  44. android:id="@+id/checkBox4"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:onClick="cb4_click"
  48. android:checked="true"
  49. android:text="縮放按鈕" />
  50. android:id="@+id/checkBox5"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:onClick="cb5_click"
  54. android:checked="true"
  55. android:text="指北針" />
  56. android:id="@+id/checkBox6"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:onClick="cb6_click"
  60. android:checked="true"
  61. android:text="捲動手勢" />
  62. android:id="@+id/checkBox7"
  63. android:layout_width="wrap_content"
  64. android:layout_height="wrap_content"
  65. android:onClick="cb7_click"
  66. android:checked="true"
  67. android:text="縮放手勢" />
  68. android:id="@+id/checkBox8"
  69. android:layout_width="wrap_content"
  70. android:layout_height="wrap_content"
  71. android:onClick="cb8_click"
  72. android:checked="true"
  73. android:text="傾斜手勢" />

  74. android:id="@+id/checkBox9"
  75. android:layout_width="wrap_content"
  76. android:layout_height="wrap_content"
  77. android:onClick="cb9_click"
  78. android:checked="true"
  79. android:text="旋轉手勢" />



復制代碼



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 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-5-6 18:10 , Processed in 0.036440 second(s), 21 queries .

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

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

返回頂部