01.12.2014 Views

Google Map Google Map 应用

Google Map Google Map 应用

Google Map Google Map 应用

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Google</strong> <strong>Map</strong> 应 用<br />

<strong>Google</strong> <strong>Map</strong>


实 验 十 一 :<strong>Google</strong> <strong>Map</strong> 应 用<br />

【 目 的 】<br />

熟 练 使 用 Android 的 SDK 开 发 <strong>Google</strong> <strong>Map</strong> 应 用 。<br />

【 要 求 】<br />

1、 定 时 通 过 GPS 获 取 当 前前 位 置 的 经 纬 度 坐 标 ;<br />

2、 随 位 置 移 动动 更 新 <strong>Google</strong> <strong>Map</strong>, 使 其 以 当 前前 位 置 为 中 心 显 示 地 图 。<br />

【 原 理 】<br />

通 过 GPS 硬 件 获 取 经 纬 坐 标 , 通 过 <strong>Google</strong> APIs 实 现 地 图 的 显 示 和和 定 位 。<br />

【 堂 上 练 习 】<br />

编 写 一 个 <strong>Google</strong> <strong>Map</strong> 的 应 用 程 序 , 通 过 手 机 上 的 GPS 设 备 定 时 获 取 并 显 示 当<br />

前前 位 置 的 经 纬 度 坐 标 , 随 手 机 的 移 动动 以 当 前前 坐 标 为 中 心 显 示 <strong>Google</strong> <strong>Map</strong>。<br />

1. 最 顶 层 设 计 :<br />

图 1 <strong>Google</strong> <strong>Map</strong> 效 果 图<br />

(1) 使 用 <strong>Map</strong>View 显 示 <strong>Google</strong> <strong>Map</strong>, 使 用 <strong>Map</strong>Controller 控 制制 <strong>Map</strong>View 的 显 示


属 性 ( 如 大 小 , 地 图 中 心 位 置 等 );<br />

(2) 使 用 LocationManager 对 象 和和 设 计 getLocationPrivider() 方 法 负 责 管<br />

理 、 获 取 位 置 坐 标 等 功 能 的 实 现 ;<br />

(3) 设 计 getGeoByLocation() 方 法 提 取 出 经 纬 度 至 GeoPoint 对 象 , 设 计<br />

refresh<strong>Map</strong>View() 方 法 以 当 前前 坐 标 为 中 心 显 示 <strong>Google</strong> <strong>Map</strong>;<br />

(4) 以 LocationManager 的 requestLocationUpdates() 方 法 设 置 监 听听 器 , 定 时<br />

更 新 坐 标 。<br />

<strong>Map</strong>Activity 中 onCreate 函 数 部 分 代 码 如 下 :<br />

/* 创 建 <strong>Map</strong>View 对 象 */<br />

m<strong>Map</strong>View = (<strong>Map</strong>View)findViewById(R.id.my<strong>Map</strong>View1);<br />

m<strong>Map</strong>Controller = m<strong>Map</strong>View.getController();<br />

/* Provider 初 始 化化 */<br />

mLocationManager = (LocationManager)<br />

getSystemService(Context.LOCATION_SERVICE);<br />

/* 取 得 Provider 与 Location */<br />

getLocationPrivider();<br />

if(mLocation!=null)<br />

{<br />

/* 取 得 目 前前 的 经 纬 度 */<br />

gp1=getGeoByLocation(mLocation);<br />

/* 将 <strong>Map</strong>View 的 中 点 移 至 目 前前 位 置 */<br />

refresh<strong>Map</strong>View();<br />

/* 设 置 事 件 的 Listener */<br />

mLocationManager.requestLocationUpdates(mLocationPrivider,<br />

2000, 10, mLocationListener);<br />

}<br />

else<br />

{<br />

XXXX.this.finish();<br />

}<br />

2. getLocationPrivider() 设 计 : 以 Criteria 对 象 和和 LocationManager.<br />

getBestProvider() 设 置 获 取 坐 标 时 的 各 种 属 性 ; 以 LocationManager.<br />

getLastKnownLocation() 取 得 包包 含 当 前前 坐 标 的 Location 对 象 。<br />

