[日期: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教程:在百度地图上画出轨迹的更多相关文章

  1. Android中Google地图路径导航,使用mapfragment地图上画出线路(google map api v2)详解

    在这篇里我们只聊怎么在android中google map api v2地图上画出路径导航,用mapfragment而不是mapview,至于怎么去申请key,manifest.xml中加入的权限,系 ...

  2. GPS坐标显示在百度地图上(Qt+百度地图)

    Qt在5.6以后的版本就不支持webview控件了,这里我用的是Qt5.4的版本,里面还有这个控件: 下面简单介绍下Qt与html中的javascript调用交互过程: 一.整体实现介绍 在html中 ...

  3. 从POI到O2O 看百度地图如何走出未来之路

    近期O2O的烧钱融资大战如火如荼,有人已经把O2O大战,用乌合之众的群体心理失控来形容.其实厂商都不傻,O2O烧钱大家都知道,但是大家还知道O2O背后这块大蛋糕价值"万亿级". 有 ...

  4. [置顶] Xamarin android如何调用百度地图入门示例(一)

    在Xamarin android如何调用百度地图呢? 首先我们要区分清楚,百度地图这是一个广泛的概念,很多刚刚接触这个名词"百度地图api",的确是泛泛而谈,我们来看一下百度地图的 ...

  5. Bing必应地图中国API - 在地图上画圆

    Bing必应地图中国API - 在地图上画圆 2011-05-24 14:49:37|  分类: Bing&Google|字号 订阅     <变形金刚2>上映4日国内票房过亿,基 ...

  6. Android Studio使用百度地图示例BaiduMapsApiASDemo

    Android Studio使用百度地图示例BaiduMapsApiASDemo 用自己AVD下的debug.keystore替换掉项目中的debug.keystore 生成自己的签名 同样的方法生成 ...

  7. ArcGIS API for JavaScript根据两个点坐标在地图上画线

    ArcGIS API for JavaScript根据两个点坐标在地图上画线比如说a(xxxx,xxxxx),b(xxxx,xxxxx).利用这两个点画一条线 var polyline = new e ...

  8. 使用JavaScript在Canvas上画出一片星空

    随着Html5的迅猛发展,画布也变得越来越重要.下面我就写一个关于在canvas上画出一片星空的简单的代码的示例. 理论基础 初始化一个canvas,获得一个用于绘制图形的上下文环境context.并 ...

  9. 【地图功能开发系列:二】根据地址名称通过百度地图API查询出坐标

    根据地址名称通过百度地图API查询出坐标 百度地图ApiUrl string url = "http://api.map.baidu.com/geocoder?address={0}& ...

随机推荐

  1. 学习ML.NET(1): 构建流水线

    ML.NET使用LearningPipeline类定义执行期望的机器学习任务所需的步骤,让机器学习的流程变得直观. 下面用鸢尾花瓣预测快速入门的示例代码讲解流水线是如何工作的. using Micro ...

  2. SpringBoot日记——任务处理 之 异步、定时、邮件

    ---恢复内容开始--- 直接步入正题. 异步任务 异步任务比较简单,只需要两个注解就可以搞定,我们直接来看如何使用: 1.创建一个service,带上@EnableAsync,就是开启异步任务的注解 ...

  3. json中获取key值

    <script type="text/javascript"> getJson('age'); function getJson(key){ var jsonObj={ ...

  4. Python运算符-4

    #and or not #优先级,()> not > and > or print(2 > 1 and 1 < 4) print(2 > 1 and 1 < ...

  5. 个人博客week2

    1. 是否需要有代码规范 对于是否需要有代码规范,请考虑下列论点并反驳/支持: 这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. 我是个艺术家,手艺人,我有自己的规 ...

  6. BUAAMOOC项目终审报告

    工作总结 我们是歪果仁带你灰开发团队.我们开发的项目是北航学堂(MOOC)的android客户端:BUAAMOOC. 目前我们完成了主要功能,包括UI设计,视频播放,视频下载,学习进度,个人信息等功能 ...

  7. Linux内核分析 期末总结

    Linux内核分析 期末总结 一.知识概要 1. 计算机是如何工作的 存储程序计算机工作模型:冯诺依曼体系结构 X86汇编基础 会变一个简单的C程序分析其汇编指令执行过程 2. 操作系统是如何工作的 ...

  8. 同步手绘板——android端取色

    作为绘图软件,颜色的选取必不可少,在刚开始取色时,所选颜色和显示颜色始终不一致,比如选取白色显示绿色,在这个问题上消耗了太多的时间,后来发现是比例问题,通过修改实现恰当的取色.

  9. my项目的总结2015.8.26编

    这已经是上上个星期的事了,现在回顾一下: 负责的模块是"my",更精准的说应该是my里面的个人信息管理 由于项目分域,模块已经分好了,涉及到的只是在现有的基础上解决分域后遗留的历史 ...

  10. 4-Python3从入门到实战—基础之数据类型(字符串-String)

    Python从入门到实战系列--目录 字符串表示 在 Python 3版本中,字符串是以 Unicode 编码的:Python 中使用 ' '或者" "表示字符串 msg = 'H ...