2016-09-17 11 views
4

私の地図上にpolylineを描いていますので、今すぐユーザーにデータを表示する必要があります。GoogleマップAndroid API v2 - ポリライン上のInfoWindow?

polylineにテキストまたはインフォウィンドウを描く方法はありますか?

私のような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. onMapClickイベントでOnMapClickListener 2を実装し、ユーザがポリラインに触れたかどうかを決定します。私は四分木にポリライン点を格納し、ユーザが画面に触れたところから最も近い点を四分木で検索することでこれを行います。距離がある閾値(ポリラインに近い)内にある場合は、上で参照した不可視マーカを作成し、情報ウィンドウを開きます。タッチがしきい値内にない場合は、onMapClickイベントを無視します。 3.次のonMapClickイベントで、以前に作成した目に見えないマーカーを削除して、目に見えないマーカーがメモリを占有しないようにします。希望が役立ちます。

+0

私はGoogleマップアンドロイドなどのポリラインに設定する必要がマーカー上の情報を設定する必要はありません。 –

+0

私は理解していますが、組み込みの方法はないので、ポリライン上に不可視のマーカーを作成し、インフォメーションウィンドウを開いてインフォメーションウィンドウを開いたように見えるようにする必要がありますポリラインの場合上記の私の改訂された答えを見てください。 –

+0

私はそれを試しましたが、一度に1つのinfoWindowしか表示されません。 3つのinfoWindowを同時に開く必要がある場合 – NetDemo

0

利用のGoogle方向API

が地図にOnPolylineClickListener追加のgetTag

setTagメソッドに

  Polyline line = mMap.addPolyline(lineOptions); 
      line.setTag("" + i); 
      line.setClickable(true); 
を使用してポリラインのいずれかの参照を設定し
mMap.setOnPolylineClickListener(new GoogleMap.OnPolylineClickListener() { 
    @Override 
    public void onPolylineClick(Polyline polyline) { 
     Log.e("Polyline position", " -- " + polyline.getTag()); 
     onButtonShowPopupWindowClick(" " + polyline.getTag()); 
    } 
}); 

を使用して参照を取得ルートを取得し、ポリラインを描画します

ポリラインクリックでポップアップウィンドウを開く

ます。public void 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; 
    } 
}); 

}

関連する問題