/* 取 得 LocationProvider */<br />

public void getLocationPrivider()<br />

{<br />

/* 设 置 Provider 的 各 种 属 性 */<br />

Criteria mCriteria01 = new Criteria();


mCriteria01.setAccuracy(Criteria.ACCURACY_FINE);<br />

mCriteria01.setAltitudeRequired(false);<br />

mCriteria01.setBearingRequired(false);<br />

mCriteria01.setCostAllowed(true);<br />

mCriteria01.setPowerRequirement(Criteria.POWER_LOW);<br />

mLocationPrivider = mLocationManager<br />

.getBestProvider(mCriteria01, true);<br />

mLocation = mLocationManager<br />

.getLastKnownLocation(mLocationPrivider);<br />

}<br />

3. getGeoByLocation() 设 计 : 从 Location 对 象 中 分 别 取 出 经 度 和和 纬 度 , 创<br />

建 并 返 回 GeoPoint 对 象 。<br />

/* 取 得 GeoPoint 的 方 法 */<br />

private GeoPoint getGeoByLocation(Location location)<br />

{<br />

GeoPoint gp = null;<br />

try<br />

{<br />

if (location != null)<br />

{<br />

double geoLatitude = location.getLatitude()*1E6;<br />

double geoLongitude = location.getLongitude()*1E6;<br />

gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);<br />

}<br />

}<br />

catch(Exception e)<br />

{<br />

e.printStackTrace();<br />

}<br />

return gp;<br />

}<br />

4. refresh<strong>Map</strong>View() 设 计 : 使 用 <strong>Map</strong>Controller 的 animateTo() 方 法 使<br />

<strong>Map</strong>View 以 GeoPoint 对 象 表 示 的 经 纬 度 为 中 心 显 示 地 图 。<br />

/* 更 新 <strong>Map</strong>View 的 方 法 */<br />

public void refresh<strong>Map</strong>View()<br />

{<br />

m<strong>Map</strong>View.displayZoomControls(true);<br />

<strong>Map</strong>Controller myMC = m<strong>Map</strong>View.getController();<br />

myMC.animateTo(gp2);<br />

myMC.setZoom(zoomLevel);<br />

m<strong>Map</strong>View.setSatellite(false);


}<br />

5. 创 建 LocationListener 对 象 : 监 听听 位 置 变 化化 , 并 作 出 相 应 的 更 新 位 置 和和<br />

<strong>Map</strong>View 的 操 作 。<br />

/* <strong>Map</strong>View 的 Listener */<br />

public final LocationListener mLocationListener =<br />

new LocationListener()<br />

{<br />

@Override<br />

public void onLocationChanged(Location location)<br />

{<br />

}<br />

@Override<br />

public void onProviderDisabled(String provider)<br />

{<br />

}<br />

@Override<br />

public void onProviderEnabled(String provider)<br />

{<br />

}<br />

@Override<br />

public void onStatusChanged(String provider, int status,<br />

Bundle extras)<br />

{<br />

}<br />

};<br />

【 课 后 习 题 】<br />

在 堂 上 练 习 的 基 础 上 添 加 以 下 功 能 :<br />

1. 实 现 放 大 和和 缩 小 地 图 功 能 。<br />

2. 在 <strong>Map</strong>View 上 画 出 移 动动 的 轨 迹 , 添 加 开 始 记 录 和和 结 束 记 录 的 按 钮 。 选 择<br />

开 始 记 录 才 启启 动动 定 时 获 取 坐 标 功 能 , 并 画 出 运 动动 轨 迹 , 选 择 结 束 记 录 则<br />

停 止 跟 踪 。<br />

3. 在 整 个 跟 踪 期 间 计 算 并 显 示 目 前前 的 总 移 动动 距 离 。<br />

【 选 做 】


添 加 保 存 轨 迹 功 能 。 将 一 次 完 整 的 轨 迹 记 录 保 存 下 来 , 可 查 看 以 往 的 轨 迹 记<br />

录 , 可 在 以 往 的 轨 迹 基 础 上 继 续 跟 踪 。

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!