2012-06-26 35 views
1

我试图在地图上的用户交互方面获取两个不同变量的源和目标。源和目的地都使用单一标记。在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)); 
    } 

回答

0

使用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

+0

位置类的方法我怎样才能pointB的GeoPoint在这种方法吗? – Aswin

+0

有没有解决这个问题的方法? – Aswin