谷歌地图Android API v2-多段线上的信息窗口?

谷歌地图Android API v2-多段线上的信息窗口?,android,android-maps-v2,polyline,android-maps-utils,Android,Android Maps V2,Polyline,Android Maps Utils,我正在地图上绘制多段线,我现在需要向用户显示一些数据 如何在每条多段线上绘制文本或信息窗口 我添加多段线如下: ArrayList<LatLng> points = null; PolylineOptions lineOptions = null; MarkerOptions markerOptions = new MarkerOptions(); // Traversing through all the routes for(int i=0;i<result.size()

我正在地图上绘制
多段线
,我现在需要向用户显示一些数据

如何在每条
多段线上绘制文本或信息窗口

我添加
多段线
如下:

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);
}
arraylistpoints=null;
PolylineOptions lineOptions=null;
MarkerOptions MarkerOptions=新MarkerOptions();
//穿越所有路线

对于(int i=0;i我通过在多段线上创建一个不可见的标记,然后显示其信息窗口来完成此操作。例如:

//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();
更新以包括触摸多段线和打开信息窗口的常规方法: 1.实现一个OnMapClickListener 2.在onMapClick事件中,确定用户是否触摸了多段线。我通过将多段线点存储在四叉树中,并在四叉树中搜索用户触摸屏幕的最近点来完成此操作。如果距离在某个阈值内(即接近多段线),创建上面引用的不可见标记并打开其信息窗口。如果触摸不在阈值范围内,则忽略onMapClick事件。
3.在下一个onmaplick事件中,删除先前创建的不可见标记,这样就不会有一堆不可见标记占用内存。希望有帮助。

使用Google Direction API获取路线并绘制多段线

在映射中添加OnPolylineClickListener并使用getTag获取引用

mMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() {
    @Override
    public void onPolylineClick(Polyline polyline) {
        Log.e("Polyline position", " -- " + polyline.getTag());
        onButtonShowPopupWindowClick("  " + polyline.getTag());
    }
});
使用setTag方法设置多段线中的任何参照

            Polyline line = mMap.addPolyline(lineOptions);
            line.setTag("" + i);
            line.setClickable(true);
在多段线中打开弹出窗口单击

public void onButtonShowPopupIndowClick(字符串计数){

}

最终解决方案

我已经完成了以下最简单的方法:

private GoogleMap mMap;
在Google地图上添加标记时:

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);
在此之后,如果需要,请在Google地图上的
onMapReady()
中为InfoWindowAdapter添加以下代码:

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;
    }
});

我不需要在标记上设置信息,我需要像google map android一样在多段线上设置。我理解这一点,但没有内置的方法,所以你必须使用其他方法,比如在多段线上创建一个不可见的标记,并打开其信息窗口,这样看起来你已经为多段线打开了一个信息窗口。请参阅我的修订版上面的答案。我试过了,但一次只显示一个信息窗口。我们需要的是,三个信息窗口应该同时打开
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;
    }
});