Android教程:在百度地图上画出轨迹
[日期:2013-04-14] | 来源:Linux社区 作者:crazyxin1988 | [字体:大 中 小] |
接着上面的项目《Android访问webservice.客户端登录注册》http://www.linuxidc.com/Linux/2013-04/82747.htm,实现餐厅搜索,这个在吃客游记里就做过了,然后把餐厅显示出来,可以把该餐厅加入轨迹
关于轨迹点操作的前后台实现和之前的登录注册差不多,这里主要说一下,用户查看自己的轨迹时候,在手机
端的显示。
1.从服务器把轨迹点拿下来
2.地图上显示点
3.把点连成线
看代码吧,注释比较详细,关键的画线操作就在OverItemT这个类中。
package seu.android007.xin;
import java.util.ArrayList;
import java.util.List;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.Projection;
import seu.android007.service.TraceServcie;
import seu.xin.entity.Trace;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;
public class MyTrace extends MapActivity {
static MapView mMapView = null;
LocationListener mLocationListener = null;// onResume时注册此listener,onPause时需要Remove
MyLocationOverlay mLocationOverlay = null; // 定位图层
static View mPopView = null; // 点击mark时弹出的气泡View
TraceServcie traceServcie;
private ArrayList<Trace> myTraces = new ArrayList<Trace>();
SharedPreferences sp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_trace);
traceServcie = new TraceServcie();
sp = this.getSharedPreferences("data", MODE_WORLD_READABLE);
int uid = sp.getInt("uid", 0);
myTraces = traceServcie.findTracesByUid(uid);
BMapApp app = (BMapApp) this.getApplication();
if (app.mBMapMan == null) {
app.mBMapMan = new BMapManager(getApplication());
app.mBMapMan.init(app.mStrKey, new BMapApp.MyGeneralListener());
}
app.mBMapMan.start();
// 如果使用地图SDK,请初始化地图Activity
super.initMapActivity(app.mBMapMan);
mMapView = (MapView) findViewById(R.id.bmapView);
mMapView.setBuiltInZoomControls(true);
// 设置在缩放动画过程中也显示overlay,默认为不绘制
mMapView.setDrawOverlayWhenZooming(true);
// 添加定位图层
mLocationOverlay = new MyLocationOverlay(this, mMapView);
mMapView.getOverlays().add(mLocationOverlay);
// 注册定位事件
mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
GeoPoint pt = new GeoPoint(
(int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6));
mMapView.getController().animateTo(pt);
}
}
};
if (myTraces.size() == 0) {
System.out.println("轨迹为空,调转到MyActivityGroup");
AlertDialog.Builder builder = new AlertDialog.Builder(MyTrace.this);
builder.setTitle("提醒");
builder.setMessage("没有轨迹,请添加后再查看");
builder.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MyTrace.this, MyActivityGroup.class);
startActivity(intent);
MyTrace.this.finish();
}
});
builder.create().show();
} else {
System.out.println("获得点后添加到图层");
// 添加ItemizedOverlay
Drawable marker = getResources().getDrawable(R.drawable.iconmarka); // 得到需要标在地图上的资源
marker.setBounds(0, 0, marker.getIntrinsicWidth(),
marker.getIntrinsicHeight()); // 为maker定义位置和边界
mMapView.getOverlays().add(new OverItemT(marker, this, myTraces)); // 添加ItemizedOverlay实例到mMapView
// 创建点击mark时的弹出泡泡
mPopView = super.getLayoutInflater()
.inflate(R.layout.popview, null);
mMapView.addView(mPopView, new MapView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, null,
MapView.LayoutParams.TOP_LEFT));
mPopView.setVisibility(View.GONE);
}
}
@Override
protected void onPause() {
BMapApp app = (BMapApp) this.getApplication();
app.mBMapMan.getLocationManager().removeUpdates(mLocationListener);
mLocationOverlay.disableMyLocation();
mLocationOverlay.disableCompass(); // 关闭指南针
app.mBMapMan.stop();
super.onPause();
}
@Override
protected void onResume() {
BMapApp app = (BMapApp) this.getApplication();
// 注册定位事件,定位后将地图移动到定位点
app.mBMapMan.getLocationManager().requestLocationUpdates(
mLocationListener);
mLocationOverlay.enableMyLocation();
mLocationOverlay.enableCompass(); // 打开指南针
app.mBMapMan.start();
super.onResume();
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
class OverItemT extends ItemizedOverlay<OverlayItem> {
private List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();
private Drawable marker;
private Context mContext;
private ArrayList<Trace> traces = new ArrayList<Trace>();
public OverItemT(Drawable marker, Context context, ArrayList<Trace> traces) {
super(boundCenterBottom(marker));
this.marker = marker;
this.mContext = context;
this.traces = traces;
// 用给定的经纬度构造GeoPoint,单位是微度 (度 * 1E6)
// 构造OverlayItem的三个参数依次为:item的位置,标题文本,文字片段
for (int i = 0; i < traces.size(); i++) {
int lat = new Integer(traces.get(i).getLatitude());
int lon = new Integer(traces.get(i).getLongitude());
String resName = traces.get(i).getResname();
GeoPoint p = new GeoPoint(lat, lon);
mGeoList.add(new OverlayItem(p, resName, i + resName));
populate(); // createItem(int)方法构造item。一旦有了数据,在调用其它方法前,首先调用这个方法
}
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// Projection接口用于屏幕像素坐标和经纬度坐标之间的变换
Projection projection = mapView.getProjection();
for (int index = size() - 1; index >= 0; index--) { // 遍历mGeoList
OverlayItem overLayItem = getItem(index); // 得到给定索引的item
String title = overLayItem.getTitle();
// 把经纬度变换到相对于MapView左上角的屏幕像素坐标
Point point = projection.toPixels(overLayItem.getPoint(), null);
// 可在此处添加您的绘制代码
Paint paintText = new Paint();
paintText.setColor(Color.BLUE);
paintText.setTextSize(15);
canvas.drawText(title, point.x - 30, point.y, paintText); // 绘制文本
}
super.draw(canvas, mapView, shadow);
// 调整一个drawable边界,使得(0,0)是这个drawable底部最后一行中心的一个像素
// 先所有点的经度转像素
ArrayList<Point> points = new ArrayList<Point>();
for (int i = 0; i < this.size(); i++) {
Point p = new Point();
OverlayItem overLayItem = getItem(i);
projection.toPixels(overLayItem.getPoint(), p);
points.add(p);
}
// 第二个画笔 画线
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setDither(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(4);
// 连接 所有点
Path path = new Path();
path.moveTo(points.get(0).x, points.get(0).y);
for (int i = 1; i < points.size(); i++) {
path.lineTo(points.get(i).x, points.get(i).y);
}
// 画出路径
canvas.drawPath(path, paint);
boundCenterBottom(marker);
}
@Override
protected OverlayItem createItem(int i) {
// TODO Auto-generated method stub
return mGeoList.get(i);
}
@Override
public int size() {
// TODO Auto-generated method stub
return mGeoList.size();
}
@Override
// 处理当点击事件
protected boolean onTap(int i) {
setFocus(mGeoList.get(i));
// 更新气泡位置,并使之显示
GeoPoint pt = mGeoList.get(i).getPoint();
MyTrace.mMapView.updateViewLayout(MyTrace.mPopView,
new MapView.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, pt,
MapView.LayoutParams.BOTTOM_CENTER));
MyTrace.mPopView.setVisibility(View.VISIBLE);
Toast.makeText(this.mContext, mGeoList.get(i).getSnippet(),
Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onTap(GeoPoint arg0, MapView arg1) {
// TODO Auto-generated method stub
// 消去弹出的气泡
MyTrace.mPopView.setVisibility(View.GONE);
return super.onTap(arg0, arg1);
}
}
运行的效果图:
Android教程:在百度地图上画出轨迹的更多相关文章
- Android中Google地图路径导航,使用mapfragment地图上画出线路(google map api v2)详解
在这篇里我们只聊怎么在android中google map api v2地图上画出路径导航,用mapfragment而不是mapview,至于怎么去申请key,manifest.xml中加入的权限,系 ...
- GPS坐标显示在百度地图上(Qt+百度地图)
Qt在5.6以后的版本就不支持webview控件了,这里我用的是Qt5.4的版本,里面还有这个控件: 下面简单介绍下Qt与html中的javascript调用交互过程: 一.整体实现介绍 在html中 ...
- 从POI到O2O 看百度地图如何走出未来之路
近期O2O的烧钱融资大战如火如荼,有人已经把O2O大战,用乌合之众的群体心理失控来形容.其实厂商都不傻,O2O烧钱大家都知道,但是大家还知道O2O背后这块大蛋糕价值"万亿级". 有 ...
- [置顶]
Xamarin android如何调用百度地图入门示例(一)
在Xamarin android如何调用百度地图呢? 首先我们要区分清楚,百度地图这是一个广泛的概念,很多刚刚接触这个名词"百度地图api",的确是泛泛而谈,我们来看一下百度地图的 ...
- Bing必应地图中国API - 在地图上画圆
Bing必应地图中国API - 在地图上画圆 2011-05-24 14:49:37| 分类: Bing&Google|字号 订阅 <变形金刚2>上映4日国内票房过亿,基 ...
- Android Studio使用百度地图示例BaiduMapsApiASDemo
Android Studio使用百度地图示例BaiduMapsApiASDemo 用自己AVD下的debug.keystore替换掉项目中的debug.keystore 生成自己的签名 同样的方法生成 ...
- ArcGIS API for JavaScript根据两个点坐标在地图上画线
ArcGIS API for JavaScript根据两个点坐标在地图上画线比如说a(xxxx,xxxxx),b(xxxx,xxxxx).利用这两个点画一条线 var polyline = new e ...
- 使用JavaScript在Canvas上画出一片星空
随着Html5的迅猛发展,画布也变得越来越重要.下面我就写一个关于在canvas上画出一片星空的简单的代码的示例. 理论基础 初始化一个canvas,获得一个用于绘制图形的上下文环境context.并 ...
- 【地图功能开发系列:二】根据地址名称通过百度地图API查询出坐标
根据地址名称通过百度地图API查询出坐标 百度地图ApiUrl string url = "http://api.map.baidu.com/geocoder?address={0}& ...
随机推荐
- oracle系统化学习笔记
CentOS 4.x上安装Oracle 9i(3讲) oracle9i非常成熟,刚学主要是学9i比较经典 学会安装9i具有现实意义,先学完9i再学11g等比较好 1.安装centos 2.安装orac ...
- Uniform Generator HDU1014
题意 给你公式seed(x+1) = [seed(x) + STEP] % MOD ,输入step和mod, 问你是否可以从第一项0,算到mod,它们是否都不同 是 good choice 否则 ba ...
- Eddy's mistakes HDU
链接 [http://acm.hdu.edu.cn/showproblem.php?pid=1161] 题意 把字符串中大写字母变为小写 . 分析 主要是含有空格的字符串如何读入,用getline(c ...
- HDOJ2009_求数列的和
简单的考察对浮点数使用的水题 HDOJ2009_求数列的和 #include<iostream> #include<stdio.h> #include<stdlib.h& ...
- atcoder B - Frog 2 (DP)
B - Frog 2 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement There a ...
- iOS开发线程安全问题
先来看一下代码: - (void)viewDidLoad { [super viewDidLoad]; self.testStr = @"String initial complete&qu ...
- Odoo中连接mysql数据库
how to integrate Odoo with MySQL - Stack Overflowhttps://stackoverflow.com/questions/31959919/how-to ...
- Selenium IDE 宏 试用 一例
本质是宏(Macro)理念 Marco概念的广泛应用: 1.Office的Excel里的任何操作的 可以都可以用VBA编程记录下宏,然后把记录的宏,可以回放.当然也可以生成代码,比如给Excel设置单 ...
- 简单的数据库备份语句--Oracle+SQLSERVER
1. 工作中经常需要备份数据库表, 有时候经常需要baidu或者是询问,简单些一下.. 如果备份表不存在时: Oracle的语法 create table zhaobsh as select * fr ...
- 关于Laravel中使用response()方法调用json()返回数据unicode编码转换的问题解决
在网上找了好久没有找到,之后一步一步测试,发现了Laravel还是很强大的,解决方案如下: public function response(){ // 返回json数据 $data = [ 'err ...