2016-09-17 102 views
4

我在我的地图上绘制了polyline,我现在需要向用户显示一些数据。Google map Android API v2 - InfoWindow on polyline?

如何在每个polyline上绘制文本或InfoWindow?

我添加polyline喜欢:

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

例如,我需要这样的:

enter image description here

回答

1

我做到这一点通过创建折线一种无形的标记,然后显示出它的信息窗口。例如:

//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.在下一个onMapClick事件中,删除以前创建的不可见标记,这样就不会有一堆占用内存的不可见标记。希望有所帮助。

+0

我并不需要设置标记,我需要像谷歌地图Android的折线设置信息。 –

+0

我明白,但没有一种内置的方式来做到这一点,所以你必须使用其他方法,例如在多段线上创建一个不可见的标记并打开其信息窗口,以便它看起来像打开了一个信息窗口为折线。看到我上面修改的答案。 –

+0

我试过了,但一次只显示一个infoWindow。因为我们需要所有三个infoWindow应该同时打开 – NetDemo

0

使用谷歌的方向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); 
坐落在折线任何参考拿到参考

在折线中打开PopupWindow点击

公共无效onButtonShowPopupWindowClick(字符串计数){

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

}

相关问题