【问题标题】:Android polylines maps apiAndroid折线地图api
【发布时间】:2011-04-14 18:19:00
【问题描述】:

好吧,我是 android 的初学者,我需要更具体地在设备上使用地图(折线)我需要做这样的事情。

这是一个网络应用程序,我用来追踪我所在城市的公交路线和公交车站,我被要求在 android 中做同样的事情!我一直在检查 android 的 maps api 并没有在 JS api 中找到类似于 polyline 的任何东西,有没有办法实现这一点?

我在添加简单的叠加层时没有问题我一直在查看 android 开发者网站中的基本教程,但我不知道如何绘制折线。

【问题讨论】:

    标签: android maps polyline


    【解决方案1】:

    Android Google Maps API 中没有这样的 API。您只能先列出要绘制的路线的实际 GeoPoints,然后在 Overlay 对象上绘制点和线。没有简单的方法可以做到这一点。

    【讨论】:

      【解决方案2】:

      一种更简单的方法是获取您的点并扩展将显示您的图像的 ImageView 以绘制点,而不是只需要传递您想要绘制的点。

      在我的项目中,我这样做了:

      public class ImageDraw extends ImageView{
      private Paint   mPaint = new Paint();
      List<Point> pts = new ArrayList<Point>() ;
      
      public ImageDraw(Context context) {
          super(context);
      
      }
      //used to send the location of the points to draw on the screen
      //must be called before every redraw to update the points on the screen
      public void SetPointsToDraw(List<Point> pts)
      {
          this.pts = pts;
      }
      
      
      public ImageDraw(Context context, AttributeSet attrs)
      {
          super(context,attrs);
      }
      public ImageDraw(Context context, AttributeSet attrs, int defStyle)
      {
          super(context, attrs, defStyle);
      }
      
      @Override
      public void onDraw(Canvas canvas)
      {
          super.onDraw(canvas);
      
          Paint paintColor = mPaint;
          paintColor.setColor(Color.YELLOW);
          paintColor.setStrokeWidth(3);
      
      
          if(pts.size() > 0)
          {
              canvas.drawCircle(pts.get(0).x, pts.get(0).y, 7, paintColor);   
          }
          if (pts.size() > 1)
          {
              for (int i = 1 ; i < pts.size(); i++) {
                  paintColor.setColor(Color.YELLOW);
                  canvas.drawCircle(pts.get(i).x, pts.get(i).y, 7, paintColor);
                  paintColor.setColor(Color.RED);
                  canvas.drawLine(pts.get(i-1).x, pts.get(i-1).y, pts.get(i).x, pts.get(i).y, paintColor);
              }
          }
      
      
      }
      

      }

      当您扩展 Imageview 并使用 xml 创建布局时,不要忘记将您的新小部件的整个包放入,例如: com.Myapp.MyImageView

      【讨论】:

        【解决方案3】:

        FvZ的回答有效但不是原生方式,地图上有折线,一个简单的例子 https://stackoverflow.com/a/21877742/618419

        还可以查看 Android 文档,它们有许多简单且完整的示例: http://developer.android.com/reference/com/google/android/gms/maps/model/Polyline.html

        【讨论】:

          猜你喜欢
          • 2013-01-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-02
          • 1970-01-01
          • 2015-04-25
          • 1970-01-01
          • 2017-01-25
          相关资源
          最近更新 更多