0
好的,我已經使用谷歌,並沒有找到任何解決我的問題,我有一個MapView
多個多邊形和一切正確顯示在地圖上。我試圖做的是添加一個onTap
以顯示有關該多邊形的信息,但是我無法識別多邊形上的多邊形。以下是我目前擁有的。有人能告訴我在onTap
部分我做錯了什麼嗎?Android多邊形覆蓋ontap
public class Polygon extends Overlay {
ArrayList<GeoPoint> geoPoints;
static ArrayList<String> custCount;
static ArrayList<String> hexCode;
static ArrayList<String> custMin;
static ArrayList<String> custMax;
static String tester;
Point firstPoint;
static List<HashMap<String, String>> colorRanges;
private Path path;
public Polygon(ArrayList<GeoPoint> points){
geoPoints = points;
}
public class setColorRanges{
public setColorRanges(List<HashMap<String, String>> colorData) {
colorRanges = colorData;
}
}
public class CustCount{
public CustCount(ArrayList<String> str) {
custCount = str;
}
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow){
//Set the color and style
Paint paint = new Paint();
Paint paint1 = new Paint();
//Create path and add points
int origin = 0;
for(int i = 0; i < geoPoints.size()/5;i++){
path = new Path();
firstPoint = new Point();
mapView.getProjection().toPixels(geoPoints.get(origin), firstPoint);
path.moveTo(firstPoint.x, firstPoint.y);
for(int j=0; j<5; j++) {
Point nextPoint = new Point();
mapView.getProjection().toPixels(geoPoints.get(origin+j), nextPoint);
path.lineTo(nextPoint.x, nextPoint.y);
}
//loop thru array of color ranges
int curCustCount = Integer.valueOf(custCount.get(i));
String curColor = "";
for(int z=0; z<colorRanges.size(); z++) {
int custmin = Integer.valueOf(colorRanges.get(z).get("custmin"));
int custmax = Integer.valueOf(colorRanges.get(z).get("custmax"));
if(curCustCount >= custmin && curCustCount <= custmax) {
curColor = colorRanges.get(z).get("hexcode");
break;
}
}
paint.setColor(Color.parseColor(curColor));
paint.setAlpha(70);
paint.setStyle(Paint.Style.FILL);
paint1.setColor(Color.parseColor(curColor));
paint1.setAlpha(100);
paint1.setStyle(Paint.Style.STROKE);
path.lineTo(firstPoint.x, firstPoint.y);
path.setLastPoint(firstPoint.x, firstPoint.y);
canvas.drawPath(path, paint);
canvas.drawPath(path,paint1);
origin += 5;
}
super.draw(canvas, mapView, shadow);
}
@Override
public boolean onTap(GeoPoint geoPoint, MapView mapView) {
RectF rectF = new RectF();
path.computeBounds(rectF, true);
Region region = new Region();
region.setPath(path, new Region((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));
Point point = new Point();
mapView.getProjection().toPixels(geoPoint, point);
if (region.contains(point.x, point.y)) {
Log.d("onTap", point.x+" "+point.y);
}
return super.onTap(geoPoint, mapView);
}
}