2017-08-21 5 views
0

Googleマップとポリラインを使用してルートを指定するアプリケーションで作業しています。私が望むのは、ユーザーがマップを開くと、ユーザーとポリラインの間の最も近いポイントにマーカーが自動的に配置されるということです。ArrayListから最近点を取得<LatLng>

私はすでに、ユーザーからポリラインに最も近い点を取得する関数を設定しました。距離が<600の場合、その点にマーカが置かれます。

public ArrayList<LatLng> polylineList(){ 

    ArrayList<LatLng> coords = new ArrayList<LatLng>(); 

    coords.add(new LatLng(25.71687391423798,-100.3730825815284)); 
    coords.add(new LatLng(25.71682182829175,-100.3733097782636)); 
    coords.add(new LatLng(25.71678126641878, -100.374090669652)); 
    coords.add(new LatLng(25.71683201122758, -100.3731781269444)); 
    coords.add(new LatLng(25.72018196813817, -100.3735084186905)); 
    coords.add(new LatLng(25.72031604576068, -100.3736959395207)); 
    coords.add(new LatLng(25.71998332400039, -100.3762802484946)); 
    coords.add(new LatLng(25.71994816609866, -100.3764646004368)); 
    coords.add(new LatLng(25.72208094229626, -100.3773901496331)); 
    coords.add(new LatLng(25.72139701948525, -100.3784182403878)); 
    coords.add(new LatLng(25.72088085424743, -100.3792655793197)); 
    coords.add(new LatLng(25.7211341509678, -100.3796660319109)); 
    coords.add(new LatLng(25.72119368137114, -100.3807655073227)); 
    coords.add(new LatLng(25.72102639768713, -100.3811907891904)); 
    coords.add(new LatLng(25.72079498457438, -100.3810006195533)); 
    coords.add(new LatLng(25.720670392499, -100.3799969462212)); 
    coords.add(new LatLng(25.72062014771428, -100.3798718252661)); 
    coords.add(new LatLng(25.71970136267208, -100.3812225396649)); 
    coords.add(new LatLng(25.71935874972525, -100.3814558417726)); 
    coords.add(new LatLng(25.71758071083912, -100.3839700351668)); 
    coords.add(new LatLng(25.71722347802044, -100.3838886540422)); 
    coords.add(new LatLng(25.71531766471047, -100.3815788239292)); 
    coords.add(new LatLng(25.71564916096481, -100.3804237189767)); 
    coords.add(new LatLng(25.7159267510913, -100.3800793191019)); 
    coords.add(new LatLng(25.71627234185268, -100.3795543063391)); 
    coords.add(new LatLng(25.71665021282444, -100.3790150094068)); 

リターン:しかし、私が欲しいのは、これは私のコード

ポリラインのArrayListです...ユーザーから< 600メートルです(ポリラインの)すべての座標を取得し、最も近いものを選択することですコーズ;その後 }

、私はdistaceが< 600であれば、それは、マーカーを描画します、ユーザの位置を取得し、半正矢式を使用して、すべてのArrayListのインデックスと座標を評価します。

for (int i = 0; i < polylineList().size(); i++) { 
        Double latit = polylineList.get(i).latitude; 
        Double longit = polylineList.get(i).longitude; 
        LatLng newLatLng = new LatLng(latit, longit); 
        Double distance = distanceHaversine(userlatit, latit, userlongit, longit); 
        if (distance < 600) { 
         drawMarker(newLatLng); 
        } 
       } 

これが正しく動作しているが、それは、ユーザの位置から< 600メートルある座標系でマーカーの多くを描画します。私が望むのは、ポリラインの最も近い点にのみマーカーを描くことです。ユーザからの< 600であるすべての座標を評価し、次に最も近いものを選択するものがあります。助けをお待ちしています。

答えて

0

、利用者から利用できるポイントへの最短距離を見つけるlatlngが分の距離に関連付けられますし、

private int minDistance = 0; 
    private LatLng closestLatLng = null; 
    for (int i = 0; i < polylineList().size(); i++) { 
         Double latit = polylineList.get(i).latitude; 
         Double longit = polylineList.get(i).longitude; 
         LatLng newLatLng = new LatLng(latit, longit); 
         Double distance = distanceHaversine(userlatit, latit, userlongit, longit); 
         if (distance < minDistance) { 
          closestLatLng = newLatLng; 
          minDistance = distance; 
         } 
        } 
    if(closestLatLng !=null && minDistance < 600){ 
     drawMarker(closestLatLng); 
    } 
+0

あなたの返事のおかげで、私は理由を理解できません:距離(minDistance)、minDistance = 0 sooo、もし私がその距離が負であると仮定しているとすれば...私はそう思います... –

0

使って、マップの地図Utilsのクラスを使用すると、特定の場所を見つけることができるということがあることを利用しを描きますここで

if (PolyUtil.isLocationOnPath(start, points, true, 10)) { 
     // do your stuf here 
} 

開始ポリライン上の現在地からの距離は、現在の場所であるポリライン点の配列であり、最後のパラメータはメートルでパスの範囲(距離)のためのものである点。

これはあなたのgradleにこの依存関係を追加する必要があります。

compile 'com.google.maps.android:android-maps-utils:0.5+' 

希望します。

+0

ありがとうあなたの答え。これは、10メートルの距離内のすべてのポイントを取得しますが、10メートルで多くのポイントを持っている場合...私はちょうどユーザーから最も近いものを見つける... –

関連する問題