【问题标题】:Drawing a line between two points android & google maps在两点之间画一条线 android & google maps
【发布时间】:2012-07-16 15:48:35
【问题描述】:

我在谷歌地图上工作,我想在两点之间画一条线我使用了本网站用户问题中的以下代码,但它对我不起作用我在删除此功能时强制关闭在应用程序的内部类中工作

但我需要它,因为我必须画线

我使用的代码如下:

    class MyOverlay extends com.google.android.maps.Overlay {
    GeoPoint [] geoPointsArray ;
// constructor 
public MyOverlay(){

    }   

@Override 
public void draw(Canvas canvas, MapView mapv, boolean shadow){
    super.draw(canvas, mapv, shadow);

    Paint   mPaint = new Paint();
    mPaint.setDither(true);
    mPaint.setColor(Color.RED);
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(2);

    GeoPoint gP1 = new GeoPoint(19240000,-99120000);
    GeoPoint gP2 = new GeoPoint(37423157, -122085008);

    Point p1 = new Point();
    Point p2 = new Point();
    Path path = new Path();

    projection.toPixels(gP1, p1);
    projection.toPixels(gP2, p2);

    path.moveTo(p2.x, p2.y);
    path.lineTo(p1.x,p1.y);

    canvas.drawPath(path, mPaint);
}

}//内部类结束

我真的需要帮助,当我刚刚添加这个东西时,我被强制关闭了:S

【问题讨论】:

  • 请看我的回答,如果您有任何疑问,请告诉我。

标签: android google-maps android-layout layout maps


【解决方案1】:

试试这个给你的叠加层添加一个或多个点并用红色填充它们。

public void draw(Canvas canvas, MapView mapView, boolean shadow) 
    {
        super.draw(canvas, mapView, shadow);

        Paint mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setStyle(Style.FILL_AND_STROKE);
        mPaint.setColor(Color.RED);
        mPaint.setAlpha(9);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(2);

        Path path = new Path();

        Projection projection = mapView.getProjection();

        for(int j = 0; j < geoArrayist.size(); j++) 
        {
            Iterator<GeoPoint> it = geoArrayist.iterator();
            while(it.hasNext()) 
            {
                GeoPoint arrayListGeoPoint = it.next();

                Point currentScreenPoint = new Point();
                projection.toPixels(arrayListGeoPoint, currentScreenPoint);

                if(j == 0)
                    path.moveTo(currentScreenPoint.x, currentScreenPoint.y); 
                else
                    path.lineTo(currentScreenPoint.x, currentScreenPoint.y);
            }                 
        }
        // old_geopoint = new_geopoint;
        canvas.drawPath(path, mPaint);
    }   

geoArrayList 是一个地理点列表。

【讨论】:

  • 我喜欢您的解决方案,但是当我添加此功能时仍然存在问题,应用程序停止工作并且我的手机出现黑屏...这很烦人,因为我 90% 确定代码正确
  • 确保在设备上测试它......并确保您在应用程序中包含正确的 api 密钥。最好的办法是为您的 mapView 重新生成 api 密钥并使用新密钥。有时会发生这种情况,我也发生过这种情况,只需输入一个新的 api 密钥就可以了。我不知道你到底哪里出错了..但这可能是原因之一
【解决方案2】:

使用下面的堆栈溢出答案在谷歌地图上的两点之间绘制路线,它可能会对你有所帮助。

Draw Route Path on Google Map

【讨论】:

  • 我使用了您的解决方案,但在以下代码行中出现错误,其中 toPixel 不适用于 (int i = 0; i
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
  • 2016-08-25
  • 1970-01-01
相关资源
最近更新 更多