使用Google Map Api在Android平台上开发地图应用3

时间:2023-02-01 17:03:30

去三峡玩了几天

先把设置界面写完

 

布局文件代码如下:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<!-- 选择个人设置 -->
<TextView android:id="@+id/setting_tips"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />

<!-- 设置Gps更新频率 -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/setting_gps" />

<!-- 设置Gps更新频率 -->
<Spinner android:id="@+id/setting_gps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/spinner_gps_prompt"
/>

<!-- 地图等级 -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/setting_map_level" />

<!-- 设置地图等级 -->
<Spinner android:id="@+id/setting_map_level"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/spinner_map_prompt"
/>
<!-- 确定按钮 -->
<Button android:id="@+id/setting_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/setting_submit" />

</LinearLayout>

 


 两个列表的数据资源放在array.xml中

<?xml version="1.0" encoding="utf-8"?><resources>    <!-- Used in View/setting.java -->    <string-array name="gps">        <item>5</item>        <item>10</item>        <item>15</item>        <item>30</item>        <item>60</item>        <item>120</item>    </string-array>        <string-array name="map">        <item>5</item>        <item>6</item>        <item>7</item>        <item>8</item>        <item>9</item>        <item>10</item>        <item>11</item>        <item>12</item>        <item>13</item>        <item>14</item>        <item>15</item>        <item>16</item>            </string-array>    </resources>


 

 

新建一个Setting的Activity类,用来控制设置界面的活动

代码如下:

package karant.map;import android.R.integer;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.Spinner;import android.widget.Toast;public class Setting extends Activity{private static final String TAG = "Setting";//定义菜单需要的常量private static final int MENU_MAIN = Menu.FIRST + 1;private static final int MENU_NEW = MENU_MAIN + 1;private static final int MENU_BACK = MENU_NEW + 1;//保存个性化设置public static final String SETTING_INFOS ="SETTING_Infos";public static final String SETTING_GPS = "SETTING_Gps";public static final String SETTING_MAP = "SETTING_Map";public static final String SETTING_GPS_POSITION = "SETTING_Gps_P";public static final String SETTING_MAP_POSITION = "SETTING_Map_p";private Button button_setting_submit ;private Spinner field_setting_gps;private Spinner field_setting_map_level;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.setting);setTitle(R.string.menu_setting);findViews();setListeners();//restorePrefs();}private void findViews(){Log.d(TAG, "find Views");button_setting_submit = (Button)findViewById(R.id.setting_submit);  //确定按钮field_setting_gps = (Spinner)findViewById(R.id.setting_gps);     //更新频率//获取选项,列表视图ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.gps, android.R.layout.simple_spinner_item);     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);field_setting_gps.setAdapter(adapter);  //把列表选项加到列表框中    field_setting_map_level = (Spinner)findViewById(R.id.setting_map_level);  //地图等级ArrayAdapter<CharSequence> adapter2 =ArrayAdapter.createFromResource(this, R.array.map, android.R.layout.simple_spinner_item);adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);field_setting_map_level.setAdapter(adapter2); //把列表选项加到列表框中}//设置监听器private void setListeners(){Log.d(TAG, "set Listeners");button_setting_submit.setOnClickListener(setting_submit); //组件设置单击的监听器为setting_submit}private Button.OnClickListener setting_submit = new Button.OnClickListener(){public void onClick(android.view.View v) {Log.d(TAG, "onClick new track..");try{String gps = (field_setting_gps.getSelectedItem().toString()); //获取刷新频率String map = (field_setting_map_level.getSelectedItem().toString()); //获取地图等级if(gps.equals("") || map.equals("")){Toast.makeText(Setting.this, getString(R.string.setting_null), Toast.LENGTH_SHORT).show();//提示消息}else{//保存设定//...后面补全//跳转到主界面Intent intent = new Intent();intent.setClass(Setting.this, MapAppActivity.class);  //跳转到最开始的界面startActivity(intent);}}catch (Exception e) {//提示错误消息Log.d(TAG, "error"+ e.toString());Toast.makeText(Setting.this, getString(R.string.setting_fail), Toast.LENGTH_SHORT).show();}}}; //实现创建菜单和选择菜单public boolean onCreateOptionsMenu(Menu menu) {super.onCreateOptionsMenu(menu);menu.add(0, MENU_MAIN, 0, R.string.menu_main).setIcon(R.drawable.icon).setAlphabeticShortcut('M');menu.add(0, MENU_NEW, 0, R.string.menu_new).setIcon(R.drawable.new_track).setAlphabeticShortcut('N');menu.add(0, MENU_BACK, 0, R.string.menu_back).setIcon(R.drawable.back).setAlphabeticShortcut('E');return true;};//当菜单被选中调用@Overridepublic boolean onOptionsItemSelected(MenuItem item) {Intent intent = new Intent();switch (item.getItemId()) {case MENU_NEW:  //新建跟踪intent.setClass(Setting.this, NewTrack.class);startActivity(intent);return true;case MENU_MAIN: //主界面intent.setClass(Setting.this, MapAppActivity.class);startActivity(intent);return true;case MENU_BACK:  //返回    finish();    break;}return true;}//退出程序保存下@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();Log.d(TAG, "save setting infos");//保存用户设置//storePrefs();}}


里面有些String定义前面没有定义,可以按照代码的提示在string.xml里面相应的添加

 

程序运行结果:

进入时的界面

使用Google Map Api在Android平台上开发地图应用3

 

设置界面

使用Google Map Api在Android平台上开发地图应用3

 

菜单

使用Google Map Api在Android平台上开发地图应用3