Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Touch detection on polyline in Google Maps Android API v2

I want to implement a touchListener on a polyline displayed with Google Maps V2 Android API.

Zoom level: CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(lat_Lng, 5);

I tried the following polyline touch code:

boolean onpoly = false;
            for (Polyline polyline : mPolylines) {
                for (LatLng polyCoords : polyline.getPoints()) {
                    float[] results = new float[1];

                    Location.distanceBetween(points.latitude, points.longitude, polyCoords.latitude, polyCoords.longitude, results);

                    if (results[0] < 100 ) {
                        onpoly = true;

                        Marker mark = map.addMarker(new MarkerOptions().position(points).title("AddEvent")

                        .snippet("" + addressaddexpense).icon(BitmapDescriptorFactory.fromResource(R.drawable.addicon)));

                        UtilsClass.dropPinEffect(mark);
                    }// end if..
                } // end for loop
            }// end for loop

            if (onpoly == true) {
                Toast.makeText(getActivity(), "Poly line detected", Toast.LENGTH_SHORT).show();
            }// end if

It worked but not perfectly. it will not detect the touch event unless i zoom in, sometimes forcing me to tap the map more than 5 times before zooming to achieve detection.

I then changed the aforementioned if condition from if (results[0] < 100 ) to if (results[0] < 150 * (22 - map.getCameraPosition().zoom)) and the functionality improved but it still does not work all the time.

Note: I want to detect polyline touch at any zoom level.

like image 328
harikrishnan Avatar asked Apr 11 '15 06:04

harikrishnan


1 Answers

try this

    final List<LatLng> latLngList; // Extract polyline coordinates and put them on this list
    private GoogleMap map;

for(int i = 0; i < latLngList.size(); i++){
                MarkerOptions mar = new MarkerOptions();
                    mar.position(new LatLng(latLngList.get(i).latitude, latLngList.get(i).longitude)).icon(BitmapDescriptorFactory.fromResource(R.drawable.trasparent_image)); //this image should be very small in size and transparent
                    map.addMarker(mar);
            }

            map.setOnMarkerClickListener(new OnMarkerClickListener() {

                @Override
                public boolean onMarkerClick(Marker arg0) {
                    for(int i = 0; i < latLngList.size(); i++){
                    if(latLngList.get(i).latitude == arg0.getPosition().latitude && latLngList.get(i).longitude == arg0.getPosition().longitude){
                        Toast.makeText(MainActivity.this, "PolyLineClick ", Toast.LENGTH_SHORT).show();
                        break;
                    }
                }
                    return false;
                }
            });
like image 163
Abhishek Avatar answered Sep 30 '22 04:09

Abhishek