我想点击不同的折线。我没有在android中看到任何有关折线的可点击事件。
我找到了map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
我只是得到折线LatLng
并在地图上搜索真实的点击坐标。
新的更改在代码中。
提前致谢。
代码:
//Polyline
private class GetRouteTask extends AsyncTask<String, Void, String> {
String response = "";
private ProgressDialog progressDialog;
public void onPreExecute()
{
progressDialog = new ProgressDialog(RazhodCenaGoogleMaps2.this);
progressDialog.setMessage("Calculating directions");
progressDialog.show();
}
@Override
protected String doInBackground(String... urls) {
//Get All Route values
v2GetRouteDirection = new GMapV2DirectionGoogle();
document = v2GetRouteDirection.getDocument(latLngFrom, latLngTo, GMapV2DirectionGoogle.MODE_DRIVING);
response = "Success";
return response;
}
@Override
protected void onPostExecute(String result) {
progressDialog.dismiss();
final PolylineOptions rectLine1 = new PolylineOptions().width(3).color(Color.RED).geodesic(true);
final PolylineOptions rectLine2 = new PolylineOptions().width(3).color(Color.GRAY).geodesic(true);
final PolylineOptions rectLine3 = new PolylineOptions().width(3).color(Color.GRAY).geodesic(true);
ArrayList<Integer> Count = v2GetRouteDirection.CountPoly(document);
for(j = 0; j < Routes.size(); j++ ) {
if (j == 0) {
ArrayList<LatLng> directionPoint1 = v2GetRouteDirection.getDirection1(document);
for (int i = 0; i < directionPoint1.size(); i++) {
LatLng point1 = directionPoint1.get(i);
rectLine1.add(point1);
}
// Adding route on the map
map.addPolyline(rectLine1);
}
if (j == 1) {
ArrayList<LatLng> directionPoint2 = v2GetRouteDirection.getDirection2(document);
for (int i = 0; i < directionPoint2.size(); i++) {
LatLng point2 = directionPoint2.get(i);
rectLine2.add(point2);
}
// Adding route on the map
map.addPolyline(rectLine2);
}
if (j == 2) {
ArrayList<LatLng> directionPoint3 = v2GetRouteDirection.getDirection3(document);
for (int i = 0; i < directionPoint3.size(); i++) {
LatLng point3 = directionPoint3.get(i);
rectLine3.add(point3);
}
// Adding route on the map
map.addPolyline(rectLine3);
}
//new code
final PolylineOptions rectLine11 = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
final PolylineOptions rectLine21 = new PolylineOptions().width(5).color(Color.GREEN).geodesic(true);
final PolylineOptions rectLine31 = new PolylineOptions().width(5).color(Color.BLACK).geodesic(true);
//searching for roads
ArrayList<Integer> Count1 = v2GetRouteDirection.CountPoly(document);
int j1;
int k = -1;
int p = 0;
int c = 0;
for(j1 = 0; j1 < Count1.size(); j1++ ) {
if (j1 == 0) {
k = 0;
}
if(j1 == 1){
p = 1;
}
if (j1 == 2) {
c = 2;
}
}
final int finalK = k;
final int finalC = c;
final int finalP = p;
//new code
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
latTouch1 = point.latitude;
lonTouch1 = point.longitude;
//Remove decimal from coordinates
DecimalFormat nf = new DecimalFormat("0.0");
String formattedLat = nf.format(latTouch1);
String formattedLon = nf.format(lonTouch1);
double latTouch = Double.parseDouble(formattedLat);
double lonTouch = Double.parseDouble(formattedLon);
LatLng touch = new LatLng(latTouch, lonTouch);
if (finalK == 0) {
ArrayList<LatLng> directionPoint11 = v2GetRouteDirection.getDirection1(document);
for (int i1 = 0; i1 < directionPoint11.size(); i1++) {
LatLng point11 = directionPoint11.get(i1);
Double latlng1 = point11.latitude;
Double latlng2 = point11.longitude;
String formattedLatLng1 = nf.format(latlng1);
String formattedLatLng2 = nf.format(latlng2);
double pointDouble1 = Double.parseDouble(formattedLatLng1);
double pointDouble2 = Double.parseDouble(formattedLatLng2);
LatLng point111 = new LatLng(pointDouble1, pointDouble2);
//check if poly is equal to the real coordinates
if (point111.equals(touch)) {
ArrayList<LatLng> directionPoint1 = v2GetRouteDirection.getDirection1(document);
for (int i = 0; i < directionPoint1.size(); i++) {
LatLng point1 = directionPoint1.get(i);
rectLine11.add(point1);
}
// Adding route on the map
map.addPolyline(rectLine11);
Toast.makeText(getApplicationContext(), "Polyline 1 founded", Toast.LENGTH_SHORT).show();
break;
}
}
}
if (finalP == 1) {
ArrayList<LatLng> directionPoint21 = v2GetRouteDirection.getDirection2(document);
for (int i1 = 0; i1 < directionPoint21.size(); i1++) {
LatLng point11 = directionPoint21.get(i1);
Double latlng1 = point11.latitude;
Double latlng2 = point11.longitude;
String formattedLatLng1 = nf.format(latlng1);
String formattedLatLng2 = nf.format(latlng2);
double pointDouble1 = Double.parseDouble(formattedLatLng1);
double pointDouble2 = Double.parseDouble(formattedLatLng2);
LatLng point111 = new LatLng(pointDouble1, pointDouble2);
if (point111.equals(touch)) {
ArrayList<LatLng> directionPoint2 = v2GetRouteDirection.getDirection2(document);
for (int i = 0; i < directionPoint2.size(); i++) {
LatLng point2 = directionPoint2.get(i);
rectLine21.add(point2);
}
// Adding route on the map
map.addPolyline(rectLine21);
Toast.makeText(getApplicationContext(), "Polyline 2 founded", Toast.LENGTH_SHORT).show();
break;
}
}
}
if (finalC == 2) {
ArrayList<LatLng> directionPoint31 = v2GetRouteDirection.getDirection3(document);
for (int i1 = 0; i1 < directionPoint31.size(); i1++) {
LatLng point11 = directionPoint31.get(i1);
Double latlng1 = point11.latitude;
Double latlng2 = point11.longitude;
String formattedLatLng1 = nf.format(latlng1);
String formattedLatLng2 = nf.format(latlng2);
double pointDouble1 = Double.parseDouble(formattedLatLng1);
double pointDouble2 = Double.parseDouble(formattedLatLng2);
LatLng point111 = new LatLng(pointDouble1, pointDouble2);
if (point111.equals(touch)) {
ArrayList<LatLng> directionPoint3 = v2GetRouteDirection.getDirection3(document);
for (int i = 0; i < directionPoint3.size(); i++) {
LatLng point3 = directionPoint3.get(i);
rectLine31.add(point3);
}
// Adding route on the map
map.addPolyline(rectLine31);
Toast.makeText(getApplicationContext(), "Polyline 3 founded", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
});
}
}