4

I have drawn polylines for different points as in this image. image1
CENTER VIEW :- (CIRCLE) Code for **MARKER**:

viewIntermediateMarker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.layout_intermediate_marker, null);

middlemarker = map.addMarker(new MarkerOptions()
                    .position(MIDDLE)
                    .snippet(String.valueOf(MIDDLE))
                    .icon(BitmapDescriptorFactory
                            .fromBitmap(SearchNearByCourseActivity
                                    .createDrawableFromView(mContext,
                                            viewIntermediateMarker))));

Polyline (White color):

poly_TToMid = map.addPolyline(new PolylineOptions()
                        .add(MIDDLE_MARKER, TEE_POINT).width(3)
                        .color(Color.WHITE));

Polyline (Green color):

    poly_MidToFlage = map.addPolyline(new PolylineOptions()
                        .add(MIDDLE_MARKER, upperSquMidLat).width(3)
                        .color(Color.GREEN));

But what i want to make my image 1 as of image 2 i.e. I want to Remove(trim) the polyline (Green & white) inside center of Circle as shown in figure below. image2

Please suggest some answers how to achieve this in android Google map v2 API. Any help is Appreciated...

The Heist
  • 1,444
  • 1
  • 16
  • 32

1 Answers1

0

This is quite a question and it's not that easy to implement. At least I can't think of any easy ways to do it.

The main reason behind the difficulty of this is the changing zoom level. At different zoom levels the polylines underneath marker are at different positions, because the map meter/pixel density changes.

One way to accomplish this would be:

Calculate the radius of the marker and get the radius in meters based on the current zoom level. Then loop through the polyline points and check the distance like this:

List<LatLng> points = polyline.getPoints();

LatLng latLng = points.get(0);
// Get the starting point's lat lng coordinates
Location startLoc = new Location("Test");
startLoc.setLatitude(latLng.latitude);
startLoc.setLongitude(latLng.longitude);

for (int i=1; i<points.size(); i++) {
    Location loc = new Location("Test");
    loc.setLatitude(points.get(i).latitude);
    loc.setLongitude(points.get(i).longitude);

    float distance = startLoc.distanceTo(loc);

    if (distance >= radiusInMeters) {
        polyline.setPoints(points.subList(i, points.size()));
        break;
    }
}

This won't however be too accurate (I've tried it), as the distanceTo returns average values and radiusInMeters can't be too accurate either. Nor will it be a great performer, as it has to loop through each point and calculate the distance (and do that for every marker).

Another method would be to get the color(s) behind the marker and use it as a background. For that you can use the map snapshot ability and get the part of it that you need for your markers.

List<LatLng> points = polyline.getPoints();
final LatLng latLng = points.get(0);

mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
    @Override
    public void onMapLoaded() {
        mMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
            @Override
            public void onSnapshotReady(Bitmap bitmap) {
                if (bitmap == null) {
                    Log.e(TAG, "Error! Snapshot couldn't be taken.");
                    return;
                }

                // Get the screen coordinates
                Point point = mMap.getProjection().toScreenLocation(latLng);
                if (point == null || point.x <= 0 || point.y <= 0) {
                    Log.e(TAG, "Error! Couldn't get the lat-lng coordinates.");
                    return;
                }

                int color = bitmap.getPixel(point.x, point.y);

                // Draw your marker using `color` as the background
            }
        });
    }
});

You can also use a more advanced method getPixels on the snapshot and get pieces of the background.

Notes:

  • Both of these methods will require you to re-draw your markers and/or polylines when the zoom level changes, so if you have a lot of markers my advice is that you rethink your strategy.
  • Perhaps instead use a solid background marker or draw your polylines differently in the first place.
Simas
  • 43,548
  • 10
  • 88
  • 116