Yandex Map Kit for Androidを使用した地図上の方向インジケーターのあるパス

こんにちは



この投稿では、以下を考慮してください。

  1. プロジェクトへのYandex Map Kit Androidライブラリの埋め込み
  2. 地図上の方向インジケータとonClickイベントを使用してパスを表示するためのOverlayクラスの拡張機能の作成




画像



1.プロジェクトへのライブラリの埋め込み(Eclipse、Windows)



ここからライブラリにアーカイブをロードし、 APIキーを取得します

Eclipseでプロジェクトを作成し、\ yandexmapkit-library \ resおよび\ yandexmapkit-library \ libsフォルダーを置き換えて、ダウンロードしたアーカイブからプロジェクトフォルダーにコピーします。

プロジェクトの設定で、プロジェクトのlibsフォルダーからyandexmapkit-android.jarへのリンクを追加します(ビルドパス->ビルドパス->ライブラリ->外部JARの追加)

プロジェクトのマニフェストに権限を追加します



<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
      
      





アクティビティのマークアップを変更する



  <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/RelativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ru.yandex.yandexmapkit.MapView android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey=" " /> </RelativeLayout>
      
      





2.地図上の地図に方向インジケータとonClickイベントを含むパスを表示するために、Overlayクラスの拡張機能を作成する



パスを描画するには、IRenderインターフェイスを実装するOverlayIRenderクラスを拡張する必要があります



 public class MyOverlayIRender extends OverlayIRender { Overlay mOverlay; public MyOverlayIRender(Overlay overlay) { super(); mOverlay = overlay; } @Override public void draw(Canvas canvas, OverlayItem arg1) { super.draw(canvas, arg1); List<OverlayItem> oi = mOverlay.getOverlayItems(); if (oi.size() < 2) return; Paint mPaint = new Paint(); mPaint.setDither(true); mPaint.setColor(Color.RED); mPaint.setStyle(Paint.Style.FILL_AND_STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(2); mPaint.setAntiAlias(true); for (int i = 0; i < oi.size() - 1; i++) { Path path = new Path(); ScreenPoint p1 = mOverlay.getMapController().getScreenPoint( oi.get(i).getGeoPoint()); ScreenPoint p2 = mOverlay.getMapController().getScreenPoint( oi.get(i + 1).getGeoPoint()); float angle = (float) (Math.atan2(p2.getY() - p1.getY(), p2.getX()- p1.getX()) * 180 / 3.14); path.moveTo(p2.getX(), p2.getY()); path.lineTo(p1.getX(), p1.getY()); canvas.drawPath(path, mPaint); canvas.save(); canvas.rotate(angle + 90, p2.getX(), p2.getY()); Path path2 = new Path(); path2.moveTo(p2.getX(), p2.getY()); path2.lineTo(p2.getX() + 5, p2.getY() + 8.66f); path2.lineTo(p2.getX() - 5, p2.getY() + 8.66f); path2.close(); canvas.drawPath(path2, mPaint); canvas.restore(); } } }
      
      





パスに追加されたポイントの数が1を超える場合、パスのセグメントと方向の矢印がループに描画されます。

次に、オーバーレイの子孫であるMyPathOverLayを作成し、レンダリング用にMyOverlayIRenderを割り当てる必要があります。



 public class MyPathOverLay extends Overlay { MapView mMmapView; public MyPathOverLay(MapController arg0, MapView mapView) { super(arg0); mMmapView = mapView; this.setIRender(new MyOverlayIRender(this)); } @Override public int compareTo(Object arg0) { return 0; } @Override public boolean onLongPress(float x, float y) { OverlayItem m = new OverlayItem( this.c.getGeoPoint(new ScreenPoint(x, y)), BitmapFactory.decodeResource( this.c.getContext().getResources(), R.drawable.flag2leftred)); m.setOffsetY(-23); this.addOverlayItem(m); this.c.setPositionNoAnimationTo(this.c .getGeoPoint(new ScreenPoint(x, y))); return true; } }
      
      





オーバーライドされたonLongPressメソッドでは、マップを長押ししてパスの中間点を追加します。

R.drawable.flag2leftred-ポイントの画像、

m.setOffsetY(-23); -地図をクリックするポイントを基準にして画像をシフトする必要がある場合に選択されるオフセット。デフォルトでは、画像の中心は押すポイントに揃えられます。

オーバーレイをマップに追加してテストする必要があります



 public class BlogYandexActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MapView mMap = (MapView) findViewById(R.id.map); MapController mMapController = mMap.getMapController(); OverlayManager mOverlayManager = mMapController.getOverlayManager(); mOverlayManager.addOverlay(new MyPathOverLay(mMapController, mMap)); mMap.showBuiltInScreenButtons(true); mOverlayManager.getMyLocation().setEnabled(false); } }
      
      





結果



画像







All Articles