2

我在两个 之间的地图上画了一条线GeoPoints,并检索了当前位置。我想知道用户的当前位置是否沿着画线?

如果我得到绘制线的纬度和经度,我如何检查用户是否在绘图路径中?

我用下面的代码在地理点之间画了一条线

import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Style;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class RouteSegmentOverlay extends Overlay {
    private GeoPoint locpoint;
    private Paint paint;
    // private GeoPoint routePoints [];
    private List<GeoPoint> routePoints;
    private List<Integer> routeMode;
    private boolean routeIsActive;  
    private Point pold, pnew, pp;
    private int numberRoutePoints;

    // Constructor permitting the route array to be passed as an argument.
    public RouteSegmentOverlay(List<GeoPoint> routePoints, List<Integer> routeMode) 
    {
        this.routePoints = routePoints;
        this.routeMode = routeMode;
        numberRoutePoints  = routePoints.size();
        routeIsActive = true;
        // If first time, set initial location to start of route
        locpoint = routePoints.get(0);
        pold = new Point(0, 0);
        pnew = new Point(0,0);
        pp = new Point(0,0);
        paint = new Paint();
    }

    // Method to turn route display on and off
    public void setRouteView(boolean routeIsActive){
        this.routeIsActive = routeIsActive;
    }

    @Override
    public void draw(Canvas canvas, MapView mapview, boolean shadow) {
        super.draw(canvas, mapview, shadow);
        if(! routeIsActive) return;

        mapview.getProjection().toPixels(locpoint, pp);       // Converts GeoPoint to screen pixels

        int xoff = 0;
        int yoff = 0;
        int oldx = pp.x;
        int oldy = pp.y;
        int newx = oldx + xoff;
        int newy = oldy + yoff;

        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStrokeCap(Cap.ROUND);
        paint.setStrokeWidth(7);
            paint.setColor(Color.parseColor("#666666"));

        for(int i=0; i<numberRoutePoints-1; i++)
        {
            mapview.getProjection().toPixels(routePoints.get(i), pold);
            oldx = pold.x;
            oldy = pold.y;
            mapview.getProjection().toPixels(routePoints.get(i+1), pnew);
            newx = pnew.x;
            newy = pnew.y;

            canvas.drawLine(oldx, oldy, newx, newy, paint);
        }
    }
}
4

3 回答 3

4

您是否关心用户是否在线?如果是这样,您可以使用斜率并检查用户是否位于两个端点之间。

您可以通过检查用户坐标是否位于两条线端点之间来检查中间值。您可以使用以下公式找到线的斜率:

斜率=(y2-y1)/(x2-x1)

首先计算你的线的斜率,然后计算用户当前位置和你的线的两个端点之一之间的斜率。如果两个条件都满足,则用户在线,如果不满足,则用户未在线。

编辑

您可以使用 Google Maps Android API 实用程序库。利用PolyUtil.isLocationOnPath(LatLng point, List<LatLng> polyline, boolean geodesic, double tolerance)

于 2012-08-01T12:18:03.127 回答
2

你数学有多好?

基本上,您可以计算路径上每两个点之间的线的方程式,然后计算线与用户位置之间的距离,如果该距离足够小,您可以声称他在该路径上

于 2012-08-01T11:56:27.357 回答
0
    import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private Context mContext;
    public GeoPoint p1, p2 = null;
    int mode;
    Drawable drawable1;
    private Paint paint;
    private List<GeoPoint> points1;
    ArrayList<GeoPoint> middleGeoList1;

    public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }

    public HelloItemizedOverlay(GeoPoint gp1, GeoPoint gp2, Drawable drawable,
            List<GeoPoint> poly) {
        super(boundCenterBottom(drawable));
        // TODO Auto-generated constructor stub
        this.p1 = gp1;
        this.p2 = gp2;
        this.points1 = poly;
        this.drawable1 = drawable;  
        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setAlpha(150);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeWidth(5);
        populate();
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    public void removeOverlay(OverlayItem overlay) {

        mOverlays.remove(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        return mOverlays.size();
    }

    @Override
    protected boolean onTap(int index) {
        OverlayItem item = mOverlays.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        return true;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {

        if (!shadow) {

            Projection projection = mapView.getProjection();
            if (points1 != null && points1.size() >= 2) {
                Point start = new Point();
                projection.toPixels(points1.get(0), start);
                for (int i = 1; i < points1.size(); i++) {
                    Point end = new Point();
                    projection.toPixels(points1.get(i), end);
                    canvas.drawLine(start.x, start.y, end.x, end.y, paint);
                    start = end;
                }

            } 
        }

        return super.draw(canvas, mapView, shadow, when);
    }
}

它是我的应用程序中的一个工作示例......使用 ItemizedOverlay 类以获得更好的性能......一切顺利

于 2012-08-01T12:16:35.153 回答