有这么一种说法,导航设备属于第一代基于定位的应用,新一代基于定位的应用可以让手机用户随时掌握自己周围的信息。Android平台的Google Map功能确实是给开发者很大的便利。下面就来给大家介绍Android Google Map开发的一些小知识。
一.手机定位的几种方法:
1. 基于基站的Cell ID:
全球每个基站都有唯一的标识符和固定的地理坐标。手机可以根据联系到的基站的Cell ID和地理坐标大致估算自己的位置。
2. 基于三角定位:
手机处于多个基站的共同区域,GSM技术可以判断手机信号的方向,三角定位技术就可以更精确的确定手机当前的位置。
3. 基于GPS定位:
通过手机上的GPS功能,用户可以精确的确定自己的地理位置。
二.获得Map API
在命令行窗口输入命令:
keytool -list -alias androiddebugkey -keystore "C:\Users\Administrator\.android\debug.keystore" -storepass android -keypass android
然后点击“Enter”,获得认证指纹<MD5>(见下图马赛克部分),不要怕麻烦,一定要记下来
然后在浏览器(使用Google Chrome浏览器)中输入网址:
http://code.google.com/intl/zh-CN/android/maps-api-signup.html ;
在 “I have read and agree with the terms and conditions (printable version)” 前面的小方格中打上钩,在下面的“My certificate's MD5 fingerprint”后的空格中填上自己的认证指纹<MD5>,然后点击“Generate API Key” ,等待就可以了,然后就会出现一个新的界面,记得要记下其中长长的密钥,务必要记下(复制—粘贴一下就解决了)。还有下面一个XML文件示例,也一起复制粘贴一下吧。
然后API的事情已经解决了,很简单吧!
三.在程序中显示地图的最简单方法:
新建一个项目(Android Project),在布局文件中加入Google API给予的程序示例:
布局文件main.xml:
<?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"
>
<com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="此处为你获得的密钥"
/>
</LinearLayout>
在自己的.java文件中将继承类Activity改为MapActivity:
源代码:MyMapActivity.java文件:
package com.google.android.mymap1;
import com.google.android.maps.MapActivity;
import android.os.Bundle;
public class MyMapActivity extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
在AndroidManifest.xml文件中加入如下几行代码:
<uses-library android:name="com.google.android.maps"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
AndroidManifest.xml文件(注意:如果没有INTERNET许可,只会出现一个网格图形而不会看到地图。还要注意添加的位置):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.mymap1"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps"/>
<activity android:name=".MyMapActivity"
android:label="@string/app_name">
<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 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>
之后保存,运行以Android Application,等待结果,显示如下:

四.在程序中获得自己的地理坐标:
下面以在程序中以字符串形式显示自己位置的经度纬度为例:
布局文件中添加两个带有ID的TextView:
<?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/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/TextView02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
在AndroidManifest.xml文件中,加入如下代码:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
java源文件的内容如下:
package com.google.android.mymap3;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class MyMap3Activity extends Activity {
private LocationManager locationmanager;
public TextView textView1;
public TextView textView2;
Location location;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView1=(TextView)findViewById(R.id.TextView01);
textView2=(TextView)findViewById(R.id.TextView02);
///getSystemService()方法连接定位管理器LocationManager
locationmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
//使用此方法获取当前的位置信息
location=locationmanager.getLastKnownLocation("gps");
//获取经度和纬度
textView1.setText("纬度:"+Double.toString(location.getLatitude()));
textView2.setText("经度:"+Double.toString(location.getLongitude()));
//最后这几行代码是获得坐标的核心代码,一定要注意哦
}
}
最后以Android Application运行,显示结果如下:
关于Google Maps的程序一般一次两次都会出现问题,所以调试这类程序一定要有耐心。
注意:坐标由DDMS显示模式中的模拟器控制器中send入。
在运行程序之前就要点击send按钮。
以上是自己在学习Android开发过程中的一点小日志,希望对初学Android Map开发的人有所帮助。
注:文章参加"暑假大学生博客分享大赛—2011Android 成长篇"
本文链接:http://blog.youkuaiyun.com/android2011_1/article/details/6630159