/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ostrichmyself;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
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;
import com.google.android.maps.Projection;
/**
* 注意:记得修改Map APK Key
* @author tiant5
*
*/
public class TraceMap extends MapActivity {
private Handler timeHandler = new Handler();
private Button traceBegin;
private MapView mapView;
//WholeLineOverLay 的出台是为了播放完毕后删除之前的PlayOverLay, 避免总是重画
private WholeLineOverLay wholeLineOverLay;
private GeoPoint[] pointArr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.trace_mapview);
mapView = (MapView) findViewById(R.id.map);
traceBegin = (Button) findViewById(R.id.traceBtn);
traceBegin.setOnClickListener(new TraceBeginListener());
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
MapController mapController = mapView.getController();
// mapView.setTraffic(true);// 交通图
// mapView.setSatellite(true);//卫星图
// mapView.setStreetView(true);//街景
pointArr = new GeoPoint[9];
mapController.setZoom(15);// 初始放大倍数
pointArr[0] = new GeoPoint(28238389, 113085616);
pointArr[1] = new GeoPoint(28240846, 113085530);
pointArr[2] = new GeoPoint(28245647, 113085659);
pointArr[3] = new GeoPoint(28245610, 113092225);
pointArr[4] = new GeoPoint(28245496, 113096817);
pointArr[5] = new GeoPoint(28245458, 113102653);
pointArr[6] = new GeoPoint(28241413, 113102782);
pointArr[7] = new GeoPoint(28234683, 113102911);
pointArr[8] = new GeoPoint(28233511, 113096774);
wholeLineOverLay = new WholeLineOverLay(pointArr);
mapController.animateTo(pointArr[4]);
}
class TraceBeginListener implements OnClickListener {
public void onClick(View arg0) {
/**
* 另起线程, 实现动态描绘
*/
final PlayOverLay playOverLay = new PlayOverLay(pointArr);
synchronized(mapView.getOverlays())
{
//有两个线程操作, 避免同步带来的问题
mapView.getOverlays().remove(wholeLineOverLay);
mapView.getOverlays().add(playOverLay);
}
new Thread() {
public void run() {
for (int i = 0; i < pointArr.length - 1; i++) {
//更新界面的动作需要放在Handler中处理, 否则无法移动界面
final int index = i;
timeHandler.post(new Runnable() {
public void run() {
playOverLay.nextIndex(index);
//失效, 引发对OverLay的调用
mapView.invalidate();
}
});
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
synchronized(mapView.getOverlays())
{
////有两个线程操作, 避免同步带来的问题
mapView.getOverlays().remove(playOverLay);
mapView.getOverlays().add(wholeLineOverLay);
}
}
}.start();
//整个图像
}
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
//两点连起来, 就是一个线段
class PlayOverLay extends Overlay {
private int currentIndex = -1;
private GeoPoint[] pointArr;
private Path path;
public PlayOverLay(GeoPoint[] pointArr) {
this.pointArr = pointArr;
path = new Path();
}
public void nextIndex(int nextIndex)
{
currentIndex = nextIndex;
}
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
// 画笔
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setDither(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(4);
Projection projection = mapView.getProjection();
if (currentIndex == -1 || currentIndex + 1 >= pointArr.length)
{
return false;
}
GeoPoint first = pointArr[currentIndex];
GeoPoint second = pointArr[currentIndex + 1];
Point p1 = new Point();
projection.toPixels(first, p1);
path.moveTo(p1.x, p1.y);
Point p2 = new Point();
projection.toPixels(second, p2);
path.lineTo(p2.x, p2.y);
canvas.drawPath(path, paint);// 画出路径
return true;
}
}
//两点连起来, 就是一个线段
class WholeLineOverLay extends Overlay {
private GeoPoint[] pointArr;
public WholeLineOverLay(GeoPoint[] pointArr) {
this.pointArr = pointArr;
}
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
// TODO Auto-generated method stub
super.draw(canvas, mapView, shadow);
// 画笔
Paint paint = new Paint();
paint.setColor(Color.GREEN);
paint.setDither(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(4);
Projection projection = mapView.getProjection();
Path path = new Path();
for (int i = 0; i < pointArr.length -1; i++)
{
GeoPoint first = pointArr[i];
GeoPoint second = pointArr[i + 1];
Point p1 = new Point();
projection.toPixels(first, p1);
path.moveTo(p1.x, p1.y);
Point p2 = new Point();
projection.toPixels(second, p2);
path.lineTo(p2.x, p2.y);
}
canvas.drawPath(path, paint);// 画出路径
return true;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
最新版本的Android Google Map 轨迹回放源码, 可以用Google Map实现gps动态导航的功能. 参看博客地址:http://blog.csdn.net/ostrichmyself/article/details/6821112, 修正两个问题: 1. 不能重复播放 2. 拖动才能显示播放 3. OverLay过多的问题,目前用两个Overlay搞定一切播放问题
资源推荐
资源详情
资源评论
收起资源包目录
android_google_map.zip (42个子文件)
android_google_map
.project 854B
default.properties 378B
bin
com
ostrichmyself
TraceMap.class 3KB
R$string.class 424B
R$layout.class 503B
TraceMap$TraceBeginListener$1.class 2KB
TraceMap$PlayOverLay.class 3KB
R$drawable.class 509B
R.class 524B
TraceMap$WholeLineOverLay.class 3KB
DisplayMap$MyLocationOverlay.class 2KB
MapShow$ItemClickListener.class 2KB
MapShow.class 2KB
TraceMap$TraceBeginListener$1$1.class 1KB
ConstConfiguration.class 275B
R$attr.class 337B
TraceMap$TraceBeginListener.class 2KB
DisplayMap.class 2KB
R$id.class 578B
resources.ap_ 27KB
android_google_map.apk 35KB
classes.dex 13KB
proguard.cfg 1KB
.settings
org.eclipse.jdt.core.prefs 208B
AndroidManifest.xml 1KB
src
com
ostrichmyself
TraceMap.java 6KB
DisplayMap.java 4KB
ConstConfiguration.java 166B
MapShow.java 4KB
res
values
strings.xml 176B
layout
trace_mapview.xml 2KB
appmain.xml 641B
mapview.xml 2KB
appmain_inneritem.xml 717B
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
service_ebusiness.png 6KB
service_maintenance.png 6KB
home.png 2KB
icon.png 3KB
.classpath 280B
gen
com
ostrichmyself
R.java 1KB
assets
共 42 条
- 1
x-teamer团队
- 粉丝: 1206
- 资源: 47
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页