我在两个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);
}
}
}
答案 0 :(得分:4)
您是否关注用户是否在线上线?如果是这样,你可以使用斜率并检查用户是否位于两个端点之间。
您可以通过检查用户坐标位于两个线端点之间来检查betweeness。您可以使用以下公式找到线的斜率:
斜率=(Y2-Y1)/(X2-X1)
首先计算线的斜率,然后计算用户当前位置与线的两个端点之一之间的斜率。如果两个条件都满足,那么如果没有用户在线,则用户不在线。
修改强>:
您可以使用
Google Maps Android API Utility库。使用PolyUtil.isLocationOnPath(LatLng point, List<LatLng> polyline, boolean geodesic, double tolerance)
答案 1 :(得分:1)
答案 2 :(得分: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类获得beter性能...所有最佳