发现如果当前位置是沿一条直线,在地图上的两个点之间绘制。 [英] Find if the current location is along a straight line, drawn between two points on a map.

查看:102
本文介绍了发现如果当前位置是沿一条直线,在地图上的两个点之间绘制。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得出两个 GeoPoints 之间的地图上线,我已经检索到的当前位置。我想知道,如果用户的当前位置是沿着画线或不?

如果我得到画线的纬度和经度,我怎么可以检查用户是在绘制路径?

我用低于code绘制地理点之间的行

 进口的java.util.List;进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.DashPathEffect;
进口android.graphics.Paint;
进口android.graphics.Paint.Cap;
进口android.graphics.Paint.Style;
进口android.graphics.Point;进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.MapView;
进口com.google.android.maps.Overlay;公共类RouteSegmentOverlay扩展覆盖{
    私人的GeoPoint locpoint;
    私人涂料粉刷;
    //私人的GeoPoint路线点[];
    私人列表<&GeoPoint的GT;路线点;
    私人列表<整数GT; routeMode;
    私人布尔routeIsActive;
    私人POLD点,Pnew最多,PP;
    私人诠释numberRoutePoints;    //构造函数允许路线数组作为参数传递。
    公共RouteSegmentOverlay(列表<&GeoPoint的GT;路线点,列表与LT;整数GT; routeMode)
    {
        this.routePoints =路线点;
        this.routeMode = routeMode;
        numberRoutePoints = routePoints.size();
        routeIsActive = TRUE;
        //如果第一次设置的初始位置开始的路线
        locpoint = routePoints.get(0);
        POLD =新点(0,0);
        Pnew最多=新的点(0,0);
        第=新点(0,0);
        油漆=新的油漆();
    }    //方法打开路线显示和关闭
    公共无效setRouteView(布尔routeIsActive){
        this.routeIsActive = routeIsActive;
    }    @覆盖
    公共无效画(油画画布,MapView类MapView类,布尔阴影){
        super.draw(画布,MapView类,阴影);
        如果(routeIsActive!)回报;        MapView.getProjection()在toPixels(locpoint,PP)。 //转换的GeoPoint屏幕像素        INT XOFF = 0;
        INT YOFF = 0;
        INT oldx = pp.x;
        INT oldy = pp.y;
        INT下一页末= oldx + XOFF;
        INT newy指定= oldy + YOFF;        paint.setAntiAlias​​(真);
        paint.setDither(真);
        paint.setStrokeCap​​(Cap.ROUND);
        paint.setStrokeWidth(7);
            paint.setColor(Color.parseColor(#666666));        的for(int i = 0; I< numberRoutePoints-1;我++)
        {
            MapView.getProjection()在toPixels(routePoints.get(ⅰ),POLD)。
            oldx = pold.x;
            oldy = pold.y;
            。MapView.getProjection()在toPixels(routePoints.get第(i + 1),Pnew最多);
            下一页末= pnew.x;
            newy指定= pnew.y;            canvas.drawLine(oldx,oldy,下一页末,newy指定,油漆);
        }
    }
}


解决方案

是您的关心寻找是否上线用户移动或不?如果是这样,可以使用斜率和检查该用户是在两个端点之间它做

您可以通过检查用户坐标的两个线路端点之间的谎言检查与介。你可以用下面的公式找到直线的斜率:


  

斜率=(Y2-Y1)/(X2-X1)


首先计算你的直线的斜率,然后计算出用户的当前位置和你的线的两个端点之一的斜率。如果这两个条件满足则用户就行了如果没有的话用户不就行了。

I've draw a line on a map between two GeoPoints, and I have retrieved the current location. I want to know if the user's current position is along the drawn line or not?

If I get the latitude and longitude of the drawn line, how can I check the user is in the drawing path?

I've used below code to draw a line between geo points

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);
        }
    }
}

解决方案

Is your concern to find whether the user moves on line or not? If so you can do it using slope and checking that the user is in between the two end points.

You can check betweeness by checking that user coordinates lies between your two line end points. You can find slope of line using below formula:

slope=(y2-y1)/(x2-x1)

First calculate the slope of your line and then calculate the slope between the user's current location and one of the two end points of your line. If the both condition satisfies then user is on the line if not then user is not on line.

这篇关于发现如果当前位置是沿一条直线,在地图上的两个点之间绘制。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