Android 无法将源和目标地质点获取到不同的变量中
我试图在两个不同的变量中获得源和目的地,这两个变量与地图上的用户交互有关。对源和目标使用单个标记。在Android 无法将源和目标地质点获取到不同的变量中,android,maps,android-mapview,Android,Maps,Android Mapview,我试图在两个不同的变量中获得源和目的地,这两个变量与地图上的用户交互有关。对源和目标使用单个标记。在onTouchEvent方法中,我无法区分源和目标 使用这些点,我想计算距离,并通过用户交互在地图上动态绘制路线 这是我的密码 public boolean onTouchEvent(MotionEvent event, MapView mapView) { final int action=event.getAction(); final int x=(int)e
onTouchEvent
方法中,我无法区分源和目标
使用这些点,我想计算距离,并通过用户交互在地图上动态绘制路线
这是我的密码
public boolean onTouchEvent(MotionEvent event, MapView mapView) {
final int action=event.getAction();
final int x=(int)event.getX();
final int y=(int)event.getY();
boolean result=false;
if (action==MotionEvent.ACTION_DOWN) {
for (OverlayItem item : items) {
Point p=new Point(0,0);
map.getProjection().toPixels(item.getPoint(), p);
if (hitTest(item, marker, x-p.x, y-p.y)) {
result=true;
inDrag=item;
items.remove(inDrag);
populate();
xDragTouchOffset=0;
yDragTouchOffset=0;
setDragImagePosition(p.x, p.y);
dragImage.setVisibility(View.VISIBLE);
xDragTouchOffset=x-p.x;
yDragTouchOffset=y-p.y;
break;
}
}
}
else if (action==MotionEvent.ACTION_MOVE && inDrag!=null) {
setDragImagePosition(x, y);
result=true;
}
else if (action==MotionEvent.ACTION_UP && inDrag!=null) {
dragImage.setVisibility(View.GONE);
GeoPoint pt=map.getProjection().fromPixels(x-xDragTouchOffset,
y-yDragTouchOffset);
slatlng[0] = pt.getLatitudeE6()/1E6;
slatlng[1] = pt.getLongitudeE6()/1E6;
urlString = getUrl(slatlng[0], slatlng[1], dlatlng[0],dlatlng[1]);
try {
distance = getDistance(urlString);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Distance : "+distance);
System.out.println("Lat : " +slatlng[0]+"Lng :"+slatlng[1]);
OverlayItem toDrop=new OverlayItem(pt, inDrag.getTitle(),
inDrag.getSnippet());
items.add(toDrop);
populate();
inDrag=null;
result=true;
}
return(result || super.onTouchEvent(event, mapView));
}
使用Location类解决您的问题
Location locationA = new Location("point A");
locationA.setLatitude(pointA.getLatitudeE6() / 1E6);
locationA.setLongitude(pointB.getLongitudeE6() / 1E6);
Location locationB = new Location("point B");
locationB.setLatitude(pointB.getLatitudeE6() / 1E6);
locationB.setLongitude(pointB.getLongitudeE6() / 1E6);
double distance = locationA.distanceTo(locationB);
检查位于的Location类方法是否有此方法的解决方案??