2016-09-17 101 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; 
    } 
}); 

}

相關問題