Google Maps Android API V2 – infowindow on polyline?

I draw polylines on the map and I need to display some data to the user

How do I draw text or infowindow on each polyline?

I add a polyline like:

ArrayList<LatLng> points = null;
PolylineOptions lineOptions = null;
MarkerOptions markerOptions = new MarkerOptions();

// Traversing through all the routes
for(int i=0;i<result.size();i++){
    points = new ArrayList<LatLng>();
    lineOptions = new PolylineOptions();
    String color = colors[i % colors.length];
    // Fetching i-th route
    List<HashMap<String, String>> path = result.get(i);

    // Fetching all the points in i-th route
    for(int j=0;j<path.size();j++){
        HashMap<String,String> point = path.get(j);

        double lat = Double.parseDouble(point.get("lat"));
        double lng = Double.parseDouble(point.get("lng"));
        LatLng position = new LatLng(lat, lng);

        points.add(position);
    }

    // Adding all the points in the route to LineOptions
    lineOptions.addAll(points);
    lineOptions.width(5);
    lineOptions.color(Color.parseColor(color));

    // Drawing polyline in the Google Map for the i-th route
    mMap.addPolyline(lineOptions);
}

For example, I need to:

resolvent:

I do this by creating an invisible mark on the polyline and displaying its information window. For example:

//use a transparent 1px & 1px @R_686_2419@ as your marker
BitmapDescriptor transparent = BitmapDescriptorFactory.fromResource(R.drawable.transparent);
MarkerOptions options = new MarkerOptions()
                .position(new LatLng(someLatitide, someLongitude))
                .title(someTitle)
                .snippet(someSnippet)
                .icon(transparent)
                .anchor((float) 0.5, (float) 0.5); //puts the info window on the polyline

Marker marker = mMap.addMarker(options);

//open the marker's info window
marker.showInfoWindow();

General methods of updating to include touching the polyline and opening the information window: 1. Implement onmapcclicklistener 2. On the onmapclick event, determine whether the user has touched the polyline. I do this by storing the polyline points in the quadtree and searching the quadtree for the nearest point the user touches the screen. If the distance is within a certain threshold (i.e. close to the polyline), Then create the invisible tag referenced above and open its information window. If the touch is not within the threshold range, ignore the onmapclick event. 3. Delete the previously created invisible tag in the next onmapclick event, so that a pile of invisible tags will not occupy memory. Hope to help

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>