Google的3G平台Android學習(四)GPS在地图上定位

写在最前面:GoogleMap是我觉得最实用的应用

我想GoogleMap的大名无人不晓吧;从普通的电子地图,到卫星地图很多人都用过吧!  我们这里要讲的这个应用是建立在GoogleMap基础上的一个应用它结合了手机的GPS定位功能,也就是说你的手机要支持GPS全球定位;通过GPS定位你的坐标然后在GoogleMap上显示出来你的位置;

我们还是老样子先设计VIEW层:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/LinearLayout01"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android">
 <FrameLayout
  android:id="@+id/llayout02"  
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"    
  >
  <LinearLayout
   android:orientation="horizontal"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
  >
  <EditText android:id="@+id/locinput"   
    android:layout_width="260dp"
    android:layout_height="wrap_content"   
    android:singleLine="true"
    />
  <Button
   android:id="@+id/entry"
   android:layout_width="wrap_content"
   android:layout_height="fill_parent"
   android:text="SEARCH"
   android:inputType="numberDecimal" 
  />
  </LinearLayout>
 </FrameLayout>
  <view class="com.google.android.maps.MapView"
   android:id="@+id/mapview"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:clickable="true"
   android:apiKey="0CjiRLttps7spwpPELg-mRVtDD_58eHF1lsmxkA"
  >  
   </view>
 
</LinearLayout>

我啦解释下新出现的几个属性:

android:inputType="numberDecimal"  定义输入类型为浮点数

view class="com.google.android.maps.MapView" 定义一个MapView的视图类

android:clickable="true"可以被点击

android:apiKey="this is your apikey"这里是需要重点来讲 Google的一些未开放的API 需要我们申请一个类似于指纹识别的钥匙,这个钥匙对于不同的机器来说是不同得,GoogleMap就是需要这个钥匙才能调用;如果你要将这个应用发表到应用商场中去需要去申请一个专用的APIKEY,如果你像我样只是编程娱乐的话可以按照我告诉你的方法来获得这个APIKEY:

首先找到你的.android文件夹请看清楚前面有个“.”

先进入CMD(命令提示符)然后进入上面“.android”目录接着运行下面的代码

keytool -list -alias androiddebugkey \
-keystore  keystore \
-storepass android -keypass android

这个你会获得一个MD5码接着你要去一个网站:

http://code.google. com/android/maps-api-signup.html

我可以保证这个网站现在你去不了的,别问我为什么!因此你需要穿墙或者用在线代理来访问,按照网站提示你最终或得到一串识别码将这个识别码填写到

android:apiKey="this is your apikey"引号内;

到这里MAIN.XML设置完成了;不过我们还需要在AnadoirdManifest.xml文件中配置下才能引用GoogleMap:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.android.naturn"
      android:versionCode="1"
      android:versionName="1.0">
       <application android:icon="@drawable/icon" android:label="@string/app_name">     
     <uses-library android:name="com.google.android.maps" />
        <activity android:name=".GoogleMap"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>          
        </activity>
    </application>

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
</manifest>

首先我们在应用程序中引入一个类库

<uses-library android:name="com.google.android.maps" />

接着我们调用2个PERMISSION:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>允许系统进行网络通讯(网络套接字)

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>允许对本地的加密文件进行读取