android Google 应用(二) -Google Map 的使用-
2012-01-11 15:26
561 查看
Google Map 的使用
Google Map是谷歌最成功的网络服务之一,广泛运用在旅游景点的查询和线路导航的场合。本节介绍在android平台上整合Google Map服务的应用程序,主要包括获取Map API Key,地图查询和导航应用。
1, 获取Map API Key
开发人员在开发Google Map程序之前需要申请一组验证过的Map API Key ,互联网上很容易查到。
------------------------------------------------------------------------------------------------------------------
申请android google map API key
1.首先找到debug keystore位置:
打开Eclipse--->Windows--->Preferences--->Android--->Build
一般是这样的路径 C:\Documents and Settings\Administrator\.android\debug.keystore
2.在cmd中执行
keytool -list -alias androiddebugkey -keystore "C:\Documents and Settings\Administrator\.android\debug.keystore" -storepass android -keypass android
D:\install\Java\jdk1.6.0_24\bin>keytool -list -alias androiddebugkey -keystore "
C:\Documents and Settings\Administrator\.android\debug.keystore" -storepass andr
oid -keypass android
androiddebugkey, 2011-3-14, PrivateKeyEntry,
认证指纹 (MD5): 33:27:0C:5F:63:4D:E2:D2:8A:E6:E2:5B:F8:86:6E:C5
复制代码
3.打开 http://code.google.com/intl/zh-CN/android/maps-api-signup.html
复制 认证指纹 (MD5):到下面的 My certificate's MD5 fingerprint
4.然后点击 Generate Api key
?
------------------------------------------------------------------------------------------------------------------
2, 创建模拟器
申请完Map API Key 后需要创建一个能够运行Google Map 的模拟器,创建步骤与正常的模拟器有所不同(省略)
3, Google Map 地图查询应用
下面通过一个案例来演示如何开发基于Google Map 服务的应用程序,该案例提供的功能是:通过用户输入经纬度,找出地图中的位置,并进行地图放大缩小,还能切换卫星视图,步骤如下:
3.1 在eclipse 新建一个android项目,在目录res\layout下,配置mian.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"
>
<!-- 声明了一个线性布局 -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
> <!-- 声明一个线性布局 -->
<TextView
android:text="@string/txtLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
<EditText
android:id="@+id/etLong"
android:text="@string/etLong"
android:layout_width="110px"
android:layout_height="45px"
/>
<TextView
android:text="@string/txtLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="8px"
/>
<EditText
android:id="@+id/etLat"
android:text="@string/etLat"
android:layout_width="110px"
android:layout_height="45px"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/btnGo"
android:text="@string/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
/>
<RadioGroup
android:id="@+id/rg"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<RadioButton
android:text="@string/normal"
android:id="@+id/normal"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
<RadioButton
android:text="@string/satellite"
android:id="@+id/satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
</RadioGroup>
</LinearLayout>
<!-- 声明了一个MapView 控件,这个是关键-->
<com.google.android.maps.MapView
android:id="@+id/mv"
android:clickable="true"
android:enabled="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0uUxs9aHasYpZ5iAhcWYXLbOgSHDTO1HNvXFh7w"
/>
</LinearLayout>
res\values目录下 string.xml 配置如下:
------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Sample_15_2!</string>
<string name="app_name">Sample_15_2</string>
<string name="txtLong">经度:</string>
<!-- 声明名为txtLong的字符串资源 -->
<string name="txtLat">纬度:</string>
<!-- 声明名为txtLat的字符串资源 -->
<string name="btnGo">查询</string>
<!-- 声明名为btnGo的字符串资源 -->
<string name="etLong">116.3975</string>
<!-- 声明名为etLong的字符串资源 -->
<string name="etLat">39.9083</string>
<!-- 声明名为etLat的字符串资源 -->
<string name="satellite">卫星视图</string>
<!-- 声明名为satellite的字符串资源 -->
<string name="normal">普通视图</string>
<!-- 声明名为normal的字符串资源 -->
</resources>
-----------------------------------------------------------------------------------------------------------------
下面是具体的功能代码:
-----------------------------------------------------------------------------------------------------------------
主类Activity –-> Google_Sample02 类:
package com.xtp;
import java.util.List;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
public class Google_Sample02 extends MapActivity {
/** Called when the activity is first created. */
MapView mv;
MapController controller;
Bitmap bmpArrow; //指向目标位置的箭头图标
RadioButton rbNormal;
RadioButton rbSatellite;
@Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.main);
bmpArrow = BitmapFactory.decodeResource(getResources(), R.drawable.arrow);
mv = (MapView) findViewById(R.id.mv);
controller = mv.getController();
mv.setBuiltInZoomControls(true); //设置是否显示放大缩小
Button btnGo = (Button) findViewById(R.id.btnGo);
btnGo.setOnClickListener(new OnClickListener() {
//设置 OnClickListener 监听器
@Override
public void onClick(View v) {
EditText etLong = (EditText) findViewById(R.id.etLong);
EditText etLat = (EditText) findViewById(R.id.etLat);
String strLong = etLong.getEditableText().toString().trim();
String strLat = etLat.getEditableText().toString().trim();
if (strLong.equals("") || strLat.equals("")) {
Toast.makeText(Google_Sample02.this, "请正确填写经纬度信息", Toast.LENGTH_LONG).show();
return; //不能忘记加上 “return”
}
double dLong = Double.parseDouble(strLong);
double dLat = Double.parseDouble(strLat);
updateMapView(dLong, dLat);
}
});
btnGo.performClick(); //执行,运转
RadioGroup rg = (RadioGroup) findViewById(R.id.rg);
rbNormal = (RadioButton) findViewById(R.id.normal);
rbSatellite = (RadioButton) findViewById(R.id.satellite);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//设置 onCheckedChangeListener 监听器
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == rbNormal.getId()) {
mv.setSatellite(false);
}else if (checkedId == rbSatellite.getId()) {
mv.setSatellite(true);
}
}
});
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void updateMapView(double dLat,double dLong){
GeoPoint gp = new GeoPoint((int)(dLat*1E6),(int)(dLong*1E6));
/**
* GeoPoint
* public GeoPoint(int latitudeE6,int longitudeE6)用给定的经度和纬度构造一个GeoPoint,单位微度 (度 * 1E6)。
* 参数:
* latitudeE6 - 该点的纬度,单位微度 (度 * 1E6)。为保持Mercator投影精确度,其取值范围是[-80*1E6,80*1E6]。
* longitudeE6 - 该点的经度,单位微度 (度 * 1E6)。可被规范化到(-180*1E6,180*1E6]。
*/
mv.displayZoomControls(true); //设置放大缩小按钮
controller.animateTo(gp); //将地图移到指定的位置
List<Overlay> ol = mv.getOverlays(); //获得 MapView 的 Overlay
ol.clear(); //清除掉原来的 Overlay
ol.add(new ArrawOverlay(gp,bmpArrow)); //添加一个新的 Overlay
}
}
-----------------------------------------------------------------------------------------------------------------
类ArrawOverlay:
package com.xtp;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
public class ArrawOverlay extends Overlay{
//成员变量
GeoPoint gp;
Bitmap bmpArrow;
//构造方法
public ArrawOverlay(GeoPoint gp,Bitmap bmp){
super();
this.gp = gp;
this.bmpArrow = bmp;
}
public void draw(Canvas canvas,MapView mapView,boolean shadow){
//重写 draw 方法
if (!shadow) {
Projection proj = mapView.getProjection(); //获得一个 Projection “投影” 对象
Point p = new Point();
proj.toPixels(gp, p); //将真实的地理上的坐标转换为设备屏幕上的坐标
canvas.drawBitmap(bmpArrow, p.x-bmpArrow.getWidth()/2, p.y-bmpArrow.getHeight(), null);
}
}
}
---------------------------------------------------------------------------------------------------------------
完成主要代码之后,不要忘了给Androidmanifest.xml 添加相应的权限
<uses-library android:name=”com.google.android.maps”/>
<uses-permission andoid:name=”android.permission.INTERNET”>
Google Map是谷歌最成功的网络服务之一,广泛运用在旅游景点的查询和线路导航的场合。本节介绍在android平台上整合Google Map服务的应用程序,主要包括获取Map API Key,地图查询和导航应用。
1, 获取Map API Key
开发人员在开发Google Map程序之前需要申请一组验证过的Map API Key ,互联网上很容易查到。
------------------------------------------------------------------------------------------------------------------
申请android google map API key
1.首先找到debug keystore位置:
打开Eclipse--->Windows--->Preferences--->Android--->Build
一般是这样的路径 C:\Documents and Settings\Administrator\.android\debug.keystore
2.在cmd中执行
keytool -list -alias androiddebugkey -keystore "C:\Documents and Settings\Administrator\.android\debug.keystore" -storepass android -keypass android
D:\install\Java\jdk1.6.0_24\bin>keytool -list -alias androiddebugkey -keystore "
C:\Documents and Settings\Administrator\.android\debug.keystore" -storepass andr
oid -keypass android
androiddebugkey, 2011-3-14, PrivateKeyEntry,
认证指纹 (MD5): 33:27:0C:5F:63:4D:E2:D2:8A:E6:E2:5B:F8:86:6E:C5
复制代码
3.打开 http://code.google.com/intl/zh-CN/android/maps-api-signup.html
复制 认证指纹 (MD5):到下面的 My certificate's MD5 fingerprint
4.然后点击 Generate Api key
?
感谢您注册 Android 地图 API 密钥! 您的密钥是: 0vDJgxtrB71gl2bwGRP2mPrW5jfSxeTno0xDyVw 此密钥适用于所有使用以下指纹所对应证书进行验证的应用程序: 33:27:0C:5F:63:4D:E2:D2:8A:E6:E2:5B:F8:86:6E:C5 下面是一个 xml 格式的示例,帮助您了解地图功能: <com.google.android.maps.MapView android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="0vDJgxtrB71gl2bwGRP2mPrW5jfSxeTno0xDyVw" /> |
2, 创建模拟器
申请完Map API Key 后需要创建一个能够运行Google Map 的模拟器,创建步骤与正常的模拟器有所不同(省略)
3, Google Map 地图查询应用
下面通过一个案例来演示如何开发基于Google Map 服务的应用程序,该案例提供的功能是:通过用户输入经纬度,找出地图中的位置,并进行地图放大缩小,还能切换卫星视图,步骤如下:
3.1 在eclipse 新建一个android项目,在目录res\layout下,配置mian.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"
>
<!-- 声明了一个线性布局 -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
> <!-- 声明一个线性布局 -->
<TextView
android:text="@string/txtLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
/>
<EditText
android:id="@+id/etLong"
android:text="@string/etLong"
android:layout_width="110px"
android:layout_height="45px"
/>
<TextView
android:text="@string/txtLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="8px"
/>
<EditText
android:id="@+id/etLat"
android:text="@string/etLat"
android:layout_width="110px"
android:layout_height="45px"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/btnGo"
android:text="@string/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
/>
<RadioGroup
android:id="@+id/rg"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<RadioButton
android:text="@string/normal"
android:id="@+id/normal"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
<RadioButton
android:text="@string/satellite"
android:id="@+id/satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioButton>
</RadioGroup>
</LinearLayout>
<!-- 声明了一个MapView 控件,这个是关键-->
<com.google.android.maps.MapView
android:id="@+id/mv"
android:clickable="true"
android:enabled="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0uUxs9aHasYpZ5iAhcWYXLbOgSHDTO1HNvXFh7w"
/>
</LinearLayout>
res\values目录下 string.xml 配置如下:
------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Sample_15_2!</string>
<string name="app_name">Sample_15_2</string>
<string name="txtLong">经度:</string>
<!-- 声明名为txtLong的字符串资源 -->
<string name="txtLat">纬度:</string>
<!-- 声明名为txtLat的字符串资源 -->
<string name="btnGo">查询</string>
<!-- 声明名为btnGo的字符串资源 -->
<string name="etLong">116.3975</string>
<!-- 声明名为etLong的字符串资源 -->
<string name="etLat">39.9083</string>
<!-- 声明名为etLat的字符串资源 -->
<string name="satellite">卫星视图</string>
<!-- 声明名为satellite的字符串资源 -->
<string name="normal">普通视图</string>
<!-- 声明名为normal的字符串资源 -->
</resources>
-----------------------------------------------------------------------------------------------------------------
下面是具体的功能代码:
-----------------------------------------------------------------------------------------------------------------
主类Activity –-> Google_Sample02 类:
package com.xtp;
import java.util.List;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
public class Google_Sample02 extends MapActivity {
/** Called when the activity is first created. */
MapView mv;
MapController controller;
Bitmap bmpArrow; //指向目标位置的箭头图标
RadioButton rbNormal;
RadioButton rbSatellite;
@Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.main);
bmpArrow = BitmapFactory.decodeResource(getResources(), R.drawable.arrow);
mv = (MapView) findViewById(R.id.mv);
controller = mv.getController();
mv.setBuiltInZoomControls(true); //设置是否显示放大缩小
Button btnGo = (Button) findViewById(R.id.btnGo);
btnGo.setOnClickListener(new OnClickListener() {
//设置 OnClickListener 监听器
@Override
public void onClick(View v) {
EditText etLong = (EditText) findViewById(R.id.etLong);
EditText etLat = (EditText) findViewById(R.id.etLat);
String strLong = etLong.getEditableText().toString().trim();
String strLat = etLat.getEditableText().toString().trim();
if (strLong.equals("") || strLat.equals("")) {
Toast.makeText(Google_Sample02.this, "请正确填写经纬度信息", Toast.LENGTH_LONG).show();
return; //不能忘记加上 “return”
}
double dLong = Double.parseDouble(strLong);
double dLat = Double.parseDouble(strLat);
updateMapView(dLong, dLat);
}
});
btnGo.performClick(); //执行,运转
RadioGroup rg = (RadioGroup) findViewById(R.id.rg);
rbNormal = (RadioButton) findViewById(R.id.normal);
rbSatellite = (RadioButton) findViewById(R.id.satellite);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//设置 onCheckedChangeListener 监听器
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == rbNormal.getId()) {
mv.setSatellite(false);
}else if (checkedId == rbSatellite.getId()) {
mv.setSatellite(true);
}
}
});
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
public void updateMapView(double dLat,double dLong){
GeoPoint gp = new GeoPoint((int)(dLat*1E6),(int)(dLong*1E6));
/**
* GeoPoint
* public GeoPoint(int latitudeE6,int longitudeE6)用给定的经度和纬度构造一个GeoPoint,单位微度 (度 * 1E6)。
* 参数:
* latitudeE6 - 该点的纬度,单位微度 (度 * 1E6)。为保持Mercator投影精确度,其取值范围是[-80*1E6,80*1E6]。
* longitudeE6 - 该点的经度,单位微度 (度 * 1E6)。可被规范化到(-180*1E6,180*1E6]。
*/
mv.displayZoomControls(true); //设置放大缩小按钮
controller.animateTo(gp); //将地图移到指定的位置
List<Overlay> ol = mv.getOverlays(); //获得 MapView 的 Overlay
ol.clear(); //清除掉原来的 Overlay
ol.add(new ArrawOverlay(gp,bmpArrow)); //添加一个新的 Overlay
}
}
-----------------------------------------------------------------------------------------------------------------
类ArrawOverlay:
package com.xtp;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Point;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
public class ArrawOverlay extends Overlay{
//成员变量
GeoPoint gp;
Bitmap bmpArrow;
//构造方法
public ArrawOverlay(GeoPoint gp,Bitmap bmp){
super();
this.gp = gp;
this.bmpArrow = bmp;
}
public void draw(Canvas canvas,MapView mapView,boolean shadow){
//重写 draw 方法
if (!shadow) {
Projection proj = mapView.getProjection(); //获得一个 Projection “投影” 对象
Point p = new Point();
proj.toPixels(gp, p); //将真实的地理上的坐标转换为设备屏幕上的坐标
canvas.drawBitmap(bmpArrow, p.x-bmpArrow.getWidth()/2, p.y-bmpArrow.getHeight(), null);
}
}
}
---------------------------------------------------------------------------------------------------------------
完成主要代码之后,不要忘了给Androidmanifest.xml 添加相应的权限
<uses-library android:name=”com.google.android.maps”/>
<uses-permission andoid:name=”android.permission.INTERNET”>
相关文章推荐
- 首届 Google 暑期大学生博客分享大赛——2010 Android 篇 android关于 Google Map 开发的应用
- 使用Google Map Api在Android平台上开发地图应用4
- 使用Android Google Map开发LBS应用
- 使用Google帐号登录Android应用
- android Google 应用(一) -使用GPS定位手机的位置-
- 使用Google Map Api在Android平台上开发地图应用2
- 使用Google Map Api在Android平台上开发地图应用3
- 如何使用Google Map API开发Android地图应用
- 使用Google Map Api在Android平台上开发地图应用1
- 【公益】Google技术用户组Android 4.0典型应用案例讲座
- Android应用之——微信微博第三方sdk登录分享使用过程中的一些常见问题
- android studio lint(查看未被应用的资源文件)的使用
- android开发(26) 和其他应用交换数据方式一,使用intent指定自定义action调用其他程序里的activity,并获得其返回的结果
- Android应用性能优化之使用SparseArray替代HashMap
- 使用Fiddler抓取手机应用数据接口(Android和ios)
- 【Android开发基础】应用界面主题Theme使用方法
- 【Android 应用开发】Android资源文件 - 使用资源存储字符串 颜色 尺寸 整型 布尔值 数组
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- android应用开发全程实录-你有多熟悉listview? getView重写 inflate使用
- Android应用使用Multidex突破64K方法数限制