我在两个 之间的地图上画了一条线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);
}
}
}