Google map Android API v2 - InfoWindow on polyline?

。_饼干妹妹 提交于 2019-12-13 13:07:42

问题


I am drawing polyline on my map,I need to show some data to user now.

How I can draw text or InfoWindow on each polyline ?

I add 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 this :


回答1:


I do this by creating an invisible marker on the polyline and then showing its info window. For example:

//use a transparent 1px & 1px box 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();

Update to include general approach to touching the polyline and opening an info window: 1. Implement an OnMapClickListener 2. On an onMapClick event, determine whether the user has touched the polyline. I do this by storing the polyline points in a quadtree and searching the quadtree for the closest point from where the user touched the screen. If the distance is within a certain threshold (i.e. close to the polyline), create the invisible marker referenced above and open its info window. If the touch is not within the threshold, ignore the onMapClick event. 3. On the next onMapClick event delete the previously created invisible marker so you do not have a bunch of invisible markers taking up memory. Hope that helps.




回答2:


Use Google Direction API to get the route and draw the polyline

Add OnPolylineClickListener in Map and get the reference using getTag

mMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() {
    @Override
    public void onPolylineClick(Polyline polyline) {
        Log.e("Polyline position", " -- " + polyline.getTag());
        onButtonShowPopupWindowClick("  " + polyline.getTag());
    }
});

Set any reference in polyline using setTag method

            Polyline line = mMap.addPolyline(lineOptions);
            line.setTag("" + i);
            line.setClickable(true);

Open PopupWindow in polyline click

public void onButtonShowPopupWindowClick(String count) {

String[] timeDistance = count.split(",");

// get a reference to the already created main layout
LinearLayout mainLayout = (LinearLayout)
        findViewById(R.id.whole_layout);

// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater)
        getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.polyline_window, null);

((TextView) popupView.findViewById(R.id.time)).setText("30 mins");
((TextView) popupView.findViewById(R.id.distance)).setText("20 km");

// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);

// show the popup window
popupWindow.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);

// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        popupWindow.dismiss();
        return true;
    }
});

}




回答3:


Ultimate solution by Hiren Patel

I have done with easiest way like below:

private GoogleMap mMap;

While adding marker on Google Map:

LatLng mLatLng = new LatLng(YourLatitude, YourLongitude); 

//transparent 1x1 drawable 
BitmapDescriptor transparent = BitmapDescriptorFactory.fromResource(R.drawable.transparent_box_1dp); 

// add marker 
mMap.addMarker(new MarkerOptions().position(mLatLng).title("My Title").snippet("My Snippet"+"\n"+"1st Line Text"+"\n"+"2nd Line Text"+"\n"+"3rd Line Text").icon(transparent);

After that put below code for InfoWindow adapter on Google Map, in onMapReady() if you want:

mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

      @Override
      public View getInfoWindow(Marker arg0) {
         return null;
      }

      @Override
      public View getInfoContents(Marker marker) {

        LinearLayout info = new LinearLayout(mContext);
        info.setOrientation(LinearLayout.VERTICAL);

        TextView title = new TextView(mContext);
        title.setTextColor(Color.BLACK);
        title.setGravity(Gravity.CENTER);
        title.setTypeface(null, Typeface.BOLD);
        title.setText(marker.getTitle());

        TextView snippet = new TextView(mContext);
        snippet.setTextColor(Color.GRAY);
        snippet.setText(marker.getSnippet());

        info.addView(title);
        info.addView(snippet);

      return info;
    }
});


来源:https://stackoverflow.com/questions/39543904/google-map-android-api-v2-infowindow-on-polyline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!