zoukankan      html  css  js  c++  java
  • 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);
     }
    }

    运行的效果图:

  • 相关阅读:
    每日一篇文献:Robotic pick-and-place of novel objects in clutter with multi-affordance grasping and cross-domain image matching
    每日一篇文献:Intuitive Bare-Hand Teleoperation of a Robotic Manipulator Using Virtual Reality and Leap Motion
    每日一篇文献:Virtual Kinesthetic Teaching for Bimanual Telemanipulation
    HEBI Robotic Arm VR Teleoperation
    「iQuotient Case」AR device teleoperated robotic arm
    VR and Digital Twin Based Teleoperation of Robotic Arm
    HEBI Robotic Arm VR Teleoperation
    Human Robot Interaction
    Immersive Teleoperation Project
    机器人演示学习
  • 原文地址:https://www.cnblogs.com/bigben0123/p/4184030.html
Copyright © 2011-2022 走看看