我的应用中有一张地图,其上有圆圈,如图中所示,三张png图像A,B和C. 我希望突出显示其中一个图像,当用户精确打算点击它们以执行与它们相关的某些操作时。 为了达到这个目的,我使用了一种非常粗略的方法
for(int i = 0; i < vectorPts.size(); i ++){
dest_location.setLatitude(vectorPts.get(i).x);
dest_location.setLongitude(vectorPts.get(i).y);
double distance = (double) mylocation.distanceTo(dest_location);
distanceCollect.add(distance);
int minIndex = distanceCollect.indexOf(Collections.min(distanceCollect));
System.out.println("The value >> " + minIndex + DSS.activeRoute);
indexUnderEdit = minIndex;
}
点的矢量矢量点具有所有点坐标。我使用distanceTo方法来查找点击点和图像位置之间的距离。点击点和实际图像位置之间的最小距离意味着用户想要点击该图像是我的假设。但这是一个微弱的应用程序,因为任何地图上的任何位置(远离任何图像)也将被考虑,并且将选择最短距离。您能否建议我另一种方法来查找用户点击的特定图像。
@Override
public void drawRoute(Canvas canvas,String routeName,MapActivity mact) {
// TODO Auto-generated method stub
if((routeName == null || (routeName.equals(PROJ.currentRoute.activeRouteName)))){
if (cpts.size() > 0)
{
if (this.wktType == WKTType.ePolyLine || this.wktType == WKTType.ePolyLineM)
{
if ( mPath != null)
canvas.drawPath(mPath, mPen);
}
// draw point icons
Resources Res = PROJ.getInstance().getResources();
int i = 0;
for(; i < cpts.size(); i++)
{
if(i == 0)
{
// Draw start point //
if(this.angleRecieved >= 0){
Bitmap img = BitmapFactory.decodeResource(Res, R.drawable.startpoint);
canvas.drawBitmap( img, cpts.get(i).x - img.getWidth()+2, cpts.get(i).y - (img.getHeight()/2) , null);
}
else{
Bitmap img = BitmapFactory.decodeResource(Res, R.drawable.startpoint_inv);
canvas.drawBitmap( img, cpts.get(i).x-3 , cpts.get(i).y - (img.getHeight()/2), null);
}
}
else if((i == cpts.size() - 1)&&(bIsFinished == true))
{
// Draw end point
if(this.angleRecievedFinish >= 0){
Bitmap img = BitmapFactory.decodeResource(Res,R.drawable.finishpoint);
canvas.drawBitmap( img, cpts.get(i).x - img.getWidth()+2, cpts.get(i).y - (img.getHeight()/2), null);
}
else{
Bitmap img = BitmapFactory.decodeResource(Res, R.drawable.finishpoint_inv);
canvas.drawBitmap( img, cpts.get(i).x-3 , cpts.get(i).y - (img.getHeight()/2), null);
}
if(!bIsSaved){
Intent intent = new Intent();
intent.setAction(Constants.CUSTOM_NEWMESSAGE_INTENT);
intent.putExtra(Constants.EVENT_STRING, Constants.mapEvents.ROUTE_FINISHED);
PROJ.getCurrentApplicationContext().sendBroadcast(intent);
bIsSaved = true;
}
}
else
//Draw intermediate points from 1 to 10
{
if (i ==1){
this.forFirstIteration = this.angleRecieved;
}
if(i == 10){
this.lastNotifier = true;
}
int wpResId = getWPResourceId(i);
Bitmap img = BitmapFactory.decodeResource(Res, wpResId);
canvas.drawBitmap( img, cpts.get(i).x - img.getWidth()/2, cpts.get(i).y - img.getWidth()/2, null);
}
}
}
}
}